Skip to content

#[cfg(...)] ignores namespace #53489

@kennytm

Description

@kennytm
Member

Repro:

fn main() {
    println!("           cfg(a) = {}", cfg!(a));
    println!("cfg(something::a) = {}", cfg!(something::a));
}
$ rustc --cfg 'a' 1.rs && ./1
           cfg(a) = true
cfg(something::a) = true

$ rustc --cfg 'something::a' 1.rs && ./1
           cfg(a) = true
cfg(something::a) = true

$ rustc --cfg 'what::a' 1.rs && ./1
           cfg(a) = true
cfg(something::a) = true

while I expected these should print true/false, false/true and false/false instead.

(Note: this also affects --print cfg which something::a is collapsed to a)

cc @petrochenkov @eddyb


$ rustc -vV
rustc 1.30.0-nightly (33b923fd4 2018-08-18)
binary: rustc
commit-hash: 33b923fd44c5c5925e635815fce68bdf1f98740f
commit-date: 2018-08-18
host: x86_64-apple-darwin
release: 1.30.0-nightly
LLVM version: 7.0

Activity

eddyb

eddyb commented on Aug 19, 2018

@eddyb
Member

What happens if you also have cfg(a), does that interact with cfg(something::a)?

changed the title [-]--print=cfg ignores namespace[/-] [+]cfg(...) ignores namespace[/+] on Aug 19, 2018
changed the title [-]cfg(...) ignores namespace[/-] [+]#[cfg(...)] ignores namespace[/+] on Aug 19, 2018
kennytm

kennytm commented on Aug 19, 2018

@kennytm
MemberAuthor

@eddyb Oh no, cfg(something::a) is the same as cfg(a). Edited the issue.

kennytm

kennytm commented on Aug 19, 2018

@kennytm
MemberAuthor

This behavior exists since 1.27. On 1.26 or before, #[cfg(module::path)] is simply a syntax error.

petrochenkov

petrochenkov commented on Aug 19, 2018

@petrochenkov
Contributor

This looks like an unintended side-effect of #50030 that allowed arbitrary paths in meta-items (including nested ones), but fn name() for meta-items still returns the last segment ignoring everything else.
cc @flip1995 @topecongiro

Most of attributes, including cfg want nested meta-item paths to be single-segment.
So, looks like fn name should return Option<Name> and all its callers need to be taught to deal with None somehow - report an error, or pass it further, etc.

eddyb

eddyb commented on Aug 19, 2018

@eddyb
Member

I'd suggest having a flag on "meta-item parsing" to say whether it should allow paths at all - most of the builtin attributes don't need paths.

petrochenkov

petrochenkov commented on Oct 15, 2018

@petrochenkov
Contributor

The cfg issue specifically was fixed in #53893.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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

        Participants

        @eddyb@kennytm@petrochenkov

        Issue actions

          #[cfg(...)] ignores namespace · Issue #53489 · rust-lang/rust