@@ -51,13 +51,10 @@ pub struct CompileTimeInterpreter<'mir, 'tcx> {
5151 /// The virtual call stack.
5252 pub ( super ) stack : Vec < Frame < ' mir , ' tcx > > ,
5353
54- /// We need to make sure consts never point to anything mutable, even recursively. That is
55- /// relied on for pattern matching on consts with references.
56- /// To achieve this, two pieces have to work together:
57- /// * Interning makes everything outside of statics immutable.
58- /// * Pointers to allocations inside of statics can never leak outside, to a non-static global.
59- /// This boolean here controls the second part.
60- pub ( super ) can_access_statics : CanAccessStatics ,
54+ /// Pattern matching on consts with references would be unsound if those references
55+ /// could point to anything mutable. Therefore, when evaluating consts and when constructing valtrees,
56+ /// we ensure that only immutable global memory can be accessed.
57+ pub ( super ) can_access_mut_global : CanAccessMutGlobal ,
6158
6259 /// Whether to check alignment during evaluation.
6360 pub ( super ) check_alignment : CheckAlignment ,
@@ -73,26 +70,26 @@ pub enum CheckAlignment {
7370}
7471
7572#[ derive( Copy , Clone , PartialEq ) ]
76- pub ( crate ) enum CanAccessStatics {
73+ pub ( crate ) enum CanAccessMutGlobal {
7774 No ,
7875 Yes ,
7976}
8077
81- impl From < bool > for CanAccessStatics {
78+ impl From < bool > for CanAccessMutGlobal {
8279 fn from ( value : bool ) -> Self {
8380 if value { Self :: Yes } else { Self :: No }
8481 }
8582}
8683
8784impl < ' mir , ' tcx > CompileTimeInterpreter < ' mir , ' tcx > {
8885 pub ( crate ) fn new (
89- can_access_statics : CanAccessStatics ,
86+ can_access_mut_global : CanAccessMutGlobal ,
9087 check_alignment : CheckAlignment ,
9188 ) -> Self {
9289 CompileTimeInterpreter {
9390 num_evaluated_steps : 0 ,
9491 stack : Vec :: new ( ) ,
95- can_access_statics ,
92+ can_access_mut_global ,
9693 check_alignment,
9794 }
9895 }
@@ -675,7 +672,7 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
675672 machine : & Self ,
676673 alloc_id : AllocId ,
677674 alloc : ConstAllocation < ' tcx > ,
678- static_def_id : Option < DefId > ,
675+ _static_def_id : Option < DefId > ,
679676 is_write : bool ,
680677 ) -> InterpResult < ' tcx > {
681678 let alloc = alloc. inner ( ) ;
@@ -687,22 +684,15 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for CompileTimeInterpreter<'mir,
687684 }
688685 } else {
689686 // Read access. These are usually allowed, with some exceptions.
690- if machine. can_access_statics == CanAccessStatics :: Yes {
687+ if machine. can_access_mut_global == CanAccessMutGlobal :: Yes {
691688 // Machine configuration allows us read from anything (e.g., `static` initializer).
692689 Ok ( ( ) )
693- } else if static_def_id. is_some ( ) {
694- // Machine configuration does not allow us to read statics
695- // (e.g., `const` initializer).
696- // See const_eval::machine::MemoryExtra::can_access_statics for why
697- // this check is so important: if we could read statics, we could read pointers
698- // to mutable allocations *inside* statics. These allocations are not themselves
699- // statics, so pointers to them can get around the check in `validity.rs`.
700- Err ( ConstEvalErrKind :: ConstAccessesStatic . into ( ) )
690+ } else if alloc. mutability == Mutability :: Mut {
691+ // Machine configuration does not allow us to read statics (e.g., `const`
692+ // initializer).
693+ Err ( ConstEvalErrKind :: ConstAccessesMutGlobal . into ( ) )
701694 } else {
702695 // Immutable global, this read is fine.
703- // But make sure we never accept a read from something mutable, that would be
704- // unsound. The reason is that as the content of this allocation may be different
705- // now and at run-time, so if we permit reading now we might return the wrong value.
706696 assert_eq ! ( alloc. mutability, Mutability :: Not ) ;
707697 Ok ( ( ) )
708698 }
0 commit comments