Skip to content

Commit c770681

Browse files
authored
Rollup merge of #149772 - dianqk:no-builtins-linker-plugin-lto, r=jieyouxu
test: Add a test for 146133 This is a test without using `-Zbuild-std` for #146133. There is another test using `-Zbuild-std`: rust-lang/cargo#16277. Even if a crate is marked as `#![no_builtins]`, we can still generate bitcode for rlib, but we cannot emit bitcode to the linker in rustc's LTO.
2 parents 7c9c7ed + 611c956 commit c770681

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
no_builtins::foo();
3+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#![no_builtins]
2+
3+
#[inline(never)]
4+
pub fn foo() {}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
//@ only-x86_64-unknown-linux-gnu
2+
3+
use std::fs;
4+
use std::path::Path;
5+
6+
use run_make_support::{cwd, has_extension, llvm_ar, llvm_bcanalyzer, rust_lib_name, rustc};
7+
8+
// A regression test for #146133.
9+
10+
fn main() {
11+
// Compile a `#![no_builtins]` rlib crate with `-Clinker-plugin-lto`.
12+
// It is acceptable to generate bitcode for rlib, so there is no need to check something.
13+
rustc().input("no_builtins.rs").crate_type("rlib").linker_plugin_lto("on").run();
14+
15+
// Checks that rustc's LTO doesn't emit any bitcode to the linker.
16+
let stdout = rustc()
17+
.input("main.rs")
18+
.extern_("no_builtins", rust_lib_name("no_builtins"))
19+
.lto("thin")
20+
.print("link-args")
21+
.arg("-Csave-temps")
22+
.arg("-Clinker-features=-lld")
23+
.run()
24+
.stdout_utf8();
25+
for object in stdout
26+
.split_whitespace()
27+
.map(|s| s.trim_matches('"'))
28+
.filter(|path| has_extension(path, "rlib") || has_extension(path, "o"))
29+
{
30+
let object_path = if !fs::exists(object).unwrap() {
31+
cwd().join(object)
32+
} else {
33+
Path::new(object).to_path_buf()
34+
};
35+
if has_extension(object, "rlib") {
36+
let ar_stdout = llvm_ar().arg("t").arg(&object_path).run().stdout_utf8();
37+
llvm_ar().extract().arg(&object_path).run();
38+
for object in ar_stdout.split_whitespace().filter(|o| has_extension(o, "o")) {
39+
let object_path = cwd().join(object);
40+
not_bitcode(&object_path);
41+
}
42+
} else {
43+
not_bitcode(&object_path);
44+
}
45+
}
46+
}
47+
48+
fn not_bitcode(object: &Path) {
49+
llvm_bcanalyzer()
50+
.input(object)
51+
.run_fail()
52+
.assert_stderr_contains("llvm-bcanalyzer: Invalid record at top-level");
53+
}

0 commit comments

Comments
 (0)