@@ -404,19 +404,27 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
404404 // Skip validation entirely for some external statics
405405 let alloc_kind = self . ecx . tcx . alloc_map . lock ( ) . get ( ptr. alloc_id ) ;
406406 if let Some ( GlobalAlloc :: Static ( did) ) = alloc_kind {
407- // `extern static` cannot be validated as they have no body.
408- // FIXME: Statics from other crates are also skipped.
409- // They might be checked at a different type, but for now we
410- // want to avoid recursing too deeply. This is not sound!
411- if !did. is_local ( ) || self . ecx . tcx . is_foreign_item ( did) {
412- return Ok ( ( ) ) ;
413- }
407+ // See const_eval::machine::MemoryExtra::can_access_statics for why
408+ // this check is so important.
409+ // This check is reachable when the const just referenced the static,
410+ // but never read it (so we never entered `before_access_global`).
411+ // We also need to do it here instead of going on to avoid running
412+ // into the `before_access_global` check during validation.
414413 if !self . may_ref_to_static && self . ecx . tcx . is_static ( did) {
415414 throw_validation_failure ! (
416415 format_args!( "a {} pointing to a static variable" , kind) ,
417416 self . path
418417 ) ;
419418 }
419+ // `extern static` cannot be validated as they have no body.
420+ // FIXME: Statics from other crates are also skipped.
421+ // They might be checked at a different type, but for now we
422+ // want to avoid recursing too deeply. We might miss const-invalid data,
423+ // but things are still sound otherwise (in particular re: consts
424+ // referring to statics).
425+ if !did. is_local ( ) || self . ecx . tcx . is_foreign_item ( did) {
426+ return Ok ( ( ) ) ;
427+ }
420428 }
421429 }
422430 // Proceed recursively even for ZST, no reason to skip them!
0 commit comments