Skip to content

Commit 73d96b0

Browse files
committed
Auto merge of #88032 - hyd-dev:no-mangle-method, r=petrochenkov
Fix `reachable_set` for non-function items in non-library crates I unintentionally changed `reachable_set` to ignore non-function items when `!self.any_library` in #86492, which can lead to "undefined reference" errors in non-library (`cdylib`/`staticlib`/`bin`) crates, for example: https://play.rust-lang.org/?version=nightly&mode=debug&edition=2018&gist=6bb2c5065a9be7e40943d0541e161b5a This PR restores the behavior of `reachable_set` for non-function items. Fixes #88016. <details> <summary>The modified test will fail with this output without the `reachable_set` change</summary> ``` ---- [codegen] codegen/external-no-mangle-statics.rs#staticlib stdout ---- error in revision `staticlib`: verification with 'FileCheck' failed status: exit status: 1 command: "/checkout/build/x86_64-unknown-linux-gnu/ci-llvm/bin/FileCheck" "--input-file" "/checkout/build/x86_64-unknown-linux-gnu/test/codegen/external-no-mangle-statics.staticlib/external-no-mangle-statics.ll" "/checkout/src/test/codegen/external-no-mangle-statics.rs" "--check-prefixes" "CHECK,NONMSVC,staticlib" stdout: ------------------------------------------ ------------------------------------------ stderr: ------------------------------------------ /checkout/src/test/codegen/external-no-mangle-statics.rs:10:11: error: CHECK: expected string not found in input // CHECK: `@A` = local_unnamed_addr constant ^ /checkout/build/x86_64-unknown-linux-gnu/test/codegen/external-no-mangle-statics.staticlib/external-no-mangle-statics.ll:1:1: note: scanning from here ; ModuleID = 'external_no_mangle_statics.b50529d3-cgu.0' ^ /checkout/build/x86_64-unknown-linux-gnu/test/codegen/external-no-mangle-statics.staticlib/external-no-mangle-statics.ll:1:6: note: possible intended match here ; ModuleID = 'external_no_mangle_statics.b50529d3-cgu.0' ^ Input file: /checkout/build/x86_64-unknown-linux-gnu/test/codegen/external-no-mangle-statics.staticlib/external-no-mangle-statics.ll Check file: /checkout/src/test/codegen/external-no-mangle-statics.rs -dump-input=help explains the following input dump. Input was: <<<<<< 1: ; ModuleID = 'external_no_mangle_statics.b50529d3-cgu.0' check:10'0 X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found check:10'1 ? possible intended match 2: source_filename = "external_no_mangle_statics.b50529d3-cgu.0" check:10'0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 3: target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" check:10'0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4: target triple = "x86_64-unknown-linux-gnu" check:10'0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 5: check:10'0 ~ 6: !llvm.module.flags = !{!0, !1} check:10'0 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ . . . >>>>>> ------------------------------------------ failures: [codegen] codegen/external-no-mangle-statics.rs#staticlib ``` </details>
2 parents 92f3753 + 29b73ee commit 73d96b0

File tree

2 files changed

+19
-17
lines changed

2 files changed

+19
-17
lines changed

Diff for: compiler/rustc_passes/src/reachable.rs

+16-15
Original file line numberDiff line numberDiff line change
@@ -211,21 +211,22 @@ impl<'tcx> ReachableContext<'tcx> {
211211
if !self.any_library {
212212
// If we are building an executable, only explicitly extern
213213
// types need to be exported.
214-
if let Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), def_id, .. })
215-
| Node::ImplItem(hir::ImplItem {
216-
kind: hir::ImplItemKind::Fn(sig, ..),
217-
def_id,
218-
..
219-
}) = *node
220-
{
221-
let reachable = sig.header.abi != Abi::Rust;
222-
let codegen_attrs = self.tcx.codegen_fn_attrs(*def_id);
223-
let is_extern = codegen_attrs.contains_extern_indicator();
224-
let std_internal =
225-
codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
226-
if reachable || is_extern || std_internal {
227-
self.reachable_symbols.insert(search_item);
228-
}
214+
let reachable =
215+
if let Node::Item(hir::Item { kind: hir::ItemKind::Fn(sig, ..), .. })
216+
| Node::ImplItem(hir::ImplItem {
217+
kind: hir::ImplItemKind::Fn(sig, ..), ..
218+
}) = *node
219+
{
220+
sig.header.abi != Abi::Rust
221+
} else {
222+
false
223+
};
224+
let codegen_attrs = self.tcx.codegen_fn_attrs(search_item);
225+
let is_extern = codegen_attrs.contains_extern_indicator();
226+
let std_internal =
227+
codegen_attrs.flags.contains(CodegenFnAttrFlags::RUSTC_STD_INTERNAL_SYMBOL);
228+
if reachable || is_extern || std_internal {
229+
self.reachable_symbols.insert(search_item);
229230
}
230231
} else {
231232
// If we are building a library, then reachable symbols will

Diff for: src/test/codegen/external-no-mangle-statics.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1+
// revisions: lib staticlib
12
// ignore-emscripten default visibility is hidden
23
// compile-flags: -O
34
// `#[no_mangle]`d static variables always have external linkage, i.e., no `internal` in their
45
// definitions
56

6-
#![crate_type = "lib"]
7-
#![no_std]
7+
#![cfg_attr(lib, crate_type = "lib")]
8+
#![cfg_attr(staticlib, crate_type = "staticlib")]
89

910
// CHECK: @A = local_unnamed_addr constant
1011
#[no_mangle]

0 commit comments

Comments
 (0)