Skip to content

#![cfg] to disable a crate disables #![no_std] #39183

@retep998

Description

@retep998

Using #![cfg] to disable a crate on platforms where it makes no sense is usually a great idea. That way people can unconditionally depend on your crate and use it only on the targets where it is needed. However it disables the entire crate, including all global attributes, which means normally a crate that has #![no_std] wouldn't depend on std, but if you use #![cfg] to disable that crate, suddenly it does depend on std! The only workaround is to not use #![cfg] and instead manually disable each item in the lib.rs, except for specific attributes like #![no_std].

For example:

// rustc foo.rs --crate-type=rlib
#![no_std]
#![cfg(any())]
// rustc bar.rs -Zprint-link-args -Lcrate=current_directory
#![no_std]
extern crate foo;
fn main() {}

Observe as libstd is passed to the linker despite both crates using #![no_std].

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-attributesArea: Attributes (`#[…]`, `#![…]`)C-bugCategory: This is a bug.I-needs-decisionIssue: In need of a decision.T-langRelevant to the language team

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions