|
1 | 1 | //! Concrete error types for all operations which may be invalid in a certain const context.
|
2 | 2 |
|
3 | 3 | use rustc::hir::def_id::DefId;
|
4 |
| -use rustc::mir::BorrowKind; |
5 | 4 | use rustc::session::config::nightly_options;
|
6 | 5 | use rustc::ty::TyCtxt;
|
7 | 6 | use syntax::feature_gate::feature_err;
|
@@ -181,38 +180,53 @@ impl NonConstOp for Loop {
|
181 | 180 | }
|
182 | 181 |
|
183 | 182 | #[derive(Debug)]
|
184 |
| -pub struct MutBorrow(pub BorrowKind); |
| 183 | +pub struct CellBorrow; |
| 184 | +impl NonConstOp for CellBorrow { |
| 185 | + fn emit_error(&self, item: &Item<'_, '_>, span: Span) { |
| 186 | + span_err!(item.tcx.sess, span, E0492, |
| 187 | + "cannot borrow a constant which may contain \ |
| 188 | + interior mutability, create a static instead"); |
| 189 | + } |
| 190 | +} |
| 191 | + |
| 192 | +#[derive(Debug)] |
| 193 | +pub struct MutBorrow; |
185 | 194 | impl NonConstOp for MutBorrow {
|
| 195 | + fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> { |
| 196 | + Some(tcx.features().const_mut_refs) |
| 197 | + } |
| 198 | + |
186 | 199 | fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
|
187 |
| - let kind = self.0; |
188 |
| - if let BorrowKind::Mut { .. } = kind { |
189 |
| - let mut err = struct_span_err!(item.tcx.sess, span, E0017, |
190 |
| - "references in {}s may only refer \ |
191 |
| - to immutable values", item.const_kind()); |
192 |
| - err.span_label(span, format!("{}s require immutable values", |
193 |
| - item.const_kind())); |
194 |
| - if item.tcx.sess.teach(&err.get_code().unwrap()) { |
195 |
| - err.note("References in statics and constants may only refer \ |
196 |
| - to immutable values.\n\n\ |
197 |
| - Statics are shared everywhere, and if they refer to \ |
198 |
| - mutable data one might violate memory safety since \ |
199 |
| - holding multiple mutable references to shared data \ |
200 |
| - is not allowed.\n\n\ |
201 |
| - If you really want global mutable state, try using \ |
202 |
| - static mut or a global UnsafeCell."); |
203 |
| - } |
204 |
| - err.emit(); |
205 |
| - } else { |
206 |
| - span_err!(item.tcx.sess, span, E0492, |
207 |
| - "cannot borrow a constant which may contain \ |
208 |
| - interior mutability, create a static instead"); |
| 200 | + let mut err = feature_err( |
| 201 | + &item.tcx.sess.parse_sess, |
| 202 | + sym::const_mut_refs, |
| 203 | + span, |
| 204 | + &format!("references in {}s may only refer \ |
| 205 | + to immutable values", item.const_kind()) |
| 206 | + ); |
| 207 | + err.span_label(span, format!("{}s require immutable values", |
| 208 | + item.const_kind())); |
| 209 | + if item.tcx.sess.teach(&err.get_code().unwrap()) { |
| 210 | + err.note("References in statics and constants may only refer \ |
| 211 | + to immutable values.\n\n\ |
| 212 | + Statics are shared everywhere, and if they refer to \ |
| 213 | + mutable data one might violate memory safety since \ |
| 214 | + holding multiple mutable references to shared data \ |
| 215 | + is not allowed.\n\n\ |
| 216 | + If you really want global mutable state, try using \ |
| 217 | + static mut or a global UnsafeCell."); |
209 | 218 | }
|
| 219 | + err.emit(); |
210 | 220 | }
|
211 | 221 | }
|
212 | 222 |
|
213 | 223 | #[derive(Debug)]
|
214 | 224 | pub struct MutDeref;
|
215 |
| -impl NonConstOp for MutDeref {} |
| 225 | +impl NonConstOp for MutDeref { |
| 226 | + fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> { |
| 227 | + Some(tcx.features().const_mut_refs) |
| 228 | + } |
| 229 | +} |
216 | 230 |
|
217 | 231 | #[derive(Debug)]
|
218 | 232 | pub struct Panic;
|
|
0 commit comments