Skip to content

Commit 14c6f00

Browse files
authored
test(build-std): Add test for LTO (rust-lang#16277)
Add a test for rust-lang/rust#146133. `cargo +nightly-2025-08-29 test --test build-std -- lto` can reproduce the regression. This is not a bug from Cargo, but it requires `-Zbuild-std` in most use cases. The test case is from rust-lang/rust#146109. The point is that when rustc is invoked with -Clto=fat or -Clto=thin, it should perform LTO with ALL bitcodes. However, rust-lang/rust#145368 emits bitcodes for compiler_builtins but excludes it from LTO participation. As a result, the compiler_builtins bitcodes library is passed to the linker, but the linkers, such as the GNU ld or older versions of LLD, are unable to process bitcodes.
2 parents b5b8b07 + 374368d commit 14c6f00

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed

tests/build-std/main.rs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,37 @@ fn basic() {
187187
assert_eq!(p.glob(deps_dir.join("*.dylib")).count(), 0);
188188
}
189189

190+
#[cargo_test(build_std_real)]
191+
fn lto() {
192+
// Checks that `-Zbuild-std` can work with `lto = "thin"`.
193+
// This regression is from https://github.com/rust-lang/rust/issues/146109.
194+
let p = project()
195+
.file(
196+
"Cargo.toml",
197+
r#"
198+
[package]
199+
name = "foo"
200+
version = "0.1.0"
201+
edition = "2021"
202+
203+
[profile.dev]
204+
lto = "thin"
205+
"#,
206+
)
207+
.file(
208+
"src/main.rs",
209+
r#"
210+
fn main() {}
211+
"#,
212+
)
213+
.build();
214+
215+
p.cargo("build")
216+
.build_std_arg("std")
217+
.env("RUSTFLAGS", "-C linker-features=-lld")
218+
.run();
219+
}
220+
190221
#[cargo_test(build_std_real)]
191222
fn host_proc_macro() {
192223
let p = project()

0 commit comments

Comments
 (0)