Skip to content

Commit

Permalink
Allow validation of complex nested statics
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Feb 26, 2024
1 parent 681b992 commit e5836ab
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 11 deletions.
29 changes: 18 additions & 11 deletions compiler/rustc_const_eval/src/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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(..) => {
Expand Down
19 changes: 19 additions & 0 deletions tests/ui/statics/nested_struct.rs
Original file line number Diff line number Diff line change
@@ -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() {}

0 comments on commit e5836ab

Please sign in to comment.