Skip to content

cfg!($crate), cfg!(crate), cfg!(self), cfg!(Self), and cfg!(super) should not be accepted #146968

@theemathas

Description

@theemathas

I tried this code:

macro_rules! foo {
    () => {
        cfg!($crate)
    };
}

fn main() {
    let b = foo!();
    println!("{b}");
}

I expected a compile error. Instead, I got the following warning, but the code compiles fine and prints false.

warning: unexpected `cfg` condition name: `$crate`
 --> src/main.rs:3:14
  |
3 |         cfg!($crate)
  |              ^^^^^^
...
8 |     let b = foo!();
  |             ------ in this macro invocation
  |
  = help: expected names are: `docsrs`, `feature`, and `test` and 31 more
  = help: consider using a Cargo feature instead
  = help: or consider adding in `Cargo.toml` the `check-cfg` lint config for the lint:
           [lints.rust]
           unexpected_cfgs = { level = "warn", check-cfg = ['cfg($crate)'] }
  = help: or consider adding `println!("cargo::rustc-check-cfg=cfg($crate)");` to the top of the `build.rs`
  = note: see <https://doc.rust-lang.org/nightly/rustc/check-cfg/cargo-specifics.html> for more information about checking conditional configuration
  = note: `#[warn(unexpected_cfgs)]` on by default
  = note: this warning originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

Since it's impossible to set the value of this cfg, I think this should cause a compile error.

See also #146967 for more nonsense with $crate.

Meta

Reproducible on the playground with version 1.92.0-nightly (2025-09-23 975e6c8fec280816d24f)

Metadata

Metadata

Assignees

Labels

A-cfgArea: `cfg` conditional compilationA-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.T-langRelevant to the language team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions