bitflags 2.3.0 breaks flags that refer to constants in separate impl blocks via Self #353

Closed
opened 2023-05-17 01:34:03 -05:00 by Phantomical · 1 comment
Phantomical commented 2023-05-17 01:34:03 -05:00 (Migrated from github.com)

Hi!

The short of it is that the code below compiles with bitflags 2.2.1 but fails to compile with bitflags 2.3.0.

use bitflags::bitflags;

bitflags! {
    pub struct Flags: u32 {
        const SOME_FLAG = 1 << Self::SOME_FLAG_SHIFT;
    }
}

impl Flags {
    const SOME_FLAG_SHIFT: u32 = 5;
}
The error message emitted by rustc, in case it helps.
error[E0599]: no associated item named `SOME_FLAG_SHIFT` found for struct `InternalBitFlags` in the current scope
 --> src/lib.rs:5:38
  |
3 | / bitflags! {
4 | |     pub struct Flags: u32 {
5 | |         const SOME_FLAG = 1 << Self::SOME_FLAG_SHIFT;
  | |                                      ^^^^^^^^^^^^^^^
  | |                                      |
  | |                                      associated item not found in `InternalBitFlags`
  | |                                      help: there is an associated constant with a similar name: `SOME_FLAG`
6 | |     }
7 | | }
  | |_- associated item `SOME_FLAG_SHIFT` not found for this struct

For more information about this error, try `rustc --explain E0599`.
error: could not compile `bitflags-regression` (lib) due to previous error

I'm not sure how niche this pattern is but it did break the perf-event-data crate.

Hi! The short of it is that the code below compiles with bitflags 2.2.1 but fails to compile with bitflags 2.3.0. ```rust use bitflags::bitflags; bitflags! { pub struct Flags: u32 { const SOME_FLAG = 1 << Self::SOME_FLAG_SHIFT; } } impl Flags { const SOME_FLAG_SHIFT: u32 = 5; } ``` <details> <summary>The error message emitted by rustc, in case it helps.</summary> ``` error[E0599]: no associated item named `SOME_FLAG_SHIFT` found for struct `InternalBitFlags` in the current scope --> src/lib.rs:5:38 | 3 | / bitflags! { 4 | | pub struct Flags: u32 { 5 | | const SOME_FLAG = 1 << Self::SOME_FLAG_SHIFT; | | ^^^^^^^^^^^^^^^ | | | | | associated item not found in `InternalBitFlags` | | help: there is an associated constant with a similar name: `SOME_FLAG` 6 | | } 7 | | } | |_- associated item `SOME_FLAG_SHIFT` not found for this struct For more information about this error, try `rustc --explain E0599`. error: could not compile `bitflags-regression` (lib) due to previous error ``` </details> I'm not sure how niche this pattern is but it did break the `perf-event-data` crate.
KodrAus commented 2023-05-17 06:16:16 -05:00 (Migrated from github.com)

Thanks for the report @Phantomical! This looks like a regression in that we now add flags to both the overall type and the internally generated one, so Self refers to a different type in those cases. We should fix that up by not implementing the Flags trait on that internally generated type and working around any places we expect it.

Thanks for the report @Phantomical! This looks like a regression in that we now add flags to both the overall type and the internally generated one, so `Self` refers to a different type in those cases. We should fix that up by not implementing the `Flags` trait on that internally generated type and working around any places we expect it.
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#353
No description provided.