2
2
3
3
use rustc:: session:: config:: nightly_options;
4
4
use rustc:: session:: parse:: feature_err;
5
- use rustc:: ty:: TyCtxt ;
6
5
use rustc_errors:: struct_span_err;
7
6
use rustc_hir:: def_id:: DefId ;
8
7
use rustc_span:: symbol:: sym;
@@ -15,18 +14,21 @@ pub trait NonConstOp: std::fmt::Debug {
15
14
/// Whether this operation can be evaluated by miri.
16
15
const IS_SUPPORTED_IN_MIRI : bool = true ;
17
16
18
- /// Returns a boolean indicating whether the feature gate that would allow this operation is
19
- /// enabled, or `None` if such a feature gate does not exist.
20
- fn feature_gate ( _tcx : TyCtxt < ' tcx > ) -> Option < bool > {
17
+ /// Returns the `Symbol` corresponding to the feature gate that would enable this operation,
18
+ /// or `None` if such a feature gate does not exist.
19
+ fn feature_gate ( ) -> Option < Symbol > {
21
20
None
22
21
}
23
22
24
23
/// Returns `true` if this operation is allowed in the given item.
25
24
///
26
25
/// This check should assume that we are not in a non-const `fn`, where all operations are
27
26
/// legal.
27
+ ///
28
+ /// By default, it returns `true` if and only if this operation has a corresponding feature
29
+ /// gate and that gate is enabled.
28
30
fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
29
- Self :: feature_gate ( item. tcx ) . unwrap_or ( false )
31
+ Self :: feature_gate ( ) . map_or ( false , |gate| item. tcx . features ( ) . enabled ( gate ) )
30
32
}
31
33
32
34
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -55,8 +57,8 @@ pub trait NonConstOp: std::fmt::Debug {
55
57
#[ derive( Debug ) ]
56
58
pub struct Downcast ;
57
59
impl NonConstOp for Downcast {
58
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
59
- Some ( tcx . features ( ) . const_if_match )
60
+ fn feature_gate ( ) -> Option < Symbol > {
61
+ Some ( sym :: const_if_match)
60
62
}
61
63
}
62
64
@@ -147,8 +149,8 @@ impl NonConstOp for HeapAllocation {
147
149
#[ derive( Debug ) ]
148
150
pub struct IfOrMatch ;
149
151
impl NonConstOp for IfOrMatch {
150
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
151
- Some ( tcx . features ( ) . const_if_match )
152
+ fn feature_gate ( ) -> Option < Symbol > {
153
+ Some ( sym :: const_if_match)
152
154
}
153
155
154
156
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -175,8 +177,8 @@ impl NonConstOp for LiveDrop {
175
177
#[ derive( Debug ) ]
176
178
pub struct Loop ;
177
179
impl NonConstOp for Loop {
178
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
179
- Some ( tcx . features ( ) . const_loop )
180
+ fn feature_gate ( ) -> Option < Symbol > {
181
+ Some ( sym :: const_loop)
180
182
}
181
183
182
184
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -203,8 +205,8 @@ impl NonConstOp for CellBorrow {
203
205
#[ derive( Debug ) ]
204
206
pub struct MutBorrow ;
205
207
impl NonConstOp for MutBorrow {
206
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
207
- Some ( tcx . features ( ) . const_mut_refs )
208
+ fn feature_gate ( ) -> Option < Symbol > {
209
+ Some ( sym :: const_mut_refs)
208
210
}
209
211
210
212
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -238,8 +240,8 @@ impl NonConstOp for MutBorrow {
238
240
#[ derive( Debug ) ]
239
241
pub struct MutAddressOf ;
240
242
impl NonConstOp for MutAddressOf {
241
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
242
- Some ( tcx . features ( ) . const_mut_refs )
243
+ fn feature_gate ( ) -> Option < Symbol > {
244
+ Some ( sym :: const_mut_refs)
243
245
}
244
246
245
247
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -256,16 +258,16 @@ impl NonConstOp for MutAddressOf {
256
258
#[ derive( Debug ) ]
257
259
pub struct MutDeref ;
258
260
impl NonConstOp for MutDeref {
259
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
260
- Some ( tcx . features ( ) . const_mut_refs )
261
+ fn feature_gate ( ) -> Option < Symbol > {
262
+ Some ( sym :: const_mut_refs)
261
263
}
262
264
}
263
265
264
266
#[ derive( Debug ) ]
265
267
pub struct Panic ;
266
268
impl NonConstOp for Panic {
267
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
268
- Some ( tcx . features ( ) . const_panic )
269
+ fn feature_gate ( ) -> Option < Symbol > {
270
+ Some ( sym :: const_panic)
269
271
}
270
272
271
273
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -282,8 +284,8 @@ impl NonConstOp for Panic {
282
284
#[ derive( Debug ) ]
283
285
pub struct RawPtrComparison ;
284
286
impl NonConstOp for RawPtrComparison {
285
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
286
- Some ( tcx . features ( ) . const_compare_raw_pointers )
287
+ fn feature_gate ( ) -> Option < Symbol > {
288
+ Some ( sym :: const_compare_raw_pointers)
287
289
}
288
290
289
291
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -300,8 +302,8 @@ impl NonConstOp for RawPtrComparison {
300
302
#[ derive( Debug ) ]
301
303
pub struct RawPtrDeref ;
302
304
impl NonConstOp for RawPtrDeref {
303
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
304
- Some ( tcx . features ( ) . const_raw_ptr_deref )
305
+ fn feature_gate ( ) -> Option < Symbol > {
306
+ Some ( sym :: const_raw_ptr_deref)
305
307
}
306
308
307
309
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -318,8 +320,8 @@ impl NonConstOp for RawPtrDeref {
318
320
#[ derive( Debug ) ]
319
321
pub struct RawPtrToIntCast ;
320
322
impl NonConstOp for RawPtrToIntCast {
321
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
322
- Some ( tcx . features ( ) . const_raw_ptr_to_usize_cast )
323
+ fn feature_gate ( ) -> Option < Symbol > {
324
+ Some ( sym :: const_raw_ptr_to_usize_cast)
323
325
}
324
326
325
327
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -386,11 +388,12 @@ pub struct UnionAccess;
386
388
impl NonConstOp for UnionAccess {
387
389
fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
388
390
// Union accesses are stable in all contexts except `const fn`.
389
- item. const_kind ( ) != ConstKind :: ConstFn || Self :: feature_gate ( item. tcx ) . unwrap ( )
391
+ item. const_kind ( ) != ConstKind :: ConstFn
392
+ || item. tcx . features ( ) . enabled ( Self :: feature_gate ( ) . unwrap ( ) )
390
393
}
391
394
392
- fn feature_gate ( tcx : TyCtxt < ' _ > ) -> Option < bool > {
393
- Some ( tcx . features ( ) . const_fn_union )
395
+ fn feature_gate ( ) -> Option < Symbol > {
396
+ Some ( sym :: const_fn_union)
394
397
}
395
398
396
399
fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
0 commit comments