Merged
Conversation
The generated code for various operations has local variables:
```
pub const fn union(self, other: Self) -> Self {
let f = self;
let other = other;
{ Self::from_bits_retain(f.bits() | other.bits()) }
}
```
These are present due to the way the relevant macros are structured.
This commit restructures the `__impl_bitflags` macro to avoid these
indirections, resulting in this code instead:
```
pub const fn union(self, other: Self) -> Self {
Self(self.0 | other.0)
}
```
Benefits:
- `cargo expand` output is nicer.
- It is a little faster to compile.
- `__impl_bitflags!` becomes simpler, with many fewer parameters. E.g.
`$self` instead of `$union0`, `$intersect0`, etc.
- The `__impl_bitflags!` call sites are also a little nicer, using
`&self` instead of `f`.
It's simpler to just use `Self()` and `.0`. Here's an example of how
that changes the output for one method:
```
pub const fn union(self, other: Self) -> Self {
- Self::from_bits_retain(self.bits() | other.bits())
+ Self(self.0 | other.0)
}
```
As well as being simpler, this change makes the code a tiny bit faster
to compile, and results in much better code quality (no function calls)
in dev builds.
KodrAus
approved these changes
Aug 22, 2025
Member
KodrAus
left a comment
There was a problem hiding this comment.
Thanks @nnethercote! These changes all seem reasonable to me 👍
nnethercote
added a commit
to nnethercote/rust
that referenced
this pull request
Aug 24, 2025
The `bitflags!` macro in the latest release has slightly streamlined codegen. See bitflags/bitflags#458 for details.
Zalathar
added a commit
to Zalathar/rust
that referenced
this pull request
Aug 25, 2025
…triplett Update `bitflags` to 2.9.3. The `bitflags!` macro in the latest release has slightly streamlined codegen. See bitflags/bitflags#458 for details. r? `@yaahc`
Zalathar
added a commit
to Zalathar/rust
that referenced
this pull request
Aug 25, 2025
…triplett Update `bitflags` to 2.9.3. The `bitflags!` macro in the latest release has slightly streamlined codegen. See bitflags/bitflags#458 for details. r? `@yaahc`
rust-timer
added a commit
to rust-lang/rust
that referenced
this pull request
Aug 25, 2025
Rollup merge of #145828 - nnethercote:bitflags-2.9.3, r=joshtriplett Update `bitflags` to 2.9.3. The `bitflags!` macro in the latest release has slightly streamlined codegen. See bitflags/bitflags#458 for details. r? `@yaahc`
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Some minor improvements to the generated code. In a stress test containing 100 invocations like
bitflags! { struct F1: u32 { const A = 0b1; const B = 0b10; } }this reduced compile times by about 10%. The generated machine code is also a bit better in dev builds.