|
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,36 +180,39 @@ 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 {
|
186 | 195 | fn feature_gate(tcx: TyCtxt<'_>) -> Option<bool> {
|
187 | 196 | Some(tcx.features().const_mut_refs)
|
188 | 197 | }
|
189 | 198 |
|
190 | 199 | fn emit_error(&self, item: &Item<'_, '_>, span: Span) {
|
191 |
| - let kind = self.0; |
192 |
| - if let BorrowKind::Mut { .. } = kind { |
193 |
| - let mut err = struct_span_err!(item.tcx.sess, span, E0017, |
194 |
| - "references in {}s may only refer \ |
195 |
| - to immutable values", item.const_kind()); |
196 |
| - err.span_label(span, format!("{}s require immutable values", |
197 |
| - item.const_kind())); |
198 |
| - if item.tcx.sess.teach(&err.get_code().unwrap()) { |
199 |
| - err.note("References in statics and constants may only refer \ |
200 |
| - to immutable values.\n\n\ |
201 |
| - Statics are shared everywhere, and if they refer to \ |
202 |
| - mutable data one might violate memory safety since \ |
203 |
| - holding multiple mutable references to shared data \ |
204 |
| - is not allowed.\n\n\ |
205 |
| - If you really want global mutable state, try using \ |
206 |
| - static mut or a global UnsafeCell."); |
207 |
| - } |
208 |
| - err.emit(); |
209 |
| - } else { |
210 |
| - span_err!(item.tcx.sess, span, E0492, |
211 |
| - "cannot borrow a constant which may contain \ |
212 |
| - interior mutability, create a static instead"); |
| 200 | + let mut err = struct_span_err!(item.tcx.sess, span, E0017, |
| 201 | + "references in {}s may only refer \ |
| 202 | + to immutable values", item.const_kind()); |
| 203 | + err.span_label(span, format!("{}s require immutable values", |
| 204 | + item.const_kind())); |
| 205 | + if item.tcx.sess.teach(&err.get_code().unwrap()) { |
| 206 | + err.note("References in statics and constants may only refer \ |
| 207 | + to immutable values.\n\n\ |
| 208 | + Statics are shared everywhere, and if they refer to \ |
| 209 | + mutable data one might violate memory safety since \ |
| 210 | + holding multiple mutable references to shared data \ |
| 211 | + is not allowed.\n\n\ |
| 212 | + If you really want global mutable state, try using \ |
| 213 | + static mut or a global UnsafeCell."); |
213 | 214 | }
|
| 215 | + err.emit(); |
214 | 216 | }
|
215 | 217 | }
|
216 | 218 |
|
|
0 commit comments