@@ -169,6 +169,20 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
169
169
& self . stack
170
170
}
171
171
172
+ /// Returns true if the current frame or any parent frame is part of a ctfe.
173
+ ///
174
+ /// Used to disable features in const eval, which do not have a rfc enabling
175
+ /// them or which can't be written in a way that they produce the same output
176
+ /// that evaluating the code at runtime would produce.
177
+ pub fn const_env ( & self ) -> bool {
178
+ for frame in self . stack . iter ( ) . rev ( ) {
179
+ if let StackPopCleanup :: MarkStatic ( _) = frame. return_to_block {
180
+ return true ;
181
+ }
182
+ }
183
+ false
184
+ }
185
+
172
186
pub ( crate ) fn str_to_value ( & mut self , s : & str ) -> EvalResult < ' tcx , Value > {
173
187
let ptr = self . memory . allocate_cached ( s. as_bytes ( ) ) ?;
174
188
Ok ( Value :: ByValPair ( PrimVal :: Ptr ( ptr) , PrimVal :: from_u128 ( s. len ( ) as u128 ) ) )
@@ -655,7 +669,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
655
669
}
656
670
657
671
Len ( ref lvalue) => {
658
- if self . frame ( ) . const_env ( ) {
672
+ if self . const_env ( ) {
659
673
return Err ( EvalError :: NeedsRfc ( "computing the length of arrays" . to_string ( ) ) ) ;
660
674
}
661
675
let src = self . eval_lvalue ( lvalue) ?;
@@ -704,7 +718,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
704
718
}
705
719
706
720
NullaryOp ( mir:: NullOp :: Box , ty) => {
707
- if self . frame ( ) . const_env ( ) {
721
+ if self . const_env ( ) {
708
722
return Err ( EvalError :: NeedsRfc ( "\" heap\" allocations" . to_string ( ) ) ) ;
709
723
}
710
724
// FIXME: call the `exchange_malloc` lang item if available
@@ -718,7 +732,7 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
718
732
}
719
733
720
734
NullaryOp ( mir:: NullOp :: SizeOf , ty) => {
721
- if self . frame ( ) . const_env ( ) {
735
+ if self . const_env ( ) {
722
736
return Err ( EvalError :: NeedsRfc ( "computing the size of types (size_of)" . to_string ( ) ) ) ;
723
737
}
724
738
let size = self . type_size ( ty) ?. expect ( "SizeOf nullary MIR operator called for unsized type" ) ;
@@ -1592,12 +1606,6 @@ impl<'a, 'tcx> EvalContext<'a, 'tcx> {
1592
1606
}
1593
1607
1594
1608
impl < ' tcx > Frame < ' tcx > {
1595
- pub fn const_env ( & self ) -> bool {
1596
- match self . return_to_block {
1597
- StackPopCleanup :: MarkStatic ( _) => true ,
1598
- _ => false ,
1599
- }
1600
- }
1601
1609
pub fn get_local ( & self , local : mir:: Local ) -> EvalResult < ' tcx , Value > {
1602
1610
// Subtract 1 because we don't store a value for the ReturnPointer, the local with index 0.
1603
1611
self . locals [ local. index ( ) - 1 ] . ok_or ( EvalError :: DeadLocal )
0 commit comments