Skip to content

Commit

Permalink
Rollup merge of rust-lang#88032 - hyd-dev:no-mangle-method, r=petroch…
Browse files Browse the repository at this point in the history
…enkov

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 rust-lang#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 rust-lang#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>
  • Loading branch information
camsteffen authored Aug 15, 2021
2 parents 0666b7c + 29b73ee commit 7859dbd
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
31 changes: 16 additions & 15 deletions compiler/rustc_passes/src/reachable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/test/codegen/external-no-mangle-statics.rs
Original file line number Diff line number Diff line change
@@ -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]
Expand Down

0 comments on commit 7859dbd

Please sign in to comment.