Skip to content

Commit 2fca974

Browse files
authored
Unrolled build for #143482
Rollup merge of #143482 - ia0:fix, r=fee1-dead Fix short linker error output This PR does 2 things: - It removes the braces when there's a single element. This is required since brace expansion (at least in bash and zsh) only triggers if there's at least 2 elements. - It removes the extra `.rlib` suffixes of the elements. See #135707 (comment) for context. Running `cargo +stage1 build` on the following program: ```rust unsafe extern "C" { fn foo() -> libc::c_int; } fn main() { let x = unsafe { foo() } as u32; // println!("{}", data_encoding::BASE64.encode(&x.to_le_bytes())); } ``` Gives the following diff before and after the PR: ```diff -/tmp/foo/target/debug/deps/{liblibc-faf416f178830595.rlib}.rlib +/tmp/foo/target/debug/deps/liblibc-faf416f178830595.rlib ``` Running on the same program with the additional dependency, we get the following diff: ```diff -/tmp/foo/target/debug/deps/{liblibc-faf416f178830595.rlib,libdata_encoding-84bb5aadfa9e8839.rlib}.rlib +/tmp/foo/target/debug/deps/{liblibc-faf416f178830595,libdata_encoding-84bb5aadfa9e8839}.rlib ```
2 parents 25cf7d1 + cdbdd8a commit 2fca974

File tree

6 files changed

+36
-8
lines changed

6 files changed

+36
-8
lines changed

compiler/rustc_codegen_ssa/src/errors.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
457457
} else if arg.as_encoded_bytes().ends_with(b".rlib") {
458458
let rlib_path = Path::new(&arg);
459459
let dir = rlib_path.parent().unwrap();
460-
let filename = rlib_path.file_name().unwrap().to_owned();
460+
let filename = rlib_path.file_stem().unwrap().to_owned();
461461
if let Some(ArgGroup::Rlibs(parent, rlibs)) = args.last_mut() {
462462
if parent == dir {
463463
rlibs.push(filename);
@@ -471,7 +471,7 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
471471
args.push(ArgGroup::Regular(arg));
472472
}
473473
}
474-
let crate_hash = regex::bytes::Regex::new(r"-[0-9a-f]+\.rlib$").unwrap();
474+
let crate_hash = regex::bytes::Regex::new(r"-[0-9a-f]+").unwrap();
475475
self.command.args(args.into_iter().map(|arg_group| {
476476
match arg_group {
477477
// SAFETY: we are only matching on ASCII, not any surrogate pairs, so any replacements we do will still be valid.
@@ -494,7 +494,11 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
494494
Err(_) => false,
495495
};
496496
let mut arg = dir.into_os_string();
497-
arg.push("/{");
497+
arg.push("/");
498+
let needs_braces = rlibs.len() >= 2;
499+
if needs_braces {
500+
arg.push("{");
501+
}
498502
let mut first = true;
499503
for mut rlib in rlibs {
500504
if !first {
@@ -513,7 +517,10 @@ impl<G: EmissionGuarantee> Diagnostic<'_, G> for LinkingFailed<'_> {
513517
}
514518
arg.push(rlib);
515519
}
516-
arg.push("}.rlib");
520+
if needs_braces {
521+
arg.push("}");
522+
}
523+
arg.push(".rlib");
517524
arg
518525
}
519526
}

tests/run-make/linker-warning/bar.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[repr(C)]
2+
pub struct Bar(u32);

tests/run-make/linker-warning/foo.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#[repr(C)]
2+
pub struct Foo(u32);

tests/run-make/linker-warning/main.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,10 @@
1-
fn main() {}
1+
unsafe extern "C" {
2+
#[cfg(only_foo)]
3+
fn does_not_exist(p: *const u8) -> *const foo::Foo;
4+
#[cfg(not(only_foo))]
5+
fn does_not_exist(p: *const bar::Bar) -> *const foo::Foo;
6+
}
7+
8+
fn main() {
9+
let _ = unsafe { does_not_exist(core::ptr::null()) };
10+
}

tests/run-make/linker-warning/rmake.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ fn run_rustc() -> Rustc {
1111
.arg("-Clink-self-contained=-linker")
1212
.arg("-Zunstable-options")
1313
.arg("-Wlinker-messages")
14+
.args(["--extern", "foo", "--extern", "bar"])
1415
.output("main")
1516
.linker("./fake-linker");
1617
if run_make_support::target() == "x86_64-unknown-linux-gnu" {
@@ -21,8 +22,10 @@ fn run_rustc() -> Rustc {
2122
}
2223

2324
fn main() {
24-
// first, compile our linker
25+
// first, compile our linker and our dependencies
2526
rustc().arg("fake-linker.rs").output("fake-linker").run();
27+
rustc().arg("foo.rs").crate_type("rlib").run();
28+
rustc().arg("bar.rs").crate_type("rlib").run();
2629

2730
// Run rustc with our fake linker, and make sure it shows warnings
2831
let warnings = run_rustc().link_arg("run_make_warn").run();
@@ -48,7 +51,8 @@ fn main() {
4851
let out = run_rustc().link_arg("run_make_error").run_fail();
4952
out.assert_stderr_contains("fake-linker")
5053
.assert_stderr_contains("object files omitted")
51-
.assert_stderr_contains_regex(r"\{")
54+
.assert_stderr_contains("/{libfoo,libbar}.rlib\"")
55+
.assert_stderr_contains("-*}.rlib\"")
5256
.assert_stderr_not_contains(r".rcgu.o")
5357
.assert_stderr_not_contains_regex(r"lib(/|\\\\)libstd");
5458

@@ -68,6 +72,10 @@ fn main() {
6872
.run();
6973
}
7074

75+
// Make sure a single dependency doesn't use brace expansion.
76+
let out1 = run_rustc().cfg("only_foo").link_arg("run_make_error").run_fail();
77+
out1.assert_stderr_contains("fake-linker").assert_stderr_contains("/libfoo.rlib\"");
78+
7179
// Make sure we show linker warnings even across `-Z no-link`
7280
rustc()
7381
.arg("-Zno-link")

tests/run-make/linker-warning/short-error.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
error: linking with `./fake-linker` failed: exit status: 1
22
|
3-
= note: "./fake-linker" "-m64" "/symbols.o" "<2 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,libcfg_if-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/raw-dylibs" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/build-root/test/run-make/linker-warning/rmake_out" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "main" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "run_make_error"
3+
= note: "./fake-linker" "-m64" "/symbols.o" "<2 object files omitted>" "-Wl,--as-needed" "-Wl,-Bstatic" "/build-root/test/run-make/linker-warning/rmake_out/{libfoo,libbar}.rlib" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib/{libstd-*,libpanic_unwind-*,libobject-*,libmemchr-*,libaddr2line-*,libgimli-*,librustc_demangle-*,libstd_detect-*,libhashbrown-*,librustc_std_workspace_alloc-*,libminiz_oxide-*,libadler2-*,libunwind-*,libcfg_if-*,liblibc-*,librustc_std_workspace_core-*,liballoc-*,libcore-*,libcompiler_builtins-*}.rlib" "-Wl,-Bdynamic" "-lgcc_s" "-lutil" "-lrt" "-lpthread" "-lm" "-ldl" "-lc" "-L" "/raw-dylibs" "-Wl,--eh-frame-hdr" "-Wl,-z,noexecstack" "-L" "/build-root/test/run-make/linker-warning/rmake_out" "-L" "<sysroot>/lib/rustlib/x86_64-unknown-linux-gnu/lib" "-o" "main" "-Wl,--gc-sections" "-pie" "-Wl,-z,relro,-z,now" "-nodefaultlibs" "run_make_error"
44
= note: some arguments are omitted. use `--verbose` to show all linker arguments
55
= note: error: baz
66

0 commit comments

Comments
 (0)