Skip to content

#[cfg(no_std)] doesn't work #42190

@Susurrus

Description

@Susurrus

I'm working on a no_std crate that I'd like to test and also compile in a std environment. Working with includes is a little painful right now because #[cfg(no_std)] doesn't seem to work.

As an example, I'd expect the following code to work both with and without no_std (commenting out the first line. Instead, it doesn't work as written below (but will work if you comment out no_std.

#![no_std]

#[cfg(no_std)]
use core::mem;
#[cfg(not(no_std))]
use std::mem;

fn main() {
    let x: u32 = unsafe { mem::uninitialized() };
}

You'll see the following error:

error[E0432]: unresolved import `std::mem`
 --> <anon>:6:5
  |
6 | use std::mem;
  |     ^^^^^^^^ Maybe a missing `extern crate std;`?

In my crate I'd like to do things like keep println!() in my crate for testing during development, but have them work during testing. And also configure the entire module to be no_std when not testing using #![cfg_attr(not(test), no_std)]. Neither of these work because of the current situation.

cc @japaric This is something I mentions in your users forum thread about embedded dev.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions