Skip to content

Reachable statics have reachable initializers #84549

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions compiler/rustc_passes/src/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ impl<'tcx> ReachableContext<'tcx> {
// Reachable constants will be inlined into other crates
// unconditionally, so we need to make sure that their
// contents are also reachable.
hir::ItemKind::Const(_, init) => {
hir::ItemKind::Const(_, init) | hir::ItemKind::Static(_, _, init) => {
self.visit_nested_body(init);
}

Expand All @@ -261,7 +261,6 @@ impl<'tcx> ReachableContext<'tcx> {
| hir::ItemKind::Use(..)
| hir::ItemKind::OpaqueTy(..)
| hir::ItemKind::TyAlias(..)
| hir::ItemKind::Static(..)
| hir::ItemKind::Mod(..)
| hir::ItemKind::ForeignMod { .. }
| hir::ItemKind::Impl { .. }
Expand Down
4 changes: 0 additions & 4 deletions src/test/codegen/external-no-mangle-statics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ const HIDDEN: () = {
pub static mut L: u8 = 0;
};

// The surrounding item should not accidentally become external
fn x() {
// CHECK: @M = local_unnamed_addr constant
#[no_mangle]
Expand All @@ -76,6 +75,3 @@ fn x() {
#[no_mangle]
pub static mut P: u8 = 0;
}
// CHECK-LABEL: ; external_no_mangle_statics::x
// CHECK-NEXT: ; Function Attrs:
// CHECK-NEXT: define internal
10 changes: 10 additions & 0 deletions src/test/ui/cross-crate/auxiliary/static_init_aux.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
pub static V: &u32 = &X;
pub static F: fn() = f;

static X: u32 = 42;

pub fn v() -> *const u32 {
V
}

fn f() {}
15 changes: 15 additions & 0 deletions src/test/ui/cross-crate/static-init.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// run-pass
// aux-build:static_init_aux.rs
extern crate static_init_aux as aux;

static V: &u32 = aux::V;
static F: fn() = aux::F;

fn v() -> *const u32 {
V
}

fn main() {
assert_eq!(aux::v(), crate::v());
F();
}