about summary refs log tree commit diff
diff options
context:
space:
mode:
authorTomasz Miąsko <tomasz.miasko@gmail.com>2023-08-22 00:00:00 +0000
committerTomasz Miąsko <tomasz.miasko@gmail.com>2023-08-22 00:00:00 +0000
commit0383131f7f48c7ff65b982c4c07dd8a7e6d5ed5e (patch)
treeffb8c7b0a53bfd20c9ed052b7e536a1c5426aa05
parent3e9e5745dfd3ad9a553a4c7d2b3d125dc1473fa6 (diff)
downloadrust-0383131f7f48c7ff65b982c4c07dd8a7e6d5ed5e.tar.gz
rust-0383131f7f48c7ff65b982c4c07dd8a7e6d5ed5e.zip
Contents of reachable statics is reachable
-rw-r--r--compiler/rustc_passes/src/reachable.rs10
-rw-r--r--tests/ui/cross-crate/auxiliary/static_init_aux.rs4
-rw-r--r--tests/ui/cross-crate/static-init.rs3
3 files changed, 10 insertions, 7 deletions
diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs
index f9d34ea71ba..e62833b358b 100644
--- a/compiler/rustc_passes/src/reachable.rs
+++ b/compiler/rustc_passes/src/reachable.rs
@@ -98,15 +98,11 @@ impl<'tcx> Visitor<'tcx> for ReachableContext<'tcx> {
                 self.worklist.push(def_id);
             } else {
                 match res {
-                    // If this path leads to a constant, then we need to
-                    // recurse into the constant to continue finding
-                    // items that are reachable.
-                    Res::Def(DefKind::Const | DefKind::AssocConst, _) => {
+                    // Reachable constants and reachable statics can have their contents inlined
+                    // into other crates. Mark them as reachable and recurse into their body.
+                    Res::Def(DefKind::Const | DefKind::AssocConst | DefKind::Static(_), _) => {
                         self.worklist.push(def_id);
                     }
-
-                    // If this wasn't a static, then the destination is
-                    // surely reachable.
                     _ => {
                         self.reachable_symbols.insert(def_id);
                     }
diff --git a/tests/ui/cross-crate/auxiliary/static_init_aux.rs b/tests/ui/cross-crate/auxiliary/static_init_aux.rs
index 3b664f43654..5e172ef3198 100644
--- a/tests/ui/cross-crate/auxiliary/static_init_aux.rs
+++ b/tests/ui/cross-crate/auxiliary/static_init_aux.rs
@@ -1,10 +1,14 @@
 pub static V: &u32 = &X;
 pub static F: fn() = f;
+pub static G: fn() = G0;
 
 static X: u32 = 42;
+static G0: fn() = g;
 
 pub fn v() -> *const u32 {
     V
 }
 
 fn f() {}
+
+fn g() {}
diff --git a/tests/ui/cross-crate/static-init.rs b/tests/ui/cross-crate/static-init.rs
index 2e893c5d9bf..0b50c41fc5e 100644
--- a/tests/ui/cross-crate/static-init.rs
+++ b/tests/ui/cross-crate/static-init.rs
@@ -1,9 +1,11 @@
+// Regression test for #84455 and #115052.
 // 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;
+static G: fn() = aux::G;
 
 fn v() -> *const u32 {
     V
@@ -12,4 +14,5 @@ fn v() -> *const u32 {
 fn main() {
     assert_eq!(aux::v(), crate::v());
     F();
+    G();
 }