From 224cb3f638881525c00c46b012721f0aafc73263 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Fri, 28 Jun 2024 20:59:01 +0000 Subject: [PATCH 1/2] Revert "Rollup merge of #126938 - RalfJung:link_section, r=compiler-errors" This reverts commit 5c4ede88c61e746ed5c852d7a7e38ab1a824ae52, reversing changes made to 95332b89187bb6a0c910574cfeff1933b619565a. --- compiler/rustc_passes/src/reachable.rs | 12 ++++++++++-- .../miri/tests/pass/tls/win_tls_callback.rs | 16 ---------------- .../miri/tests/pass/tls/win_tls_callback.stderr | 1 - 3 files changed, 10 insertions(+), 19 deletions(-) delete mode 100644 src/tools/miri/tests/pass/tls/win_tls_callback.rs delete mode 100644 src/tools/miri/tests/pass/tls/win_tls_callback.stderr diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index da4435ebebe87..6dd8eaf7e6734 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -30,7 +30,7 @@ use rustc_hir::def_id::{DefId, LocalDefId}; use rustc_hir::intravisit::{self, Visitor}; use rustc_hir::Node; use rustc_middle::bug; -use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags; +use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; use rustc_middle::middle::privacy::{self, Level}; use rustc_middle::mir::interpret::{ConstAllocation, ErrorHandled, GlobalAlloc}; use rustc_middle::query::Providers; @@ -178,7 +178,15 @@ impl<'tcx> ReachableContext<'tcx> { if !self.any_library { // If we are building an executable, only explicitly extern // types need to be exported. - if has_custom_linkage(self.tcx, search_item) { + let codegen_attrs = if self.tcx.def_kind(search_item).has_codegen_attrs() { + self.tcx.codegen_fn_attrs(search_item) + } else { + CodegenFnAttrs::EMPTY + }; + let is_extern = codegen_attrs.contains_extern_indicator(); + let std_internal = + codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL); + if is_extern || std_internal { self.reachable_symbols.insert(search_item); } } else { diff --git a/src/tools/miri/tests/pass/tls/win_tls_callback.rs b/src/tools/miri/tests/pass/tls/win_tls_callback.rs deleted file mode 100644 index 99a8de29e9170..0000000000000 --- a/src/tools/miri/tests/pass/tls/win_tls_callback.rs +++ /dev/null @@ -1,16 +0,0 @@ -//! Ensure that we call Windows TLS callbacks in the local crate. -//@only-target-windows -// Calling eprintln in the callback seems to (re-)initialize some thread-local storage -// and then leak the memory allocated for that. Let's just ignore these leaks, -// that's not what this test is about. -//@compile-flags: -Zmiri-ignore-leaks - -#[link_section = ".CRT$XLB"] -#[used] // Miri only considers explicitly `#[used]` statics for `lookup_link_section` -pub static CALLBACK: unsafe extern "system" fn(*const (), u32, *const ()) = tls_callback; - -unsafe extern "system" fn tls_callback(_h: *const (), _dw_reason: u32, _pv: *const ()) { - eprintln!("in tls_callback"); -} - -fn main() {} diff --git a/src/tools/miri/tests/pass/tls/win_tls_callback.stderr b/src/tools/miri/tests/pass/tls/win_tls_callback.stderr deleted file mode 100644 index 8479558954456..0000000000000 --- a/src/tools/miri/tests/pass/tls/win_tls_callback.stderr +++ /dev/null @@ -1 +0,0 @@ -in tls_callback From 57931e50409f9365791f7923a68f9ae1ec301c4c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?R=C3=A9my=20Rakic?= Date: Fri, 28 Jun 2024 20:59:33 +0000 Subject: [PATCH 2/2] add non-regression test for issue 127052 --- .../unreferenced-used-static-issue-127052.rs | 9 +++++++++ 1 file changed, 9 insertions(+) create mode 100644 tests/ui/linkage-attr/unreferenced-used-static-issue-127052.rs diff --git a/tests/ui/linkage-attr/unreferenced-used-static-issue-127052.rs b/tests/ui/linkage-attr/unreferenced-used-static-issue-127052.rs new file mode 100644 index 0000000000000..aa8236b74315c --- /dev/null +++ b/tests/ui/linkage-attr/unreferenced-used-static-issue-127052.rs @@ -0,0 +1,9 @@ +// This is a non-regression test for issue #127052 where unreferenced `#[used]` statics couldn't be +// removed by the MSVC linker, causing linking errors. + +//@ build-pass: needs linking +//@ only-msvc + +#[used] +static FOO: u32 = 0; +fn main() {}