Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#![cfg_attr(not(test), no_std)] doesn't work when building multiple crate-types #48665

Open
glandium opened this issue Mar 2, 2018 · 3 comments
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@glandium
Copy link
Contributor

glandium commented Mar 2, 2018

$ cargo create --lib foo
$ cd foo
$ cat >> Cargo.toml <<EOF
[lib]
crate-type = ["rlib", "staticlib"]
[profile.dev]
panic="abort"
EOF
$ cat > src/lib.rs <<EOF
#![cfg_attr(not(test), no_std)]
#![feature(lang_items)]

#[cfg(not(test))]
#[lang = "panic_fmt"]
#[no_mangle]
pub fn panic_fmt(_: core::fmt::Arguments, _: &'static str, _: u32, _: u32) -> ! {
    loop {}
}
EOF
$ cargo +nightly build
   Compiling foo v0.1.0 (file:///tmp/foo)
    Finished dev [unoptimized + debuginfo] target(s) in 0.13 secs
$ cargo +nightly test
   Compiling foo v0.1.0 (file:///tmp/foo)
error: language item required, but not found: `eh_personality`

error: aborting due to previous error

That is an error that is expected with no_std, but the code is supposed to be essentially empty in the test configuration.

It works when crate-type contains only one value (either one).

@pietroalbini pietroalbini added C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 6, 2018
@tcz717
Copy link

tcz717 commented Aug 29, 2020

I have the same issue even after 2 years (2020)

@harrier-lcc
Copy link

just hit the same issue today. any follow-up on this?

@XiangYyang
Copy link

I have the same issue #100766.

It doesn't seem to be well resolved.

However, I found some options in Cargo test :

I noted that I can use the --lib to special some libraries what I want to link. So I tried to special linking the test library:

cargo test --lib test

And in lib.rs:

-  #![cfg_attr(not(test), no_std)]
+ // specify no_std does not mean that the std crate cannot be linked.
+ // https://doc.rust-lang.org/reference/names/preludes.html#the-no_std-attribute
+ #![no_std]

It is now ready to run tests for the time being.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants