Constants cannot be cfg-gated multiple times #137

Closed
opened 2017-11-16 02:58:15 -06:00 by nox · 5 comments
nox commented 2017-11-16 02:58:15 -06:00 (Migrated from github.com)
No description provided.
sfackler commented 2017-12-03 22:35:55 -06:00 (Migrated from github.com)

Why not? This seems to work just fine:

#[macro_use]
extern crate bitflags;

bitflags! {
    struct Flags: u32 {
        #[cfg(target_os = "linux")]
        const FOO = 1;
    }
}

fn main() {
    let _ = Flags::FOO;
}
Why not? This seems to work just fine: ```rust #[macro_use] extern crate bitflags; bitflags! { struct Flags: u32 { #[cfg(target_os = "linux")] const FOO = 1; } } fn main() { let _ = Flags::FOO; } ```
nox commented 2017-12-04 02:14:11 -06:00 (Migrated from github.com)

@sfackler Thanks for the answer, I just realised I misformulated this issue.

This doesn't work:

#[macro_use]
extern crate bitflags;

bitflags! {
    struct Flags: u32 {
        #[cfg(target_os = "linux")]
        const FOO = 1;
        #[cfg(not(target_os = "linux"))]
        const FOO = 2;
    }
}

fn main() {
    let _ = Flags::FOO;
}

I've worked around it doing this:

bitflags! {
    struct Flags: u32 {
        const FOO = {
            #[cfg(target_os = "linux")] { 1 }
            #[cfg(not(target_os = "linux"))] { 2 }
        };
    }
}
@sfackler Thanks for the answer, I just realised I misformulated this issue. This doesn't work: ```rust #[macro_use] extern crate bitflags; bitflags! { struct Flags: u32 { #[cfg(target_os = "linux")] const FOO = 1; #[cfg(not(target_os = "linux"))] const FOO = 2; } } fn main() { let _ = Flags::FOO; } ``` I've worked around it doing this: ```rust bitflags! { struct Flags: u32 { const FOO = { #[cfg(target_os = "linux")] { 1 } #[cfg(not(target_os = "linux"))] { 2 } }; } } ```
sfackler commented 2017-12-04 14:38:05 -06:00 (Migrated from github.com)

Weird!

Weird!
KodrAus commented 2021-10-02 05:20:46 -05:00 (Migrated from github.com)

It looks like the problem here is our logic that attempts to handle flags that have been cfg'd out. We intentionally omit attributes from flags to ensure they're known to the debug format and the all function. Unfortunately this doesn't handle flags where the same flag is defined with different values based on cfg. I haven't quite unpicked why we need the names of all flags to be available, but I think the way we currently do it might just be plain broken given a flag isn't just either present or not, it may have a totally different value.

It looks like the problem here is our logic that attempts to handle flags that have been cfg'd out. We intentionally omit attributes from flags to ensure they're known to the debug format and the `all` function. Unfortunately this doesn't handle flags where the same flag is defined with different values based on cfg. I haven't quite unpicked _why_ we need the names of all flags to be available, but I think the way we currently do it might just be plain broken given a flag isn't just either present or not, it may have a totally different value.
KodrAus commented 2021-10-02 06:19:07 -05:00 (Migrated from github.com)

It looks like it might actually be a bit of an artefact from pre-associated-const days (how far we’ve come). It might be possible to simplify these now and either avoid the need for those traits or just apply the cfgs to them.

It looks like it might actually be a bit of an artefact from pre-associated-const days (how far we’ve come). It might be possible to simplify these now and either avoid the need for those traits or just apply the cfgs to them.
Sign in to join this conversation.
No labels
bug
good first issue
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
gh-bitflags/bitflags#137
No description provided.