From 29b73ee5fa04e4ccbd1468e50fb88470b03d4d27 Mon Sep 17 00:00:00 2001 From: hyd-dev Date: Sat, 14 Aug 2021 23:24:33 +0800 Subject: [PATCH] Fix `reachable_set` for non-function items in non-library crates --- compiler/rustc_passes/src/reachable.rs | 31 ++++++++++--------- .../codegen/external-no-mangle-statics.rs | 5 +-- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs index 963153a01a080..5ca098c22878b 100644 --- a/compiler/rustc_passes/src/reachable.rs +++ b/compiler/rustc_passes/src/reachable.rs @@ -211,21 +211,22 @@ impl<'tcx> ReachableContext<'tcx> { if !self.any_library { // If we are building an executable, only explicitly extern // types need to be exported. - if let Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), def_id, .. }) - | Node::ImplItem(hir::ImplItem { - kind: hir::ImplItemKind::Fn(sig, ..), - def_id, - .. - }) = *node - { - let reachable = sig.header.abi != Abi::Rust; - let codegen_attrs = self.tcx.codegen_fn_attrs(*def_id); - let is_extern = codegen_attrs.contains_extern_indicator(); - let std_internal = - codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL); - if reachable || is_extern || std_internal { - self.reachable_symbols.insert(search_item); - } + let reachable = + if let Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. }) + | Node::ImplItem(hir::ImplItem { + kind: hir::ImplItemKind::Fn(sig, ..), .. + }) = *node + { + sig.header.abi != Abi::Rust + } else { + false + }; + let codegen_attrs = self.tcx.codegen_fn_attrs(search_item); + let is_extern = codegen_attrs.contains_extern_indicator(); + let std_internal = + codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL); + if reachable || is_extern || std_internal { + self.reachable_symbols.insert(search_item); } } else { // If we are building a library, then reachable symbols will diff --git a/src/test/codegen/external-no-mangle-statics.rs b/src/test/codegen/external-no-mangle-statics.rs index feb4af6286eea..6274434cd8fcc 100644 --- a/src/test/codegen/external-no-mangle-statics.rs +++ b/src/test/codegen/external-no-mangle-statics.rs @@ -1,10 +1,11 @@ +// revisions: lib staticlib // ignore-emscripten default visibility is hidden // compile-flags: -O // `#[no_mangle]`d static variables always have external linkage, i.e., no `internal` in their // definitions -#![crate_type = "lib"] -#![no_std] +#![cfg_attr(lib, crate_type = "lib")] +#![cfg_attr(staticlib, crate_type = "staticlib")] // CHECK: @A = local_unnamed_addr constant #[no_mangle]