@@ -129,17 +129,20 @@ pub enum PathElem {
129129pub enum CtfeValidationMode {
130130 /// Validation of a `static`
131131 Static { mutbl : Mutability } ,
132- /// Validation of a `const` (including promoteds).
132+ /// Validation of a promoted.
133+ Promoted ,
134+ /// Validation of a `const`.
133135 /// `allow_immutable_unsafe_cell` says whether we allow `UnsafeCell` in immutable memory (which is the
134136 /// case for the top-level allocation of a `const`, where this is fine because the allocation will be
135137 /// copied at each use site).
136- Const { allow_immutable_unsafe_cell : bool , allow_extern_static_ptrs : bool } ,
138+ Const { allow_immutable_unsafe_cell : bool } ,
137139}
138140
139141impl CtfeValidationMode {
140142 fn allow_immutable_unsafe_cell ( self ) -> bool {
141143 match self {
142144 CtfeValidationMode :: Static { .. } => false ,
145+ CtfeValidationMode :: Promoted { .. } => false ,
143146 CtfeValidationMode :: Const { allow_immutable_unsafe_cell, .. } => {
144147 allow_immutable_unsafe_cell
145148 }
@@ -149,6 +152,7 @@ impl CtfeValidationMode {
149152 fn may_contain_mutable_ref ( self ) -> bool {
150153 match self {
151154 CtfeValidationMode :: Static { mutbl } => mutbl == Mutability :: Mut ,
155+ CtfeValidationMode :: Promoted { .. } => false ,
152156 CtfeValidationMode :: Const { .. } => false ,
153157 }
154158 }
@@ -476,34 +480,32 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
476480 throw_validation_failure ! ( self . path, MutableRefToImmutable ) ;
477481 }
478482 }
483+ // Mode-specific checks
479484 match self . ctfe_mode {
480- Some ( CtfeValidationMode :: Static { .. } ) => {
485+ Some (
486+ CtfeValidationMode :: Static { .. }
487+ | CtfeValidationMode :: Promoted { .. } ,
488+ ) => {
481489 // We skip recursively checking other statics. These statics must be sound by
482490 // themselves, and the only way to get broken statics here is by using
483491 // unsafe code.
484492 // The reasons we don't check other statics is twofold. For one, in all
485493 // sound cases, the static was already validated on its own, and second, we
486494 // trigger cycle errors if we try to compute the value of the other static
487- // and that static refers back to us.
495+ // and that static refers back to us (potentially through a promoted) .
488496 // This could miss some UB, but that's fine.
489497 return Ok ( ( ) ) ;
490498 }
491- Some ( CtfeValidationMode :: Const {
492- allow_extern_static_ptrs, ..
493- } ) => {
499+ Some ( CtfeValidationMode :: Const { .. } ) => {
494500 // For consts on the other hand we have to recursively check;
495501 // pattern matching assumes a valid value. However we better make
496502 // sure this is not mutable.
497503 if is_mut {
498504 throw_validation_failure ! ( self . path, ConstRefToMutable ) ;
499505 }
506+ // We can't recursively validate `extern static`, so we better reject them.
500507 if self . ecx . tcx . is_foreign_item ( did) {
501- if !allow_extern_static_ptrs {
502- throw_validation_failure ! ( self . path, ConstRefToExtern ) ;
503- } else {
504- // We can't validate this...
505- return Ok ( ( ) ) ;
506- }
508+ throw_validation_failure ! ( self . path, ConstRefToExtern ) ;
507509 }
508510 }
509511 None => { }
0 commit comments