rustc --test should not check linking when using --emit=metadata or -Z no-trans

fc867bc
Opened by Wilfred Hughes at 2019-10-23 02:44:49

Suppose I have a library configured with panic = "abort":

$ cargo init --lib foo
$ cd foo
$ nano Cargo.toml
$ more Cargo.toml
[package]
name = "foo"
version = "0.1.0"
authors = ["Wilfred Hughes <me@wilfred.me.uk>"]

[dependencies]

[profile.dev]
panic = "abort"

I'm unable to build with with --test:

$ cargo rustc -- --test
   Compiling foo v0.1.0 (file:///home/wilfred/tmp/foo)
error: the linked panic runtime `panic_unwind` is not compiled with this crate's panic strategy `abort`

error: aborting due to previous error

error: Could not compile `foo`.

To learn more, run the command again with --verbose.

As discussed in https://github.com/rust-lang/cargo/issues/3166#issuecomment-252120541 this is because the test harness needs unwinding on panic.

However, I get the same error with -Z no-trans:

$ cargo rustc -- -Z no-trans --test 
   Compiling foo v0.1.0 (file:///home/wilfred/tmp/foo)
error: the linked panic runtime `panic_unwind` is not compiled with this crate's panic strategy `abort`

error: aborting due to previous error

error: Could not compile `foo`.

To learn more, run the command again with --verbose.

This causes problems in editor integration like https://github.com/flycheck/flycheck-rust/issues/47 . We want to pass --test as otherwise we don't get syntax checking on test files. However, we get the above error for projects with abort-on-panic.

Could we teach --test to ignore the runtime when we pass -Z no-trans?

  1. -Z no-trans is on its way out, now that we have the meatdata-only crate type, right?

    Steve Klabnik at 2017-02-19 01:25:26

  2. Right; -Z no-trans will likely become an unusable actually unstable flag even before that.

    Simonas Kazlauskas at 2017-02-19 10:34:59

  3. Aha, great to see this stabilising. My understanding from #38666 is that --crate-type=metadata has had a short life (and had issues distinguishing between libraries and applications) and the correct version is now --emit=metadata.

    We'll certainly move to --emit=metadata once it lands on stable Rust.

    Note that the same problem exists with --emit=metadata though:

    $ rustc --version
    rustc 1.17.0-nightly (306035c21 2017-02-18)
    $ cargo rustc -- --emit=metadata --test
       Compiling abortexample v0.1.0 (file:///home/wilfred/tmp/abortexample)
    error: the linked panic runtime `panic_unwind` is not compiled with this crate's panic strategy `abort`
    
    error: aborting due to previous error
    
    error: Could not compile `abortexample`.
    
    To learn more, run the command again with --verbose.
    

    Wilfred Hughes at 2017-02-19 14:47:34

  4. As far as I can tell this works today, so I'm going to close. rustc --emit=metadata -Cpanic=abort --test test.rs works on:

    #[test]
    fn t() {
        panic!("oh no!");
    }
    

    Mark Rousskov at 2017-05-23 19:16:04

  5. @Mark-Simulacrum I don't think your test is equivalent. The example from the first comment still fails on current Rust. Could you please reopen?

    Wilfred Hughes at 2017-05-23 20:07:58

  6. The intended way to do this with cargo is to use cargo check --profile=test. That forces panic to be unwind in order to be compatible with libtest.

    Additionally, once #65710 lands, there is a new flag cargo check --profile=test -Zpanic-abort-tests where it will successfully build with abort.

    Eric Huss at 2019-10-23 02:44:49