From e5836ab3dbbbfb95d15ec6452eaec24fe6e80e61 Mon Sep 17 00:00:00 2001 From: Oli Scherer Date: Mon, 26 Feb 2024 20:23:48 +0000 Subject: [PATCH] Allow validation of complex nested statics --- .../src/interpret/validity.rs | 29 ++++++++++++------- tests/ui/statics/nested_struct.rs | 19 ++++++++++++ 2 files changed, 37 insertions(+), 11 deletions(-) create mode 100644 tests/ui/statics/nested_struct.rs diff --git a/compiler/rustc_const_eval/src/interpret/validity.rs b/compiler/rustc_const_eval/src/interpret/validity.rs index 25f2450edab7f..27ee5227def3a 100644 --- a/compiler/rustc_const_eval/src/interpret/validity.rs +++ b/compiler/rustc_const_eval/src/interpret/validity.rs @@ -465,16 +465,6 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' // Special handling for pointers to statics (irrespective of their type). assert!(!self.ecx.tcx.is_thread_local_static(did)); assert!(self.ecx.tcx.is_static(did)); - let is_mut = matches!( - self.ecx.tcx.def_kind(did), - DefKind::Static { mt: Mutability::Mut, .. } - ) || !self - .ecx - .tcx - .type_of(did) - .no_bound_vars() - .expect("statics should not have generic parameters") - .is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all()); // Mode-specific checks match self.ctfe_mode { Some( @@ -500,7 +490,24 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, ' None => {} } // Return alloc mutability - if is_mut { Mutability::Mut } else { Mutability::Not } + if let DefKind::Static { mt, nested } = self.ecx.tcx.def_kind(did) { + if nested { + mt + } else if self + .ecx + .tcx + .type_of(did) + .no_bound_vars() + .expect("statics should not have generic parameters") + .is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all()) + { + Mutability::Not + } else { + mt + } + } else { + Mutability::Not + } } GlobalAlloc::Memory(alloc) => alloc.inner().mutability, GlobalAlloc::Function(..) | GlobalAlloc::VTable(..) => { diff --git a/tests/ui/statics/nested_struct.rs b/tests/ui/statics/nested_struct.rs new file mode 100644 index 0000000000000..06ee5ffcdf975 --- /dev/null +++ b/tests/ui/statics/nested_struct.rs @@ -0,0 +1,19 @@ +//@ check-pass + +pub struct Lint { + pub name: &'static str, + pub desc: &'static str, + pub report_in_external_macro: bool, + pub is_loaded: bool, + pub crate_level_only: bool, +} + +static FOO: &Lint = &Lint { + name: &"foo", + desc: "desc", + report_in_external_macro: false, + is_loaded: true, + crate_level_only: false, +}; + +fn main() {}