@@ -10,7 +10,6 @@ use std::num::NonZero;
1010use either:: { Left , Right } ;
1111
1212use hir:: def:: DefKind ;
13- use hir:: def_id:: DefId ;
1413use rustc_ast:: Mutability ;
1514use rustc_data_structures:: fx:: FxHashSet ;
1615use rustc_hir as hir;
@@ -450,8 +449,9 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
450449 // `!` is a ZST and we want to validate it.
451450 if let Ok ( ( alloc_id, _offset, _prov) ) = self . ecx . ptr_try_get_alloc_id ( place. ptr ( ) ) {
452451 let mut skip_recursive_check = false ;
453- let ( alloc_actual_mutbl, is_static) = mutability ( self . ecx , alloc_id) ;
454- if let Some ( ( did, nested) ) = is_static {
452+ let alloc_actual_mutbl = mutability ( self . ecx , alloc_id) ;
453+ if let GlobalAlloc :: Static ( did) = self . ecx . tcx . global_alloc ( alloc_id) {
454+ let DefKind :: Static { nested, .. } = self . ecx . tcx . def_kind ( did) else { bug ! ( ) } ;
455455 // Special handling for pointers to statics (irrespective of their type).
456456 assert ! ( !self . ecx. tcx. is_thread_local_static( did) ) ;
457457 assert ! ( self . ecx. tcx. is_static( did) ) ;
@@ -682,7 +682,7 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
682682 fn in_mutable_memory ( & self , op : & OpTy < ' tcx , M :: Provenance > ) -> bool {
683683 if let Some ( mplace) = op. as_mplace_or_imm ( ) . left ( ) {
684684 if let Some ( alloc_id) = mplace. ptr ( ) . provenance . and_then ( |p| p. get_alloc_id ( ) ) {
685- return mutability ( self . ecx , alloc_id) . 0 . is_mut ( ) ;
685+ return mutability ( self . ecx , alloc_id) . is_mut ( ) ;
686686 }
687687 }
688688 false
@@ -695,13 +695,19 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
695695fn mutability < ' mir , ' tcx : ' mir > (
696696 ecx : & InterpCx < ' mir , ' tcx , impl Machine < ' mir , ' tcx > > ,
697697 alloc_id : AllocId ,
698- ) -> ( Mutability , Option < ( DefId , bool ) > ) {
698+ ) -> Mutability {
699699 // Let's see what kind of memory this points to.
700700 // We're not using `try_global_alloc` since dangling pointers have already been handled.
701701 match ecx. tcx . global_alloc ( alloc_id) {
702702 GlobalAlloc :: Static ( did) => {
703703 let DefKind :: Static { mutability, nested } = ecx. tcx . def_kind ( did) else { bug ! ( ) } ;
704- let mutability = if nested {
704+ if nested {
705+ assert ! (
706+ ecx. memory. alloc_map. get( alloc_id) . is_none( ) ,
707+ "allocations of nested statics are already interned: {alloc_id:?}, {did:?}"
708+ ) ;
709+ // Nested statics in a `static` are never interior mutable,
710+ // so just use the declared mutability.
705711 mutability
706712 } else {
707713 let mutability = match mutability {
@@ -721,13 +727,12 @@ fn mutability<'mir, 'tcx: 'mir>(
721727 assert_eq ! ( alloc. mutability, mutability) ;
722728 }
723729 mutability
724- } ;
725- ( mutability, Some ( ( did, nested) ) )
730+ }
726731 }
727- GlobalAlloc :: Memory ( alloc) => ( alloc. inner ( ) . mutability , None ) ,
732+ GlobalAlloc :: Memory ( alloc) => alloc. inner ( ) . mutability ,
728733 GlobalAlloc :: Function ( ..) | GlobalAlloc :: VTable ( ..) => {
729734 // These are immutable, we better don't allow mutable pointers here.
730- ( Mutability :: Not , None )
735+ Mutability :: Not
731736 }
732737 }
733738}
0 commit comments