about summary refs log tree commit diff
path: root/compiler
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2021-08-16 09:38:18 +0000
committerbors <bors@rust-lang.org>2021-08-16 09:38:18 +0000
commit73d96b090bb68065cd3a469b27cbd568e39bf0e7 (patch)
treef066443002bd89843a317e250065a7e0e2c8a2b4 /compiler
parent92f3753b073c03184118a315cc0d289116102ae1 (diff)
parent29b73ee5fa04e4ccbd1468e50fb88470b03d4d27 (diff)
downloadrust-73d96b090bb68065cd3a469b27cbd568e39bf0e7.tar.gz
rust-73d96b090bb68065cd3a469b27cbd568e39bf0e7.zip
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 https://github.com/rust-lang/rust/pull/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 https://github.com/rust-lang/rust/issues/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>
Diffstat (limited to 'compiler')
-rw-r--r--compiler/rustc_passes/src/reachable.rs31
1 files changed, 16 insertions, 15 deletions
diff --git a/compiler/rustc_passes/src/reachable.rs b/compiler/rustc_passes/src/reachable.rs
index 963153a01a0..5ca098c2287 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