Skip to content

Commit f8e1e92

Browse files
committed
Auto merge of #84549 - tmiasko:static-initializer, r=varkor
Reachable statics have reachable initializers Static initializer can read other statics. Initializers are evaluated at compile time, and so their content could become inlined into another crate. Ensure that initializers of reachable statics are also reachable. Previously, when an item incorrectly considered to be unreachable was reached from another crate an attempt would be made to codegen it. The attempt could fail with an ICE (in the case MIR wasn't available to do so) in some circumstances the attempt could also succeed resulting in a local codegen of non-local items, including static ones. Fixes #84455.
2 parents 747a5d2 + eaddc8f commit f8e1e92

File tree

4 files changed

+26
-6
lines changed

4 files changed

+26
-6
lines changed

compiler/rustc_passes/src/reachable.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ impl<'tcx> ReachableContext<'tcx> {
250250
// Reachable constants will be inlined into other crates
251251
// unconditionally, so we need to make sure that their
252252
// contents are also reachable.
253-
hir::ItemKind::Const(_, init) => {
253+
hir::ItemKind::Const(_, init) | hir::ItemKind::Static(_, _, init) => {
254254
self.visit_nested_body(init);
255255
}
256256

@@ -261,7 +261,6 @@ impl<'tcx> ReachableContext<'tcx> {
261261
| hir::ItemKind::Use(..)
262262
| hir::ItemKind::OpaqueTy(..)
263263
| hir::ItemKind::TyAlias(..)
264-
| hir::ItemKind::Static(..)
265264
| hir::ItemKind::Mod(..)
266265
| hir::ItemKind::ForeignMod { .. }
267266
| hir::ItemKind::Impl { .. }

src/test/codegen/external-no-mangle-statics.rs

-4
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ const HIDDEN: () = {
5858
pub static mut L: u8 = 0;
5959
};
6060

61-
// The surrounding item should not accidentally become external
6261
fn x() {
6362
// CHECK: @M = local_unnamed_addr constant
6463
#[no_mangle]
@@ -76,6 +75,3 @@ fn x() {
7675
#[no_mangle]
7776
pub static mut P: u8 = 0;
7877
}
79-
// CHECK-LABEL: ; external_no_mangle_statics::x
80-
// CHECK-NEXT: ; Function Attrs:
81-
// CHECK-NEXT: define internal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
pub static V: &u32 = &X;
2+
pub static F: fn() = f;
3+
4+
static X: u32 = 42;
5+
6+
pub fn v() -> *const u32 {
7+
V
8+
}
9+
10+
fn f() {}
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// run-pass
2+
// aux-build:static_init_aux.rs
3+
extern crate static_init_aux as aux;
4+
5+
static V: &u32 = aux::V;
6+
static F: fn() = aux::F;
7+
8+
fn v() -> *const u32 {
9+
V
10+
}
11+
12+
fn main() {
13+
assert_eq!(aux::v(), crate::v());
14+
F();
15+
}

0 commit comments

Comments
 (0)