22
33use rustc:: session:: config:: nightly_options;
44use rustc:: session:: parse:: feature_err;
5- use rustc:: ty:: TyCtxt ;
65use rustc_errors:: struct_span_err;
76use rustc_hir:: def_id:: DefId ;
87use rustc_span:: symbol:: sym;
@@ -15,18 +14,21 @@ pub trait NonConstOp: std::fmt::Debug {
1514 /// Whether this operation can be evaluated by miri.
1615 const IS_SUPPORTED_IN_MIRI : bool = true ;
1716
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 > {
2120 None
2221 }
2322
2423 /// Returns `true` if this operation is allowed in the given item.
2524 ///
2625 /// This check should assume that we are not in a non-const `fn`, where all operations are
2726 /// 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.
2830 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 ) )
3032 }
3133
3234 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -55,8 +57,8 @@ pub trait NonConstOp: std::fmt::Debug {
5557#[ derive( Debug ) ]
5658pub struct Downcast ;
5759impl 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)
6062 }
6163}
6264
@@ -147,8 +149,8 @@ impl NonConstOp for HeapAllocation {
147149#[ derive( Debug ) ]
148150pub struct IfOrMatch ;
149151impl 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)
152154 }
153155
154156 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -175,8 +177,8 @@ impl NonConstOp for LiveDrop {
175177#[ derive( Debug ) ]
176178pub struct Loop ;
177179impl 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)
180182 }
181183
182184 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -203,8 +205,8 @@ impl NonConstOp for CellBorrow {
203205#[ derive( Debug ) ]
204206pub struct MutBorrow ;
205207impl 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)
208210 }
209211
210212 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -238,8 +240,8 @@ impl NonConstOp for MutBorrow {
238240#[ derive( Debug ) ]
239241pub struct MutAddressOf ;
240242impl 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)
243245 }
244246
245247 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -256,16 +258,16 @@ impl NonConstOp for MutAddressOf {
256258#[ derive( Debug ) ]
257259pub struct MutDeref ;
258260impl 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)
261263 }
262264}
263265
264266#[ derive( Debug ) ]
265267pub struct Panic ;
266268impl 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)
269271 }
270272
271273 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -282,8 +284,8 @@ impl NonConstOp for Panic {
282284#[ derive( Debug ) ]
283285pub struct RawPtrComparison ;
284286impl 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)
287289 }
288290
289291 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -300,8 +302,8 @@ impl NonConstOp for RawPtrComparison {
300302#[ derive( Debug ) ]
301303pub struct RawPtrDeref ;
302304impl 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)
305307 }
306308
307309 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -318,8 +320,8 @@ impl NonConstOp for RawPtrDeref {
318320#[ derive( Debug ) ]
319321pub struct RawPtrToIntCast ;
320322impl 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)
323325 }
324326
325327 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
@@ -386,11 +388,12 @@ pub struct UnionAccess;
386388impl NonConstOp for UnionAccess {
387389 fn is_allowed_in_item ( & self , item : & Item < ' _ , ' _ > ) -> bool {
388390 // 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 ( ) )
390393 }
391394
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)
394397 }
395398
396399 fn emit_error ( & self , item : & Item < ' _ , ' _ > , span : Span ) {
0 commit comments