Skip to content

Commit 5ddd51d

Browse files
authored
Unrolled build for rust-lang#133300
Rollup merge of rust-lang#133300 - Enselic:build-std-instrument-coverage, r=jieyouxu inject_panic_runtime(): Avoid double negation for 'any non rlib' <details> <summary>This PR originally did more things .Click to expand to see.</summary> By not trying to inject a profiler runtime when only building an rlib. This logic already exists for the panic runtime. This makes RUSTFLAGS="-Cinstrument-coverage" cargo build -Zbuild-std=std,profiler_builtins work. Note that you probably also need `RUST_COMPILER_RT_FOR_PROFILER=$src/llvm-project/compiler-rt` in your environment. cc rust-lang#79401 # Demonstration Before this fix you get these errors: ```console $ rm -rf target ; RUST_COMPILER_RT_FOR_PROFILER=/home/martin/src/llvm-project/compiler-rt RUSTFLAGS="-Cinstrument-coverage" cargo +nightly build --release -Zbuild-std=std,profiler_builtins error: `profiler_builtins` crate (required by compiler options) is not compatible with crate attribute `#![no_core]` error[E0152]: found duplicate lang item `manually_drop` = note: first definition in `core` loaded from /home/martin/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib/rustlib/x86_64-unknown-linux-gnu/lib/libcore-d453bab70303062c.rlib = note: second definition in the local crate (`core`) ``` With the fix the build succeeds: ```console $ rm -rf target ; RUST_COMPILER_RT_FOR_PROFILER=/home/martin/src/llvm-project/compiler-rt RUSTFLAGS="-Cinstrument-coverage" cargo +stage1 build --release -Zbuild-std=std,profiler_builtins Finished `release` profile [optimized] target(s) in 45.57s ``` And we can check code coverage. My example program looks like this: ```rs fn main() { if std::env::args_os().nth(1) == Some("write-file".into()) { std::fs::write("hello.txt", "Hello, world!").unwrap(); } else { println!("Hello, world!"); } } ``` when the program prints to stdout: ``` $ LLVM_PROFILE_FILE=stdout.profraw ./target/release/hello-world Hello, world! ``` we can see that `fs::write()` is not being used (note the `0`'s): ```console $ /home/martin/src/rust/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata merge -sparse stdout.profraw -o stdout.profdata $ /home/martin/src/rust/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-cov show target/release/hello-world --sources /home/martin/src/rust/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/src/rust/library/std/src/fs.rs --instr-profile stdout.profdata --color | grep -A 3 'pub fn write(&mut self, write: b ool) -> &mut Self {' 1357| 0| pub fn write(&mut self, write: bool) -> &mut Self { 1358| 0| self.0.write(write); 1359| 0| self 1360| 0| } ``` but when we print to a file: ```console $ LLVM_PROFILE_FILE=file.profraw ./target/release/hello-world write-file ``` the code coverage shows `fs::write()` as being used (note the `1`'s): ```console $ /home/martin/src/rust/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-profdata merge -sparse file.profraw -o file.profdata $ /home/martin/src/rust/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/x86_64-unknown-linux-gnu/bin/llvm-cov show target/release/hello-world --sources /home/martin/src/rust/build/x86_64-unknown-linux-gnu/stage1/lib/rustlib/src/rust/library/std/src/fs.rs --instr-profile file.profdata --color | grep -A 3 'pub fn write(&mut self, write: bool) -> &mut Self {' 1357| 1| pub fn write(&mut self, write: bool) -> &mut Self { 1358| 1| self.0.write(write); 1359| 1| self 1360| 1| } ``` </summary>
2 parents ab3cf26 + a0e5aea commit 5ddd51d

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

compiler/rustc_metadata/src/creader.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -724,8 +724,8 @@ impl<'a, 'tcx> CrateLoader<'a, 'tcx> {
724724
fn inject_panic_runtime(&mut self, krate: &ast::Crate) {
725725
// If we're only compiling an rlib, then there's no need to select a
726726
// panic runtime, so we just skip this section entirely.
727-
let any_non_rlib = self.tcx.crate_types().iter().any(|ct| *ct != CrateType::Rlib);
728-
if !any_non_rlib {
727+
let only_rlib = self.tcx.crate_types().iter().all(|ct| *ct == CrateType::Rlib);
728+
if only_rlib {
729729
info!("panic runtime injection skipped, only generating rlib");
730730
return;
731731
}

0 commit comments

Comments
 (0)