Skip to content

Commit 133d5bd

Browse files
committed
Allow validation of complex nested statics
1 parent 47e859c commit 133d5bd

File tree

2 files changed

+37
-11
lines changed

2 files changed

+37
-11
lines changed

compiler/rustc_const_eval/src/interpret/validity.rs

+18-11
Original file line numberDiff line numberDiff line change
@@ -465,16 +465,6 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
465465
// Special handling for pointers to statics (irrespective of their type).
466466
assert!(!self.ecx.tcx.is_thread_local_static(did));
467467
assert!(self.ecx.tcx.is_static(did));
468-
let is_mut = matches!(
469-
self.ecx.tcx.def_kind(did),
470-
DefKind::Static { mt: Mutability::Mut, .. }
471-
) || !self
472-
.ecx
473-
.tcx
474-
.type_of(did)
475-
.no_bound_vars()
476-
.expect("statics should not have generic parameters")
477-
.is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all());
478468
// Mode-specific checks
479469
match self.ctfe_mode {
480470
Some(
@@ -500,7 +490,24 @@ impl<'rt, 'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValidityVisitor<'rt, 'mir, '
500490
None => {}
501491
}
502492
// Return alloc mutability
503-
if is_mut { Mutability::Mut } else { Mutability::Not }
493+
if let DefKind::Static { mt, nested } = self.ecx.tcx.def_kind(did) {
494+
if nested {
495+
mt
496+
} else if self
497+
.ecx
498+
.tcx
499+
.type_of(did)
500+
.no_bound_vars()
501+
.expect("statics should not have generic parameters")
502+
.is_freeze(*self.ecx.tcx, ty::ParamEnv::reveal_all())
503+
{
504+
Mutability::Not
505+
} else {
506+
mt
507+
}
508+
} else {
509+
Mutability::Not
510+
}
504511
}
505512
GlobalAlloc::Memory(alloc) => alloc.inner().mutability,
506513
GlobalAlloc::Function(..) | GlobalAlloc::VTable(..) => {

tests/ui/statics/nested_struct.rs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
//@ check-pass
2+
3+
pub struct Lint {
4+
pub name: &'static str,
5+
pub desc: &'static str,
6+
pub report_in_external_macro: bool,
7+
pub is_loaded: bool,
8+
pub crate_level_only: bool,
9+
}
10+
11+
static FOO: &Lint = &Lint {
12+
name: &"foo",
13+
desc: "desc",
14+
report_in_external_macro: false,
15+
is_loaded: true,
16+
crate_level_only: false,
17+
};
18+
19+
fn main() {}

0 commit comments

Comments
 (0)