repo
stringclasses 1
value | problem_statement
stringclasses 9
values | hints_text
stringclasses 4
values | instance_id
stringclasses 9
values | issue_numbers
listlengths 1
1
| base_commit
stringclasses 8
values | test_patch
stringclasses 9
values | version
stringclasses 6
values | pull_number
int64 211
355
| created_at
stringclasses 9
values | patch
stringclasses 9
values | environment_setup_commit
stringclasses 6
values | FAIL_TO_PASS
listlengths 1
2
| PASS_TO_PASS
listlengths 42
95
| FAIL_TO_FAIL
listlengths 0
1
| PASS_TO_FAIL
listlengths 0
0
|
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
bitflags/bitflags
|
Clippy warnings around "manual implementation of an assign operation"
Hi.
I've run into a new clippy lint warnings such as the following:
> manual implementation of an assign operation
> for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#assign_op_pattern
> `#[warn(clippy::assign_op_pattern)]` on by default
I'm following the example from the docs page for the use of the macro (more or less, as below). Can you enlighten me as to why this lint notification is appearing here and if there is some way to fix it? I know I can silence the warnings, it's just annoying to see it pop up whenever I run into it.
```rust
bitflags! {
pub struct MemoryAccess: u8 {
/// None.
const N = 1 << 0;
/// Public read.
const R = 1 << 1;
/// Public write.
const W = 1 << 2;
/// Private read.
const PR = 1 << 3;
/// Private write.
const PW = 1 << 4;
/// Execute.
const EX = 1 << 5;
}
}
```
Thanks!
|
bitflags__bitflags-355
|
[
"357"
] |
31d3e4afefc964045156d7fe3622733f48511353
|
diff --git a/tests/compile-fail/bitflags_custom_bits.rs b/tests/compile-fail/bitflags_custom_bits.rs
--- a/tests/compile-fail/bitflags_custom_bits.rs
+++ b/tests/compile-fail/bitflags_custom_bits.rs
@@ -19,7 +19,7 @@ use std::{
},
};
-use bitflags::{bitflags, Bits, parser::{ParseError, FromHex}};
+use bitflags::{bitflags, Bits, parser::{ParseError, WriteHex, ParseHex}};
// Ideally we'd actually want this to work, but currently need something like `num`'s `Zero`
// With some design work it could be made possible
diff --git a/tests/compile-fail/bitflags_custom_bits.rs b/tests/compile-fail/bitflags_custom_bits.rs
--- a/tests/compile-fail/bitflags_custom_bits.rs
+++ b/tests/compile-fail/bitflags_custom_bits.rs
@@ -117,12 +117,18 @@ impl Binary for MyInt {
}
}
-impl FromHex for MyInt {
- fn from_hex(input: &str) -> Result<Self, ParseError> {
+impl ParseHex for MyInt {
+ fn parse_hex(input: &str) -> Result<Self, ParseError> {
Ok(MyInt(u8::from_str_radix(input, 16).map_err(|_| ParseError::invalid_hex_flag(input))?))
}
}
+impl WriteHex for MyInt {
+ fn write_hex<W: fmt::Write>(&self, writer: W) -> fmt::Result {
+ LowerHex::fmt(&self.0, writer)
+ }
+}
+
bitflags! {
struct Flags128: MyInt {
const A = MyInt(0b0000_0001u8);
diff --git a/tests/compile-fail/bitflags_custom_bits.stderr b/tests/compile-fail/bitflags_custom_bits.stderr
--- a/tests/compile-fail/bitflags_custom_bits.stderr
+++ b/tests/compile-fail/bitflags_custom_bits.stderr
@@ -1,7 +1,7 @@
error[E0277]: the trait bound `MyInt: bitflags::traits::Primitive` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:127:22
+ --> tests/compile-fail/bitflags_custom_bits.rs:133:22
|
-127 | struct Flags128: MyInt {
+133 | struct Flags128: MyInt {
| ^^^^^ the trait `bitflags::traits::Primitive` is not implemented for `MyInt`
|
= help: the following other types implement trait `bitflags::traits::Primitive`:
diff --git a/tests/compile-fail/bitflags_custom_bits.stderr b/tests/compile-fail/bitflags_custom_bits.stderr
--- a/tests/compile-fail/bitflags_custom_bits.stderr
+++ b/tests/compile-fail/bitflags_custom_bits.stderr
@@ -20,442 +20,457 @@ note: required by a bound in `PublicFlags::Primitive`
| type Primitive: Primitive;
| ^^^^^^^^^ required by this bound in `PublicFlags::Primitive`
+error[E0308]: mismatched types
+ --> tests/compile-fail/bitflags_custom_bits.rs:128:32
+ |
+127 | fn write_hex<W: fmt::Write>(&self, writer: W) -> fmt::Result {
+ | - this type parameter
+128 | LowerHex::fmt(&self.0, writer)
+ | ------------- ^^^^^^ expected `&mut Formatter<'_>`, found type parameter `W`
+ | |
+ | arguments to this function are incorrect
+ |
+ = note: expected mutable reference `&mut Formatter<'_>`
+ found type parameter `W`
+note: method defined here
+ --> $RUST/core/src/fmt/mod.rs
+
error[E0277]: can't compare `MyInt` with `_` in const contexts
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt == _`
|
= help: the trait `~const PartialEq<_>` is not implemented for `MyInt`
note: the trait `PartialEq<_>` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: can't compare `MyInt` with `_` in const contexts
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt == _`
|
= help: the trait `~const PartialEq<_>` is not implemented for `MyInt`
note: the trait `PartialEq<_>` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: BitAnd` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt & MyInt`
|
= help: the trait `~const BitAnd` is not implemented for `MyInt`
note: the trait `BitAnd` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: can't compare `MyInt` with `_` in const contexts
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt == _`
|
= help: the trait `~const PartialEq<_>` is not implemented for `MyInt`
note: the trait `PartialEq<_>` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: BitOr` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt | MyInt`
|
= help: the trait `~const BitOr` is not implemented for `MyInt`
note: the trait `BitOr` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: can't compare `MyInt` with `_` in const contexts
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt == _`
|
= help: the trait `~const PartialEq<_>` is not implemented for `MyInt`
note: the trait `PartialEq<_>` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: BitOr` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt | MyInt`
|
= help: the trait `~const BitOr` is not implemented for `MyInt`
note: the trait `BitOr` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: can't compare `MyInt` with `_` in const contexts
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt == _`
|
= help: the trait `~const PartialEq<_>` is not implemented for `MyInt`
note: the trait `PartialEq<_>` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: BitAnd` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt & MyInt`
|
= help: the trait `~const BitAnd` is not implemented for `MyInt`
note: the trait `BitAnd` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: BitAnd` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt & MyInt`
|
= help: the trait `~const BitAnd` is not implemented for `MyInt`
note: the trait `BitAnd` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: can't compare `MyInt` with `_` in const contexts
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt == _`
|
= help: the trait `~const PartialEq<_>` is not implemented for `MyInt`
note: the trait `PartialEq<_>` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: BitAnd` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt & MyInt`
|
= help: the trait `~const BitAnd` is not implemented for `MyInt`
note: the trait `BitAnd` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: BitOr` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt | MyInt`
|
= help: the trait `~const BitOr` is not implemented for `MyInt`
note: the trait `BitOr` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: BitAnd` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt & MyInt`
|
= help: the trait `~const BitAnd` is not implemented for `MyInt`
note: the trait `BitAnd` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: Not` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ the trait `~const Not` is not implemented for `MyInt`
|
note: the trait `Not` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: BitXor` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ no implementation for `MyInt ^ MyInt`
|
= help: the trait `~const BitXor` is not implemented for `MyInt`
note: the trait `BitXor` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0277]: the trait bound `MyInt: Not` is not satisfied
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^ the trait `~const Not` is not implemented for `MyInt`
|
note: the trait `Not` is implemented for `MyInt`, but that implementation is not `const`
- --> tests/compile-fail/bitflags_custom_bits.rs:126:1
- |
-126 | / bitflags! {
-127 | | struct Flags128: MyInt {
-128 | | const A = MyInt(0b0000_0001u8);
-129 | | const B = MyInt(0b0000_0010u8);
-130 | | const C = MyInt(0b0000_0100u8);
-131 | | }
-132 | | }
+ --> tests/compile-fail/bitflags_custom_bits.rs:132:1
+ |
+132 | / bitflags! {
+133 | | struct Flags128: MyInt {
+134 | | const A = MyInt(0b0000_0001u8);
+135 | | const B = MyInt(0b0000_0010u8);
+136 | | const C = MyInt(0b0000_0100u8);
+137 | | }
+138 | | }
| |_^
= note: this error originates in the macro `__impl_public_bitflags` which comes from the expansion of the macro `bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
diff --git /dev/null b/tests/compile-pass/bitflags_self_in_value.rs
new file mode 100644
--- /dev/null
+++ b/tests/compile-pass/bitflags_self_in_value.rs
@@ -0,0 +1,15 @@
+use bitflags::bitflags;
+
+bitflags! {
+ pub struct Flags: u32 {
+ const SOME_FLAG = 1 << Self::SOME_FLAG_SHIFT;
+ }
+}
+
+impl Flags {
+ const SOME_FLAG_SHIFT: u32 = 5;
+}
+
+fn main() {
+
+}
diff --git a/tests/smoke-test/src/main.rs b/tests/smoke-test/src/main.rs
--- a/tests/smoke-test/src/main.rs
+++ b/tests/smoke-test/src/main.rs
@@ -1,3 +1,5 @@
+#![deny(warnings)]
+
use bitflags::bitflags;
bitflags! {
|
2.3
| 355
|
2023-05-17T11:22:15Z
|
diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml
--- a/.github/workflows/rust.yml
+++ b/.github/workflows/rust.yml
@@ -81,6 +81,23 @@ jobs:
- name: Default features
run: cross test --target mips-unknown-linux-gnu
+ clippy:
+ name: Clippy
+ runs-on: ubuntu-latest
+ steps:
+ - name: Checkout sources
+ uses: actions/checkout@8e5e7e5ab8b370d6c329ec480221332ada57f0ab
+
+ - name: Install Clippy
+ run: |
+ rustup update beta
+ rustup component add clippy --toolchain beta
+
+ - name: Default features
+ run: |
+ cd ./tests/smoke-test
+ cargo +beta clippy
+
embedded:
name: Build (embedded)
runs-on: ubuntu-latest
diff --git a/src/example_generated.rs b/src/example_generated.rs
--- a/src/example_generated.rs
+++ b/src/example_generated.rs
@@ -39,7 +39,7 @@ __impl_public_bitflags_forward! {
}
__impl_public_bitflags_iter! {
- Flags
+ Flags: u32, Flags
}
__impl_public_bitflags_consts! {
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -19,7 +19,7 @@ Next, define a macro like so:
#[cfg(feature = "serde")]
macro_rules! __impl_external_bitflags_my_library {
(
- $InternalBitFlags:ident: $T:ty {
+ $InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -35,7 +35,7 @@ macro_rules! __impl_external_bitflags_my_library {
#[cfg(not(feature = "my_library"))]
macro_rules! __impl_external_bitflags_my_library {
(
- $InternalBitFlags:ident: $T:ty {
+ $InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -55,7 +55,7 @@ Now, we add our macro call to the `__impl_external_bitflags` macro body:
```rust
__impl_external_bitflags_my_library! {
- $InternalBitFlags: $T {
+ $InternalBitFlags: $T, $PublicBitFlags {
$(
$(#[$attr $($args)*])*
$Flag;
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -76,6 +76,51 @@ pub(crate) mod __private {
pub use bytemuck;
}
+/// Implements traits from external libraries for the internal bitflags type.
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! __impl_external_bitflags {
+ (
+ $InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
+ $(
+ $(#[$attr:ident $($args:tt)*])*
+ $Flag:ident;
+ )*
+ }
+ ) => {
+ // Any new library traits impls should be added here
+ // Use `serde` as an example: generate code when the feature is available,
+ // and a no-op when it isn't
+
+ __impl_external_bitflags_serde! {
+ $InternalBitFlags: $T, $PublicBitFlags {
+ $(
+ $(#[$attr $($args)*])*
+ $Flag;
+ )*
+ }
+ }
+
+ __impl_external_bitflags_arbitrary! {
+ $InternalBitFlags: $T, $PublicBitFlags {
+ $(
+ $(#[$attr $($args)*])*
+ $Flag;
+ )*
+ }
+ }
+
+ __impl_external_bitflags_bytemuck! {
+ $InternalBitFlags: $T, $PublicBitFlags {
+ $(
+ $(#[$attr $($args)*])*
+ $Flag;
+ )*
+ }
+ }
+ };
+}
+
#[cfg(feature = "serde")]
pub mod serde;
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -85,11 +130,11 @@ pub mod serde;
#[cfg(feature = "serde")]
macro_rules! __impl_external_bitflags_serde {
(
- $InternalBitFlags:ident: $T:ty {
+ $InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
- )*
+ )*
}
) => {
impl $crate::__private::serde::Serialize for $InternalBitFlags {
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -98,7 +143,7 @@ macro_rules! __impl_external_bitflags_serde {
serializer: S,
) -> $crate::__private::core::result::Result<S::Ok, S::Error> {
$crate::serde::serialize(
- self,
+ &$PublicBitFlags::from_bits_retain(self.bits()),
serializer,
)
}
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -108,9 +153,11 @@ macro_rules! __impl_external_bitflags_serde {
fn deserialize<D: $crate::__private::serde::Deserializer<'de>>(
deserializer: D,
) -> $crate::__private::core::result::Result<Self, D::Error> {
- $crate::serde::deserialize(
+ let flags: $PublicBitFlags = $crate::serde::deserialize(
deserializer,
- )
+ )?;
+
+ Ok(flags.0)
}
}
};
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -121,11 +168,11 @@ macro_rules! __impl_external_bitflags_serde {
#[cfg(not(feature = "serde"))]
macro_rules! __impl_external_bitflags_serde {
(
- $InternalBitFlags:ident: $T:ty {
+ $InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
- )*
+ )*
}
) => {};
}
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -136,58 +183,13 @@ pub mod arbitrary;
#[cfg(feature = "bytemuck")]
mod bytemuck;
-/// Implements traits from external libraries for the internal bitflags type.
-#[macro_export(local_inner_macros)]
-#[doc(hidden)]
-macro_rules! __impl_external_bitflags {
- (
- $InternalBitFlags:ident: $T:ty {
- $(
- $(#[$attr:ident $($args:tt)*])*
- $Flag:ident;
- )*
- }
- ) => {
- // Any new library traits impls should be added here
- // Use `serde` as an example: generate code when the feature is available,
- // and a no-op when it isn't
-
- __impl_external_bitflags_serde! {
- $InternalBitFlags: $T {
- $(
- $(#[$attr $($args)*])*
- $Flag;
- )*
- }
- }
-
- __impl_external_bitflags_arbitrary! {
- $InternalBitFlags: $T {
- $(
- $(#[$attr $($args)*])*
- $Flag;
- )*
- }
- }
-
- __impl_external_bitflags_bytemuck! {
- $InternalBitFlags: $T {
- $(
- $(#[$attr $($args)*])*
- $Flag;
- )*
- }
- }
- };
-}
-
/// Implement `Arbitrary` for the internal bitflags type.
#[macro_export(local_inner_macros)]
#[doc(hidden)]
#[cfg(feature = "arbitrary")]
macro_rules! __impl_external_bitflags_arbitrary {
(
- $InternalBitFlags:ident: $T:ty {
+ $InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -198,7 +200,7 @@ macro_rules! __impl_external_bitflags_arbitrary {
fn arbitrary(
u: &mut $crate::__private::arbitrary::Unstructured<'a>,
) -> $crate::__private::arbitrary::Result<Self> {
- $crate::arbitrary::arbitrary(u)
+ $crate::arbitrary::arbitrary::<$PublicBitFlags>(u).map(|flags| flags.0)
}
}
};
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -209,12 +211,12 @@ macro_rules! __impl_external_bitflags_arbitrary {
#[cfg(not(feature = "arbitrary"))]
macro_rules! __impl_external_bitflags_arbitrary {
(
- $InternalBitFlags:ident: $T:ty {
- $(
- $(#[$attr:ident $($args:tt)*])*
- $Flag:ident;
- )*
- }
+ $InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
+ $(
+ $(#[$attr:ident $($args:tt)*])*
+ $Flag:ident;
+ )*
+ }
) => {};
}
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -224,11 +226,11 @@ macro_rules! __impl_external_bitflags_arbitrary {
#[cfg(feature = "bytemuck")]
macro_rules! __impl_external_bitflags_bytemuck {
(
- $InternalBitFlags:ident: $T:ty {
+ $InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
- $Flag:ident;
- )*
+ $Flag:ident;
+ )*
}
) => {
// SAFETY: $InternalBitFlags is guaranteed to have the same ABI as $T,
diff --git a/src/external.rs b/src/external.rs
--- a/src/external.rs
+++ b/src/external.rs
@@ -256,11 +258,11 @@ macro_rules! __impl_external_bitflags_bytemuck {
#[cfg(not(feature = "bytemuck"))]
macro_rules! __impl_external_bitflags_bytemuck {
(
- $InternalBitFlags:ident: $T:ty {
+ $InternalBitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
- $Flag:ident;
- )*
+ $Flag:ident;
+ )*
}
) => {};
}
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -98,7 +98,7 @@ macro_rules! __impl_internal_bitflags {
// The internal flags type offers a similar API to the public one
__impl_public_bitflags! {
- $InternalBitFlags: $T {
+ $InternalBitFlags: $T, $PublicBitFlags {
$(
$(#[$attr $($args)*])*
$Flag;
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -106,19 +106,8 @@ macro_rules! __impl_internal_bitflags {
}
}
- __impl_public_bitflags_consts! {
- $InternalBitFlags: $T {
- $(
- $(#[$attr $($args)*])*
- #[allow(
- dead_code,
- deprecated,
- unused_attributes,
- non_upper_case_globals
- )]
- $Flag = $value;
- )*
- }
+ __impl_public_bitflags_iter! {
+ $InternalBitFlags: $T, $PublicBitFlags
}
impl $InternalBitFlags {
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -127,18 +116,6 @@ macro_rules! __impl_internal_bitflags {
pub fn bits_mut(&mut self) -> &mut $T {
&mut self.0
}
-
- /// Iterate over enabled flag values.
- #[inline]
- pub const fn iter(&self) -> $crate::iter::Iter<$PublicBitFlags> {
- $crate::iter::Iter::__private_const_new(<$PublicBitFlags as $crate::Flags>::FLAGS, $PublicBitFlags::from_bits_retain(self.0), $PublicBitFlags::from_bits_retain(self.0))
- }
-
- /// Iterate over enabled flag values with their stringified names.
- #[inline]
- pub const fn iter_names(&self) -> $crate::iter::IterNames<$PublicBitFlags> {
- $crate::iter::IterNames::__private_const_new(<$PublicBitFlags as $crate::Flags>::FLAGS, $PublicBitFlags::from_bits_retain(self.0), $PublicBitFlags::from_bits_retain(self.0))
- }
}
};
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -590,7 +590,8 @@ macro_rules! bitflags {
unused_attributes,
unused_mut,
unused_imports,
- non_upper_case_globals
+ non_upper_case_globals,
+ clippy::assign_op_pattern
)]
const _: () = {
// Declared in a "hidden" scope that can't be reached directly
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -610,7 +611,7 @@ macro_rules! bitflags {
// This is where new library trait implementations can be added
__impl_external_bitflags! {
- InternalBitFlags: $T {
+ InternalBitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag;
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -623,7 +624,7 @@ macro_rules! bitflags {
}
__impl_public_bitflags_iter! {
- $BitFlags
+ $BitFlags: $T, $BitFlags
}
};
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -657,11 +658,12 @@ macro_rules! bitflags {
unused_attributes,
unused_mut,
unused_imports,
- non_upper_case_globals
+ non_upper_case_globals,
+ clippy::assign_op_pattern
)]
const _: () = {
__impl_public_bitflags! {
- $BitFlags: $T {
+ $BitFlags: $T, $BitFlags {
$(
$(#[$inner $($args)*])*
$Flag;
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -670,7 +672,7 @@ macro_rules! bitflags {
}
__impl_public_bitflags_iter! {
- $BitFlags
+ $BitFlags: $T, $BitFlags
}
};
diff --git a/src/public.rs b/src/public.rs
--- a/src/public.rs
+++ b/src/public.rs
@@ -57,7 +57,7 @@ macro_rules! __impl_public_bitflags_forward {
Self($InternalBitFlags::from_bits_retain(bits))
}
- fn from_name(name){
+ fn from_name(name) {
match $InternalBitFlags::from_name(name) {
$crate::__private::core::option::Option::Some(bits) => $crate::__private::core::option::Option::Some(Self(bits)),
$crate::__private::core::option::Option::None => $crate::__private::core::option::Option::None,
diff --git a/src/public.rs b/src/public.rs
--- a/src/public.rs
+++ b/src/public.rs
@@ -130,7 +130,7 @@ macro_rules! __impl_public_bitflags_forward {
#[doc(hidden)]
macro_rules! __impl_public_bitflags {
(
- $PublicBitFlags:ident: $T:ty {
+ $BitFlags:ident: $T:ty, $PublicBitFlags:ident {
$(
$(#[$attr:ident $($args:tt)*])*
$Flag:ident;
diff --git a/src/public.rs b/src/public.rs
--- a/src/public.rs
+++ b/src/public.rs
@@ -138,7 +138,7 @@ macro_rules! __impl_public_bitflags {
}
) => {
__impl_bitflags! {
- $PublicBitFlags: $T {
+ $BitFlags: $T {
fn empty() {
Self(<$T as $crate::Bits>::EMPTY)
}
diff --git a/src/public.rs b/src/public.rs
--- a/src/public.rs
+++ b/src/public.rs
@@ -260,7 +260,7 @@ macro_rules! __impl_public_bitflags {
}
}
- __impl_public_bitflags_ops!($PublicBitFlags);
+ __impl_public_bitflags_ops!($BitFlags);
};
}
diff --git a/src/public.rs b/src/public.rs
--- a/src/public.rs
+++ b/src/public.rs
@@ -268,8 +268,8 @@ macro_rules! __impl_public_bitflags {
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __impl_public_bitflags_iter {
- ($PublicBitFlags:ident) => {
- impl $PublicBitFlags {
+ ($BitFlags:ident: $T:ty, $PublicBitFlags:ident) => {
+ impl $BitFlags {
/// Iterate over enabled flag values.
#[inline]
pub const fn iter(&self) -> $crate::iter::Iter<$PublicBitFlags> {
diff --git a/src/public.rs b/src/public.rs
--- a/src/public.rs
+++ b/src/public.rs
@@ -283,8 +283,8 @@ macro_rules! __impl_public_bitflags_iter {
}
}
- impl $crate::__private::core::iter::IntoIterator for $PublicBitFlags {
- type Item = Self;
+ impl $crate::__private::core::iter::IntoIterator for $BitFlags {
+ type Item = $PublicBitFlags;
type IntoIter = $crate::iter::Iter<$PublicBitFlags>;
fn into_iter(self) -> Self::IntoIter {
|
09f71f492d0f76d63cd286c3869c70676297e204
|
[
"tests/compile-pass/bitflags_self_in_value.rs",
"pass"
] |
[
"tests::bits_types",
"tests::into_iter_from_iter_roundtrip",
"tests::submodule::test_private",
"tests::test_assignment_operators",
"tests::test_binary",
"tests::test_bits",
"tests::test_const_fn",
"tests::test_contains",
"tests::test_debug",
"tests::test_default",
"tests::test_deprecated",
"tests::test_disjoint_intersects",
"tests::test_display_from_str_roundtrip",
"tests::test_empty_bitflags",
"tests::test_empty_does_not_intersect_with_full",
"tests::test_extend",
"tests::test_from_bits",
"tests::test_from_bits_edge_cases",
"tests::test_from_bits_retain",
"tests::test_from_bits_truncate",
"tests::test_from_bits_truncate_edge_cases",
"tests::test_from_iterator",
"tests::test_from_name",
"tests::test_from_str_err",
"tests::test_hash",
"tests::test_in_function",
"tests::test_is_all",
"tests::test_insert",
"tests::test_is_empty",
"tests::test_iter",
"tests::test_lowerhex",
"tests::test_lt",
"tests::test_octal",
"tests::test_operators",
"tests::test_operators_unchecked",
"tests::test_overlapping_intersects",
"tests::test_ord",
"tests::test_pub_crate",
"tests::test_pub_in_module",
"tests::test_public",
"tests::test_remove",
"tests::test_set",
"tests::test_set_ops_basic",
"tests::test_set_ops_const",
"tests::test_set_ops_unchecked",
"tests::test_two_empties_do_not_intersect",
"tests::test_u128_bitflags",
"tests::test_upperhex",
"tests::test_zero_value_flags",
"tests::test_set_ops_exhaustive",
"basic",
"tests/compile-pass/bitflags_nested_value.rs",
"tests/compile-pass/bitflags_redefined_value.rs",
"tests/compile-pass/bitflags_trait_bound_flags.rs",
"tests/compile-pass/bitflags_trait_generic_iter.rs",
"tests/compile-pass/bitflags_trait_precedence.rs",
"tests/compile-pass/bitflags_trait_supertrait.rs",
"tests/compile-pass/bitflags_trait_to_flags.rs",
"tests/compile-pass/bitflags_visibility.rs",
"tests/compile-pass/deprecated.rs",
"tests/compile-pass/doc_alias.rs",
"tests/compile-pass/flags_trait_bound_bitflags.rs",
"tests/compile-pass/flags_trait_generic_iter.rs",
"tests/compile-pass/flags_trait_precedence.rs",
"tests/compile-pass/flags_trait_supertrait.rs",
"tests/compile-pass/impl_convert_from_bits.rs",
"tests/compile-pass/impl_copy.rs",
"tests/compile-pass/impl_default.rs",
"tests/compile-pass/impl_eq.rs",
"tests/compile-pass/impl_fmt.rs",
"tests/compile-pass/impl_new.rs",
"tests/compile-pass/into_iter_trait_wrapped.rs",
"tests/compile-pass/item_positions.rs",
"tests/compile-pass/large.rs",
"tests/compile-pass/no_prelude.rs",
"tests/compile-pass/non_snake_case.rs",
"tests/compile-pass/path_based_bits.rs",
"tests/compile-pass/repr_c.rs",
"tests/compile-pass/repr_transparent.rs",
"tests/compile-pass/shadow_core.rs",
"tests/compile-pass/shadow_macros.rs",
"tests/compile-pass/shadow_result.rs",
"src/lib.rs - (line 414)",
"src/lib.rs - (line 225)",
"src/lib.rs - (line 86)",
"src/lib.rs - (line 19)",
"src/lib.rs - (line 50)",
"src/lib.rs - (line 280)",
"src/lib.rs - bitflags (line 529)",
"src/lib.rs - (line 325)",
"src/lib.rs - (line 385)",
"src/lib.rs - (line 360)",
"src/lib.rs - bitflags (line 505)",
"src/lib.rs - (line 246)",
"src/lib.rs - (line 105)"
] |
[] |
[] |
|
bitflags/bitflags
|
Bitflags reverses order of multiline doc comments
When compiling code like
```
bitflags! {
pub struct AdjustFlags: u32 {
/// Add buf.time to the current time. If buf.status includes the ADJ_NANO flag, then buf.time.tv_usec is interpreted as a nanosecond value;
/// otherwise it is interpreted as microseconds.
///
/// The value of buf.time is the sum of its two fields, but the field buf.time.tv_usec must always be nonnegative.
/// The following example shows how to normalize a timeval with nanosecond resolution.
///
/// ```C
/// while (buf.time.tv_usec < 0) {
/// buf.time.tv_sec -= 1;
/// buf.time.tv_usec += 1000000000;
/// }
/// ```
const SETOFFSET = libc::ADJ_SETOFFSET;
}
}
```
The doc-comments order is reversed on compile, causing issues with generated docs and the doctest.
This bug only occurs on bitflags 2.2.0 and not on earlier versions
|
This should be trivially fixed by swapping the order attributes are "pushed" in `__declare_bitflags`. We've already yanked `2.2.0` because it requires a lot more recursion, but will keep this open to make sure any new approach doesn't reverse the order of attributes.
|
bitflags__bitflags-345
|
[
"344"
] |
cbcafa710fc31172511e62efa06ad9eb214e4734
|
diff --git /dev/null b/tests/compile-pass/large.rs
new file mode 100644
--- /dev/null
+++ b/tests/compile-pass/large.rs
@@ -0,0 +1,311 @@
+/*
+Copyright (c) 2016 Anatoly Ikorsky
+
+Permission is hereby granted, free of charge, to any
+person obtaining a copy of this software and associated
+documentation files (the "Software"), to deal in the
+Software without restriction, including without
+limitation the rights to use, copy, modify, merge,
+publish, distribute, sublicense, and/or sell copies of
+the Software, and to permit persons to whom the Software
+is furnished to do so, subject to the following
+conditions:
+
+The above copyright notice and this permission notice
+shall be included in all copies or substantial portions
+of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
+ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
+TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
+PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
+SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
+IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
+DEALINGS IN THE SOFTWARE.
+*/
+
+#[macro_use]
+extern crate bitflags;
+
+bitflags! {
+ /// Client capability flags
+ #[derive(PartialEq, Eq, Hash, Debug, Clone, Copy)]
+ pub struct CapabilityFlags: u32 {
+ /// Use the improved version of Old Password Authentication. Assumed to be set since 4.1.1.
+ const CLIENT_LONG_PASSWORD = 0x0000_0001;
+
+ /// Send found rows instead of affected rows in EOF_Packet.
+ const CLIENT_FOUND_ROWS = 0x0000_0002;
+
+ /// Get all column flags.
+ /// Longer flags in Protocol::ColumnDefinition320.
+ ///
+ /// ### Server
+ /// Supports longer flags.
+ ///
+ /// ### Client
+ /// Expects longer flags.
+ const CLIENT_LONG_FLAG = 0x0000_0004;
+
+ /// Database (schema) name can be specified on connect in Handshake Response Packet.
+ /// ### Server
+ /// Supports schema-name in Handshake Response Packet.
+ ///
+ /// ### Client
+ /// Handshake Response Packet contains a schema-name.
+ const CLIENT_CONNECT_WITH_DB = 0x0000_0008;
+
+ /// Don't allow database.table.column.
+ const CLIENT_NO_SCHEMA = 0x0000_0010;
+
+ /// Compression protocol supported.
+ ///
+ /// ### Server
+ /// Supports compression.
+ ///
+ /// ### Client
+ /// Switches to Compression compressed protocol after successful authentication.
+ const CLIENT_COMPRESS = 0x0000_0020;
+
+ /// Special handling of ODBC behavior.
+ const CLIENT_ODBC = 0x0000_0040;
+
+ /// Can use LOAD DATA LOCAL.
+ ///
+ /// ### Server
+ /// Enables the LOCAL INFILE request of LOAD DATA|XML.
+ ///
+ /// ### Client
+ /// Will handle LOCAL INFILE request.
+ const CLIENT_LOCAL_FILES = 0x0000_0080;
+
+ /// Ignore spaces before '('.
+ ///
+ /// ### Server
+ /// Parser can ignore spaces before '('.
+ ///
+ /// ### Client
+ /// Let the parser ignore spaces before '('.
+ const CLIENT_IGNORE_SPACE = 0x0000_0100;
+
+ const CLIENT_PROTOCOL_41 = 0x0000_0200;
+
+ /// This is an interactive client.
+ /// Use System_variables::net_wait_timeout versus System_variables::net_interactive_timeout.
+ ///
+ /// ### Server
+ /// Supports interactive and noninteractive clients.
+ ///
+ /// ### Client
+ /// Client is interactive.
+ const CLIENT_INTERACTIVE = 0x0000_0400;
+
+ /// Use SSL encryption for the session.
+ ///
+ /// ### Server
+ /// Supports SSL
+ ///
+ /// ### Client
+ /// Switch to SSL after sending the capability-flags.
+ const CLIENT_SSL = 0x0000_0800;
+
+ /// Client only flag. Not used.
+ ///
+ /// ### Client
+ /// Do not issue SIGPIPE if network failures occur (libmysqlclient only).
+ const CLIENT_IGNORE_SIGPIPE = 0x0000_1000;
+
+ /// Client knows about transactions.
+ ///
+ /// ### Server
+ /// Can send status flags in OK_Packet / EOF_Packet.
+ ///
+ /// ### Client
+ /// Expects status flags in OK_Packet / EOF_Packet.
+ ///
+ /// ### Note
+ /// This flag is optional in 3.23, but always set by the server since 4.0.
+ const CLIENT_TRANSACTIONS = 0x0000_2000;
+
+ const CLIENT_RESERVED = 0x0000_4000;
+
+ const CLIENT_SECURE_CONNECTION = 0x0000_8000;
+
+ /// Enable/disable multi-stmt support.
+ /// Also sets CLIENT_MULTI_RESULTS. Currently not checked anywhere.
+ ///
+ /// ### Server
+ /// Can handle multiple statements per COM_QUERY and COM_STMT_PREPARE.
+ ///
+ /// ### Client
+ /// May send multiple statements per COM_QUERY and COM_STMT_PREPARE.
+ const CLIENT_MULTI_STATEMENTS = 0x0001_0000;
+
+ /// Enable/disable multi-results.
+ ///
+ /// ### Server
+ /// Can send multiple resultsets for COM_QUERY. Error if the server needs to send
+ /// them and client does not support them.
+ ///
+ /// ### Client
+ /// Can handle multiple resultsets for COM_QUERY.
+ ///
+ /// ### Requires
+ /// `CLIENT_PROTOCOL_41`
+ const CLIENT_MULTI_RESULTS = 0x0002_0000;
+
+ /// Multi-results and OUT parameters in PS-protocol.
+ ///
+ /// ### Server
+ /// Can send multiple resultsets for COM_STMT_EXECUTE.
+ ///
+ /// ### Client
+ /// Can handle multiple resultsets for COM_STMT_EXECUTE.
+ ///
+ /// ### Requires
+ /// `CLIENT_PROTOCOL_41`
+ const CLIENT_PS_MULTI_RESULTS = 0x0004_0000;
+
+ /// Client supports plugin authentication.
+ ///
+ /// ### Server
+ /// Sends extra data in Initial Handshake Packet and supports the pluggable
+ /// authentication protocol.
+ ///
+ /// ### Client
+ /// Supports authentication plugins.
+ ///
+ /// ### Requires
+ /// `CLIENT_PROTOCOL_41`
+ const CLIENT_PLUGIN_AUTH = 0x0008_0000;
+
+ /// Client supports connection attributes.
+ ///
+ /// ### Server
+ /// Permits connection attributes in Protocol::HandshakeResponse41.
+ ///
+ /// ### Client
+ /// Sends connection attributes in Protocol::HandshakeResponse41.
+ const CLIENT_CONNECT_ATTRS = 0x0010_0000;
+
+ /// Enable authentication response packet to be larger than 255 bytes.
+ /// When the ability to change default plugin require that the initial password
+ /// field in the Protocol::HandshakeResponse41 paclet can be of arbitrary size.
+ /// However, the 4.1 client-server protocol limits the length of the auth-data-field
+ /// sent from client to server to 255 bytes. The solution is to change the type of
+ /// the field to a true length encoded string and indicate the protocol change with
+ /// this client capability flag.
+ ///
+ /// ### Server
+ /// Understands length-encoded integer for auth response data in
+ /// Protocol::HandshakeResponse41.
+ ///
+ /// ### Client
+ /// Length of auth response data in Protocol::HandshakeResponse41 is a
+ /// length-encoded integer.
+ ///
+ /// ### Note
+ /// The flag was introduced in 5.6.6, but had the wrong value.
+ const CLIENT_PLUGIN_AUTH_LENENC_CLIENT_DATA = 0x0020_0000;
+
+ /// Don't close the connection for a user account with expired password.
+ ///
+ /// ### Server
+ /// Announces support for expired password extension.
+ ///
+ /// ### Client
+ /// Can handle expired passwords.
+ const CLIENT_CAN_HANDLE_EXPIRED_PASSWORDS = 0x0040_0000;
+
+ /// Capable of handling server state change information.
+ /// Its a hint to the server to include the state change information in OK_Packet.
+ ///
+ /// ### Server
+ /// Can set SERVER_SESSION_STATE_CHANGED in the SERVER_STATUS_flags_enum and send
+ /// Session State Information in a OK_Packet.
+ ///
+ /// ### Client
+ /// Expects the server to send Session State Information in a OK_Packet.
+ const CLIENT_SESSION_TRACK = 0x0080_0000;
+
+ /// Client no longer needs EOF_Packet and will use OK_Packet instead.
+ ///
+ /// ### Server
+ /// Can send OK after a Text Resultset.
+ ///
+ /// ### Client
+ /// Expects an OK_Packet (instead of EOF_Packet) after the resultset
+ /// rows of a Text Resultset.
+ ///
+ /// ### Background
+ /// To support CLIENT_SESSION_TRACK, additional information must be sent after all
+ /// successful commands. Although the OK_Packet is extensible, the EOF_Packet is
+ /// not due to the overlap of its bytes with the content of the Text Resultset Row.
+ ///
+ /// Therefore, the EOF_Packet in the Text Resultset is replaced with an OK_Packet.
+ /// EOF_Packet is deprecated as of MySQL 5.7.5.
+ const CLIENT_DEPRECATE_EOF = 0x0100_0000;
+
+ /// The client can handle optional metadata information in the resultset.
+ const CLIENT_OPTIONAL_RESULTSET_METADATA = 0x0200_0000;
+
+ /// Compression protocol extended to support zstd compression method.
+ ///
+ /// This capability flag is used to send zstd compression level between client and server
+ /// provided both client and server are enabled with this flag.
+ ///
+ /// # Server
+ ///
+ /// Server sets this flag when global variable protocol-compression-algorithms has zstd
+ /// in its list of supported values.
+ ///
+ /// # Client
+ ///
+ /// Client sets this flag when it is configured to use zstd compression method.
+ const CLIENT_ZSTD_COMPRESSION_ALGORITHM = 0x0400_0000;
+
+ /// Support optional extension for query parameters into the COM_QUERY
+ /// and COM_STMT_EXECUTE packets.
+ ///
+ /// # Server
+ ///
+ /// Expects an optional part containing the query parameter set(s).
+ /// Executes the query for each set of parameters or returns an error if more than 1 set
+ /// of parameters is sent and the server can't execute it.
+ ///
+ /// # Client
+ ///
+ /// Can send the optional part containing the query parameter set(s).
+ const CLIENT_QUERY_ATTRIBUTES = 0x0800_0000;
+
+ /// Support Multi factor authentication.
+ ///
+ /// # Server
+ ///
+ /// Server sends AuthNextFactor packet after every nth factor
+ /// authentication method succeeds, except the last factor authentication.
+ ///
+ /// # Client
+ ///
+ /// Client reads AuthNextFactor packet sent by server
+ /// and initiates next factor authentication method.
+ const MULTI_FACTOR_AUTHENTICATION = 0x1000_0000;
+
+ /// Client or server supports progress reports within error packet.
+ const CLIENT_PROGRESS_OBSOLETE = 0x2000_0000;
+
+ /// Verify server certificate. Client only flag.
+ ///
+ /// Deprecated in favor of –ssl-mode.
+ const CLIENT_SSL_VERIFY_SERVER_CERT = 0x4000_0000;
+
+ /// Don't reset the options after an unsuccessful connect. Client only flag.
+ const CLIENT_REMEMBER_OPTIONS = 0x8000_0000;
+ }
+}
+
+fn main() {
+
+}
|
2.2
| 345
|
2023-04-24T04:29:26Z
|
diff --git a/src/example_generated.rs b/src/example_generated.rs
--- a/src/example_generated.rs
+++ b/src/example_generated.rs
@@ -33,8 +33,17 @@ __impl_public_bitflags! {
__impl_public_bitflags_consts! {
Flags {
+ /// Field `A`.
+ ///
+ /// This flag has the value `0b00000001`.
A = 0b00000001;
+ /// Field `B`.
+ ///
+ /// This flag has the value `0b00000010`.
B = 0b00000010;
+ /// Field `C`.
+ ///
+ /// This flag has the value `0b00000100`.
C = 0b00000100;
ABC = Self::A.bits() | Self::B.bits() | Self::C.bits();
}
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -224,10 +224,14 @@ macro_rules! __impl_internal_bitflags {
let mut truncated = <$T as $crate::__private::Bits>::EMPTY;
$(
- $(#[$attr $($args)*])*
- if bits & $BitFlags::$Flag.bits() == $BitFlags::$Flag.bits() {
- truncated |= $BitFlags::$Flag.bits()
- }
+ __expr_safe_flags!(
+ $(#[$attr $($args)*])*
+ {
+ if bits & $BitFlags::$Flag.bits() == $BitFlags::$Flag.bits() {
+ truncated |= $BitFlags::$Flag.bits()
+ }
+ }
+ );
)*
Self { bits: truncated }
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -240,13 +244,19 @@ macro_rules! __impl_internal_bitflags {
#[inline]
pub fn from_name(name: &str) -> $crate::__private::core::option::Option<Self> {
- match name {
- $(
+ $(
+ __expr_safe_flags!(
$(#[$attr $($args)*])*
- $crate::__private::core::stringify!($Flag) => $crate::__private::core::option::Option::Some(Self { bits: $BitFlags::$Flag.bits() }),
- )*
- _ => $crate::__private::core::option::Option::None,
- }
+ {
+ if name == $crate::__private::core::stringify!($Flag) {
+ return $crate::__private::core::option::Option::Some(Self { bits: $BitFlags::$Flag.bits() });
+ }
+ }
+ );
+ )*
+
+ let _ = name;
+ $crate::__private::core::option::Option::None
}
#[inline]
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -384,10 +394,12 @@ macro_rules! __impl_internal_bitflags {
let mut num_flags = 0;
$(
- $(#[$attr $($args)*])*
- {
- num_flags += 1;
- }
+ __expr_safe_flags!(
+ $(#[$attr $($args)*])*
+ {
+ { num_flags += 1; }
+ }
+ );
)*
num_flags
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -395,15 +407,23 @@ macro_rules! __impl_internal_bitflags {
const OPTIONS: [$T; NUM_FLAGS] = [
$(
- $(#[$attr $($args)*])*
- $BitFlags::$Flag.bits(),
+ __expr_safe_flags!(
+ $(#[$attr $($args)*])*
+ {
+ $BitFlags::$Flag.bits()
+ }
+ ),
)*
];
const OPTIONS_NAMES: [&'static str; NUM_FLAGS] = [
$(
- $(#[$attr $($args)*])*
- $crate::__private::core::stringify!($Flag),
+ __expr_safe_flags!(
+ $(#[$attr $($args)*])*
+ {
+ $crate::__private::core::stringify!($Flag)
+ }
+ ),
)*
];
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -439,3 +459,112 @@ macro_rules! __impl_internal_bitflags {
}
};
}
+
+/// A macro that processed the input to `bitflags!` and shuffles attributes around
+/// based on whether or not they're "expression-safe".
+///
+/// This macro is a token-tree muncher that works on 2 levels:
+///
+/// For each attribute, we explicitly match on its identifier, like `cfg` to determine
+/// whether or not it should be considered expression-safe.
+///
+/// If you find yourself with an attribute that should be considered expression-safe
+/// and isn't, it can be added here.
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! __expr_safe_flags {
+ // Entrypoint: Move all flags and all attributes into `unprocessed` lists
+ // where they'll be munched one-at-a-time
+ (
+ $(#[$inner:ident $($args:tt)*])*
+ { $e:expr }
+ ) => {
+ __expr_safe_flags! {
+ expr: { $e },
+ attrs: {
+ // All attributes start here
+ unprocessed: [$(#[$inner $($args)*])*],
+ processed: {
+ // Attributes that are safe on expressions go here
+ expr: [],
+ },
+ },
+ }
+ };
+ // Process the next attribute on the current flag
+ // `cfg`: The next flag should be propagated to expressions
+ // NOTE: You can copy this rules block and replace `cfg` with
+ // your attribute name that should be considered expression-safe
+ (
+ expr: { $e:expr },
+ attrs: {
+ unprocessed: [
+ // cfg matched here
+ #[cfg $($args:tt)*]
+ $($attrs_rest:tt)*
+ ],
+ processed: {
+ expr: [$($expr:tt)*],
+ },
+ },
+ ) => {
+ __expr_safe_flags! {
+ expr: { $e },
+ attrs: {
+ unprocessed: [
+ $($attrs_rest)*
+ ],
+ processed: {
+ expr: [
+ $($expr)*
+ // cfg added here
+ #[cfg $($args)*]
+ ],
+ },
+ },
+ }
+ };
+ // Process the next attribute on the current flag
+ // `$other`: The next flag should not be propagated to expressions
+ (
+ expr: { $e:expr },
+ attrs: {
+ unprocessed: [
+ // $other matched here
+ #[$other:ident $($args:tt)*]
+ $($attrs_rest:tt)*
+ ],
+ processed: {
+ expr: [$($expr:tt)*],
+ },
+ },
+ ) => {
+ __expr_safe_flags! {
+ expr: { $e },
+ attrs: {
+ unprocessed: [
+ $($attrs_rest)*
+ ],
+ processed: {
+ expr: [
+ // $other not added here
+ $($expr)*
+ ],
+ },
+ },
+ }
+ };
+ // Once all attributes on all flags are processed, generate the actual code
+ (
+ expr: { $e:expr },
+ attrs: {
+ unprocessed: [],
+ processed: {
+ expr: [$(#[$expr:ident $($exprargs:tt)*])*],
+ },
+ },
+ ) => {
+ $(#[$expr $($exprargs)*])*
+ { $e }
+ }
+}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -558,315 +558,6 @@ macro_rules! bitflags {
}
$($t:tt)*
- ) => {
- __declare_bitflags!(
- $(#[$outer])*
- $vis struct $BitFlags: $T {
- $(
- $(#[$inner $($args)*])*
- const $Flag = $value;
- )*
- }
- );
-
- bitflags! {
- $($t)*
- }
- };
- () => {};
-}
-
-/// A macro that processed the input to `bitflags!` and shuffles attributes around
-/// based on whether or not they're "expression-safe".
-///
-/// This macro is a token-tree muncher that works on 2 levels:
-///
-/// 1. Each flag, like `#[cfg(true)] const A: 42`
-/// 2. Each attribute on that flag, like `#[cfg(true)]`
-///
-/// Flags and attributes start in an "unprocessed" list, and are shifted one token
-/// at a time into an appropriate processed list until the unprocessed lists are empty.
-///
-/// For each attribute, we explicitly match on its identifier, like `cfg` to determine
-/// whether or not it should be considered expression-safe.
-///
-/// If you find yourself with an attribute that should be considered expression-safe
-/// and isn't, it can be added here.
-#[macro_export(local_inner_macros)]
-#[doc(hidden)]
-macro_rules! __declare_bitflags {
- // Entrypoint: Move all flags and all attributes into `unprocessed` lists
- // where they'll be munched one-at-a-time
- (
- $(#[$outer:meta])*
- $vis:vis struct $BitFlags:ident: $T:ty {
- $(
- $(#[$inner:ident $($args:tt)*])*
- const $Flag:ident = $value:expr;
- )*
- }
- ) => {
- __declare_bitflags! {
- decl: {
- attrs: [$(#[$outer])*],
- vis: $vis,
- ident: $BitFlags,
- ty: $T,
- },
- flags: {
- // All flags start here
- unprocessed: [
- $(
- {
- ident: $Flag,
- value: $value,
- attrs: {
- // All attributes start here
- unprocessed: [$(#[$inner $($args)*])*],
- processed: {
- // Attributes that should be added to item declarations go here
- decl: [],
- // Attributes that are safe on expressions go here
- expr: [],
- }
- },
- },
- )*
- ],
- // Flags that have had their attributes sorted are pushed here
- processed: [],
- }
- }
- };
- // Process the next attribute on the current flag
- // `cfg`: The next flag should be propagated to expressions
- // NOTE: You can copy this rules block and replace `cfg` with
- // your attribute name that should be considered expression-safe
- (
- decl: {
- attrs: [$(#[$outer:meta])*],
- vis: $vis:vis,
- ident: $BitFlags:ident,
- ty: $T:ty,
- },
- flags: {
- unprocessed: [
- {
- ident: $Flag:ident,
- value: $value:expr,
- attrs: {
- unprocessed: [
- // cfg matched here
- #[cfg $($args:tt)*]
- $($attrs_rest:tt)*
- ],
- processed: {
- decl: [$($decl:tt)*],
- expr: [$($expr:tt)*],
- }
- },
- },
- $($flags_rest:tt)*
- ],
- processed: [
- $($flags:tt)*
- ],
- }
- ) => {
- __declare_bitflags! {
- decl: {
- attrs: [$(#[$outer])*],
- vis: $vis,
- ident: $BitFlags,
- ty: $T,
- },
- flags: {
- unprocessed: [
- {
- ident: $Flag,
- value: $value,
- attrs: {
- unprocessed: [
- $($attrs_rest)*
- ],
- processed: {
- decl: [
- // cfg added here
- #[cfg $($args)*]
- $($decl)*
- ],
- expr: [
- // cfg added here
- #[cfg $($args)*]
- $($expr)*
- ],
- }
- },
- },
- $($flags_rest)*
- ],
- processed: [
- $($flags)*
- ],
- }
- }
- };
- // Process the next attribute on the current flag
- // `$other`: The next flag should not be propagated to expressions
- (
- decl: {
- attrs: [$(#[$outer:meta])*],
- vis: $vis:vis,
- ident: $BitFlags:ident,
- ty: $T:ty,
- },
- flags: {
- unprocessed: [
- {
- ident: $Flag:ident,
- value: $value:expr,
- attrs: {
- unprocessed: [
- // $other matched here
- #[$other:ident $($args:tt)*]
- $($attrs_rest:tt)*
- ],
- processed: {
- decl: [$($decl:tt)*],
- expr: [$($expr:tt)*],
- }
- },
- },
- $($flags_rest:tt)*
- ],
- processed: [
- $($flags:tt)*
- ],
- }
- ) => {
- __declare_bitflags! {
- decl: {
- attrs: [$(#[$outer])*],
- vis: $vis,
- ident: $BitFlags,
- ty: $T,
- },
- flags: {
- unprocessed: [
- {
- ident: $Flag,
- value: $value,
- attrs: {
- unprocessed: [
- $($attrs_rest)*
- ],
- processed: {
- decl: [
- // $other added here
- #[$other $($args)*]
- $($decl)*
- ],
- expr: [
- // $other not added here
- $($expr)*
- ],
- }
- },
- },
- $($flags_rest)*
- ],
- processed: [
- $($flags)*
- ],
- }
- }
- };
- // Complete the current flag once there are no unprocessed attributes left
- (
- decl: {
- attrs: [$(#[$outer:meta])*],
- vis: $vis:vis,
- ident: $BitFlags:ident,
- ty: $T:ty,
- },
- flags: {
- unprocessed: [
- {
- ident: $Flag:ident,
- value: $value:expr,
- attrs: {
- unprocessed: [],
- processed: {
- decl: [$($decl:tt)*],
- expr: [$($expr:tt)*],
- }
- },
- },
- $($flags_rest:tt)*
- ],
- processed: [
- $($flags:tt)*
- ],
- }
- ) => {
- __declare_bitflags! {
- decl: {
- attrs: [$(#[$outer])*],
- vis: $vis,
- ident: $BitFlags,
- ty: $T,
- },
- flags: {
- unprocessed: [
- $($flags_rest)*
- ],
- processed: [
- $($flags)*
- {
- ident: $Flag,
- value: $value,
- attrs: {
- unprocessed: [],
- processed: {
- decl: [
- $($decl)*
- ],
- expr: [
- $($expr)*
- ],
- }
- },
- },
- ],
- }
- }
- };
- // Once all attributes on all flags are processed, generate the actual code
- (
- decl: {
- attrs: [$(#[$outer:meta])*],
- vis: $vis:vis,
- ident: $BitFlags:ident,
- ty: $T:ty,
- },
- flags: {
- unprocessed: [],
- processed: [
- $(
- {
- ident: $Flag:ident,
- value: $value:expr,
- attrs: {
- unprocessed: [],
- processed: {
- decl: [$(#[$decl:ident $($declargs:tt)*])*],
- expr: [$(#[$expr:ident $($exprargs:tt)*])*],
- }
- },
- },
- )*
- ],
- }
) => {
// Declared in the scope of the `bitflags!` call
// This type appears in the end-user's API
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -879,7 +570,7 @@ macro_rules! __declare_bitflags {
__impl_public_bitflags_consts! {
$BitFlags {
$(
- $(#[$decl $($declargs)*])*
+ $(#[$inner $($args)*])*
#[allow(
dead_code,
deprecated,
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -912,7 +603,7 @@ macro_rules! __declare_bitflags {
__impl_internal_bitflags! {
InternalBitFlags: $T, $BitFlags, Iter, IterRaw {
$(
- $(#[$expr $($exprargs)*])*
+ $(#[$inner $($args)*])*
$Flag;
)*
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -922,7 +613,7 @@ macro_rules! __declare_bitflags {
__impl_external_bitflags! {
InternalBitFlags: $T {
$(
- $(#[$expr $($exprargs)*])*
+ $(#[$inner $($args)*])*
$Flag;
)*
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -932,7 +623,12 @@ macro_rules! __declare_bitflags {
$BitFlags: $T, InternalBitFlags, Iter, IterRaw;
}
};
- }
+
+ bitflags! {
+ $($t)*
+ }
+ };
+ () => {};
}
#[macro_use]
|
cbcafa710fc31172511e62efa06ad9eb214e4734
|
[
"tests/compile-pass/large.rs",
"pass"
] |
[
"tests::bits_types",
"tests::submodule::test_private",
"tests::into_iter_from_iter_roundtrip",
"tests::test_assignment_operators",
"tests::test_binary",
"tests::test_bits",
"tests::test_const_fn",
"tests::test_contains",
"tests::test_debug",
"tests::test_default",
"tests::test_deprecated",
"tests::test_disjoint_intersects",
"tests::test_display_from_str_roundtrip",
"tests::test_empty_bitflags",
"tests::test_empty_does_not_intersect_with_full",
"tests::test_extend",
"tests::test_from_bits",
"tests::test_from_bits_edge_cases",
"tests::test_from_bits_retain",
"tests::test_from_bits_truncate",
"tests::test_from_bits_truncate_edge_cases",
"tests::test_from_iterator",
"tests::test_from_name",
"tests::test_from_str_err",
"tests::test_hash",
"tests::test_in_function",
"tests::test_insert",
"tests::test_is_all",
"tests::test_is_empty",
"tests::test_iter",
"tests::test_lowerhex",
"tests::test_lt",
"tests::test_octal",
"tests::test_operators",
"tests::test_operators_unchecked",
"tests::test_ord",
"tests::test_overlapping_intersects",
"tests::test_pub_crate",
"tests::test_pub_in_module",
"tests::test_public",
"tests::test_remove",
"tests::test_set",
"tests::test_set_ops_basic",
"tests::test_set_ops_const",
"tests::test_set_ops_unchecked",
"tests::test_two_empties_do_not_intersect",
"tests::test_u128_bitflags",
"tests::test_upperhex",
"tests::test_zero_value_flags",
"tests::test_set_ops_exhaustive",
"basic",
"tests/compile-pass/cfg/nested-value.rs",
"tests/compile-pass/cfg/redefined-value.rs",
"tests/compile-pass/deprecated.rs",
"tests/compile-pass/doc_alias.rs",
"tests/compile-pass/impls/convert.rs",
"tests/compile-pass/impls/copy.rs",
"tests/compile-pass/impls/default.rs",
"tests/compile-pass/impls/eq.rs",
"tests/compile-pass/impls/fmt.rs",
"tests/compile-pass/impls/inherent_methods.rs",
"tests/compile-pass/item_positions.rs",
"tests/compile-pass/no_prelude.rs",
"tests/compile-pass/non_snake_case.rs",
"tests/compile-pass/path_based_bits.rs",
"tests/compile-pass/redefinition/core.rs",
"tests/compile-pass/redefinition/macros.rs",
"tests/compile-pass/redefinition/result.rs",
"tests/compile-pass/repr/c.rs",
"tests/compile-pass/repr/transparent.rs",
"tests/compile-pass/trait/generic_iter.rs",
"tests/compile-pass/trait/precedence.rs",
"tests/compile-pass/trait/wrapped_iter.rs",
"tests/compile-pass/visibility/bits_field.rs",
"tests/compile-pass/visibility/pub_in.rs",
"src/lib.rs - (line 414)",
"src/lib.rs - (line 360)",
"src/lib.rs - (line 280)",
"src/lib.rs - (line 325)",
"src/lib.rs - (line 105)",
"src/lib.rs - (line 385)",
"src/lib.rs - (line 86)",
"src/lib.rs - (line 19)",
"src/lib.rs - bitflags (line 498)",
"src/lib.rs - (line 246)",
"src/lib.rs - (line 225)",
"src/lib.rs - bitflags (line 522)",
"src/lib.rs - (line 50)"
] |
[] |
[] |
bitflags/bitflags
|
Display missing extra bits for multi-bit flags
See: https://github.com/bitflags/bitflags/issues/310#issuecomment-1470122112
Given a flags type with two flags, `BIT = 0b0000_0001` and `MASK = 0b0001_1110`, formatting the value `3` should result in `BIT | 0x2`, but instead it gives `BIT`. That extra bit gets lost, so doesn't roundtrip. The problem seems to be in the generated `iter_names` method.
|
bitflags__bitflags-316
|
[
"315"
] |
ad0271116e28a79ea880334dd3d06212a224ec09
|
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1154,17 +1154,39 @@ mod tests {
#[test]
fn test_display_from_str_roundtrip() {
- fn format_parse_case(flags: FmtFlags) {
+ fn format_parse_case<T: fmt::Debug + fmt::Display + str::FromStr + PartialEq>(flags: T) where <T as str::FromStr>::Err: fmt::Display {
assert_eq!(flags, {
- match flags.to_string().parse::<FmtFlags>() {
+ match flags.to_string().parse::<T>() {
Ok(flags) => flags,
Err(e) => panic!("failed to parse `{}`: {}", flags, e),
}
});
}
- fn parse_case(expected: FmtFlags, flags: &str) {
- assert_eq!(expected, flags.parse::<FmtFlags>().unwrap());
+ fn parse_case<T: fmt::Debug + str::FromStr + PartialEq>(expected: T, flags: &str) where <T as str::FromStr>::Err: fmt::Display + fmt::Debug {
+ assert_eq!(expected, flags.parse::<T>().unwrap());
+ }
+
+ bitflags! {
+ #[derive(Debug, Eq, PartialEq)]
+ pub struct MultiBitFmtFlags: u8 {
+ const A = 0b0000_0001u8;
+ const B = 0b0001_1110u8;
+ }
+ }
+
+ impl fmt::Display for MultiBitFmtFlags {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ fmt::Display::fmt(&self.0, f)
+ }
+ }
+
+ impl str::FromStr for MultiBitFmtFlags {
+ type Err = crate::parser::ParseError;
+
+ fn from_str(s: &str) -> Result<Self, Self::Err> {
+ Ok(MultiBitFmtFlags(s.parse()?))
+ }
}
format_parse_case(FmtFlags::empty());
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1174,6 +1196,7 @@ mod tests {
format_parse_case(FmtFlags::물고기_고양이);
format_parse_case(FmtFlags::from_bits_retain(0xb8));
format_parse_case(FmtFlags::from_bits_retain(0x20));
+ format_parse_case(MultiBitFmtFlags::from_bits_retain(3));
parse_case(FmtFlags::empty(), "");
parse_case(FmtFlags::empty(), " \r\n\t");
|
2.0
| 316
|
2023-03-16T00:19:13Z
|
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -89,7 +89,8 @@ macro_rules! __impl_internal_bitflags {
// Iterate over the valid flags
let mut first = true;
- for (name, _) in self.iter_names() {
+ let mut iter = self.iter_names();
+ for (name, _) in &mut iter {
if !first {
f.write_str(" | ")?;
}
diff --git a/src/internal.rs b/src/internal.rs
--- a/src/internal.rs
+++ b/src/internal.rs
@@ -99,8 +100,7 @@ macro_rules! __impl_internal_bitflags {
}
// Append any extra bits that correspond to flags to the end of the format
- let extra_bits = self.bits & !Self::all().bits;
-
+ let extra_bits = iter.state.bits();
if extra_bits != <$T as $crate::__private::Bits>::EMPTY {
if !first {
f.write_str(" | ")?;
|
11640f19a7644f3967631733f33ec87b9f911951
|
[
"tests::test_display_from_str_roundtrip"
] |
[
"tests::into_iter_from_iter_roundtrip",
"tests::test_assignment_operators",
"tests::submodule::test_private",
"tests::test_binary",
"tests::test_bits",
"tests::test_const_fn",
"tests::test_contains",
"tests::test_debug",
"tests::test_default",
"tests::test_deprecated",
"tests::test_disjoint_intersects",
"tests::test_empty_bitflags",
"tests::test_empty_does_not_intersect_with_full",
"tests::test_from_bits",
"tests::test_extend",
"tests::test_from_bits_edge_cases",
"tests::test_from_bits_retain",
"tests::test_from_bits_truncate",
"tests::test_from_bits_truncate_edge_cases",
"tests::test_from_iterator",
"tests::test_from_name",
"tests::test_from_str_err",
"tests::test_hash",
"tests::test_in_function",
"tests::test_insert",
"tests::test_is_all",
"tests::test_is_empty",
"tests::test_iter",
"tests::test_lowerhex",
"tests::test_lt",
"tests::test_octal",
"tests::test_operators",
"tests::test_operators_unchecked",
"tests::test_ord",
"tests::test_overlapping_intersects",
"tests::test_pub_crate",
"tests::test_pub_in_module",
"tests::test_public",
"tests::test_remove",
"tests::test_set",
"tests::test_set_ops_basic",
"tests::test_set_ops_const",
"tests::test_set_ops_unchecked",
"tests::test_two_empties_do_not_intersect",
"tests::test_u128_bitflags",
"tests::test_upperhex",
"tests::test_zero_value_flags",
"tests::test_set_ops_exhaustive",
"basic",
"tests/compile-pass/cfg/nested-value.rs",
"tests/compile-pass/cfg/redefined-value.rs",
"tests/compile-pass/deprecated.rs",
"tests/compile-pass/impls/convert.rs",
"tests/compile-pass/impls/copy.rs",
"tests/compile-pass/impls/default.rs",
"tests/compile-pass/impls/eq.rs",
"tests/compile-pass/impls/fmt.rs",
"tests/compile-pass/impls/inherent_methods.rs",
"tests/compile-pass/item_positions.rs",
"tests/compile-pass/no_prelude.rs",
"tests/compile-pass/non_snake_case.rs",
"tests/compile-pass/path_based_bits.rs",
"tests/compile-pass/redefinition/core.rs",
"tests/compile-pass/redefinition/macros.rs",
"tests/compile-pass/redefinition/result.rs",
"tests/compile-pass/repr/c.rs",
"tests/compile-pass/repr/transparent.rs",
"tests/compile-pass/trait/generic_iter.rs",
"tests/compile-pass/trait/precedence.rs",
"tests/compile-pass/trait/wrapped_iter.rs",
"tests/compile-pass/visibility/bits_field.rs",
"tests/compile-pass/visibility/pub_in.rs",
"pass",
"src/lib.rs - (line 288)",
"src/lib.rs - (line 225)",
"src/lib.rs - (line 246)",
"src/lib.rs - (line 19)",
"src/lib.rs - (line 50)",
"src/lib.rs - (line 321)",
"src/lib.rs - bitflags (line 423)",
"src/lib.rs - (line 86)",
"src/lib.rs - bitflags (line 447)",
"src/lib.rs - (line 105)"
] |
[] |
[] |
|
bitflags/bitflags
|
Debug formatting leads to less desireable output
[Link to rust playground](https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&code=%23%5Bmacro_use%5D%0Aextern%20crate%20bitflags%3B%0A%0Abitflags!%20%7B%0A%20%20%20%20struct%20Flags%3A%20u32%20%7B%0A%20%20%20%20%20%20%20%20const%20A%20%3D%200b00000001%3B%0A%20%20%20%20%20%20%20%20const%20B%20%3D%200b00000010%3B%0A%20%20%20%20%20%20%20%20const%20C%20%3D%200b00000100%3B%0A%20%20%20%20%20%20%20%20const%20ABC%20%3D%20Self%3A%3AA.bits%20%7C%20Self%3A%3AB.bits%20%7C%20Self%3A%3AC.bits%3B%0A%20%20%20%20%7D%0A%7D%0A%0Afn%20main()%20%7B%0A%20%20%20%20println!(%22%7B%3A%3F%7D%22%2C%20Flags%3A%3AA%20%7C%20Flags%3A%3AB%20%7C%20Flags%3A%3AC%20)%3B%0A%7D)
```rust
#[macro_use]
extern crate bitflags;
bitflags! {
struct Flags: u32 {
const A = 0b00000001;
const B = 0b00000010;
const C = 0b00000100;
const ABC = Self::A.bits | Self::B.bits | Self::C.bits;
}
}
fn main() {
println!("{:?}", Flags::A | Flags::B | Flags::C );
}
```
prints:
```bash
A | B | C | ABC
```
I find it somewhat less helpful that both the expanded (`A | B | C`) and "compressed" form (`ABC`) are reported...
Is there a reason behind this? Is this considered more correct for some reason?
|
I think the current algorithm used for debug output is to loop over all flags and append identifiers that correspond to set bits. I think an alternative here that could work would be to short-circuit when we’ve built a format that covers all the set bits. As an implementation note we wouldn’t be able to work off a single source and just mask out bits as we see them, we’ll need to use the whole set of bits to see if a flag is applicable and then a second set that’s masked to see when we’ve catered for all set bits. Otherwise something like this wouldn’t work:
```
const A: 0b00000100
const B: 0b00001100
let input = B;
```
we’d mask out bit 3 for `A` but then have one leftover for `B` that doesn’t correspond to it. In that example we’d end up writing `A | B` still, but wouldn’t duplicate compound identifiers the same.
In general, I’m not sure if there’s a reasonable algorithm that would produce the smallest possible format for any given set of bits.
|
bitflags__bitflags-281
|
[
"215"
] |
f38ce72d11ef3e264d4b62f360bd8a5597b916d9
|
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1353,7 +1321,10 @@ mod tests {
assert_eq!(UNION, Flags::A | Flags::C);
assert_eq!(DIFFERENCE, Flags::all() - Flags::A);
assert_eq!(COMPLEMENT, !Flags::C);
- assert_eq!(SYM_DIFFERENCE, (Flags::A | Flags::C) ^ (Flags::all() - Flags::A));
+ assert_eq!(
+ SYM_DIFFERENCE,
+ (Flags::A | Flags::C) ^ (Flags::all() - Flags::A)
+ );
}
#[test]
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1609,13 +1580,15 @@ mod tests {
assert_eq!(format!("{:?}", Flags::A | Flags::B), "A | B");
assert_eq!(format!("{:?}", Flags::empty()), "(empty)");
assert_eq!(format!("{:?}", Flags::ABC), "A | B | C");
+
let extra = unsafe { Flags::from_bits_unchecked(0xb8) };
+
assert_eq!(format!("{:?}", extra), "0xb8");
assert_eq!(format!("{:?}", Flags::A | extra), "A | 0xb8");
assert_eq!(
format!("{:?}", Flags::ABC | extra),
- "A | B | C | 0xb8"
+ "A | B | C | ABC | 0xb8"
);
assert_eq!(format!("{:?}", EmptyFlags::empty()), "(empty)");
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1830,7 +1803,8 @@ mod tests {
fn test_serde_bitflags_roundtrip() {
let flags = SerdeFlags::A | SerdeFlags::B;
- let deserialized: SerdeFlags = serde_json::from_str(&serde_json::to_string(&flags).unwrap()).unwrap();
+ let deserialized: SerdeFlags =
+ serde_json::from_str(&serde_json::to_string(&flags).unwrap()).unwrap();
assert_eq!(deserialized.bits, flags.bits);
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1875,7 +1848,7 @@ mod tests {
let flags = Flags::from_bits_truncate(0b00000101);
assert_eq!(flags, Flags::A);
}
-
+
#[test]
fn test_iter() {
bitflags! {
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1887,24 +1860,31 @@ mod tests {
const FOUR_WIN = 0b1000;
#[cfg(unix)]
const FOUR_UNIX = 0b10000;
+ const FIVE = 0b01000100;
}
}
let count = {
#[cfg(any(unix, windows))]
{
- 4
+ 5
}
#[cfg(not(any(unix, windows)))]
{
- 3
+ 4
}
};
let flags = Flags::all();
assert_eq!(flags.iter().count(), count);
+
+ for (_, flag) in flags.iter() {
+ assert!(flags.contains(flag));
+ }
+
let mut iter = flags.iter();
+
assert_eq!(iter.next().unwrap(), ("ONE", Flags::ONE));
assert_eq!(iter.next().unwrap(), ("TWO", Flags::TWO));
assert_eq!(iter.next().unwrap(), ("THREE", Flags::THREE));
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1918,6 +1898,8 @@ mod tests {
assert_eq!(iter.next().unwrap(), ("FOUR_WIN", Flags::FOUR_WIN));
}
+ assert_eq!(iter.next().unwrap(), ("FIVE", Flags::FIVE));
+
assert_eq!(iter.next(), None);
let flags = Flags::empty();
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1925,7 +1907,9 @@ mod tests {
let flags = Flags::ONE | Flags::THREE;
assert_eq!(flags.iter().count(), 2);
+
let mut iter = flags.iter();
+
assert_eq!(iter.next().unwrap(), ("ONE", Flags::ONE));
assert_eq!(iter.next().unwrap(), ("THREE", Flags::THREE));
assert_eq!(iter.next(), None);
|
1.3
| 281
|
2022-05-03T06:59:46Z
|
diff --git a/src/bitflags_trait.rs b/src/bitflags_trait.rs
--- a/src/bitflags_trait.rs
+++ b/src/bitflags_trait.rs
@@ -1,3 +1,5 @@
+use core::ops::{BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not};
+
#[doc(hidden)]
pub trait ImplementedByBitFlagsMacro {}
diff --git a/src/bitflags_trait.rs b/src/bitflags_trait.rs
--- a/src/bitflags_trait.rs
+++ b/src/bitflags_trait.rs
@@ -5,7 +7,8 @@ pub trait ImplementedByBitFlagsMacro {}
///
/// It should not be implemented manually.
pub trait BitFlags: ImplementedByBitFlagsMacro {
- type Bits;
+ type Bits: Bits;
+
/// Returns an empty set of flags.
fn empty() -> Self;
/// Returns the set containing all flags.
diff --git a/src/bitflags_trait.rs b/src/bitflags_trait.rs
--- a/src/bitflags_trait.rs
+++ b/src/bitflags_trait.rs
@@ -15,7 +18,8 @@ pub trait BitFlags: ImplementedByBitFlagsMacro {
/// Convert from underlying bit representation, unless that
/// representation contains bits that do not correspond to a flag.
fn from_bits(bits: Self::Bits) -> Option<Self>
- where Self: Sized;
+ where
+ Self: Sized;
/// Convert from underlying bit representation, dropping any bits
/// that do not correspond to flags.
fn from_bits_truncate(bits: Self::Bits) -> Self;
diff --git a/src/bitflags_trait.rs b/src/bitflags_trait.rs
--- a/src/bitflags_trait.rs
+++ b/src/bitflags_trait.rs
@@ -48,3 +52,58 @@ pub trait BitFlags: ImplementedByBitFlagsMacro {
/// Inserts or removes the specified flags depending on the passed value.
fn set(&mut self, other: Self, value: bool);
}
+
+// Not re-exported
+pub trait Sealed {}
+
+/// A private trait that encodes the requirements of underlying bits types that can hold flags.
+///
+/// This trait may be made public at some future point, but it presents a compatibility hazard
+/// so is left internal for now.
+#[doc(hidden)]
+pub trait Bits:
+ Clone
+ + Copy
+ + BitAnd
+ + BitAndAssign
+ + BitOr
+ + BitOrAssign
+ + BitXor
+ + BitXorAssign
+ + Not
+ + Sized
+ + Sealed
+{
+ /// The value of `Self` where no bits are set.
+ const EMPTY: Self;
+
+ /// The value of `Self` where all bits are set.
+ const ALL: Self;
+}
+
+macro_rules! impl_bits {
+ ($($u:ty, $i:ty,)*) => {
+ $(
+ impl Bits for $u {
+ const EMPTY: $u = 0;
+ const ALL: $u = <$u>::MAX;
+ }
+
+ impl Bits for $i {
+ const EMPTY: $i = 0;
+ const ALL: $i = <$u>::MAX as $i;
+ }
+
+ impl Sealed for $u {}
+ impl Sealed for $i {}
+ )*
+ }
+}
+
+impl_bits! {
+ u8, i8,
+ u16, i16,
+ u32, i32,
+ u64, i64,
+ u128, i128,
+}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -285,8 +285,8 @@ mod bitflags_trait;
#[doc(hidden)]
pub mod __private {
+ pub use crate::bitflags_trait::{Bits, ImplementedByBitFlagsMacro};
pub use core;
- pub use crate::bitflags_trait::ImplementedByBitFlagsMacro;
}
/// The macro used to generate the flag structure.
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -389,45 +389,6 @@ macro_rules! bitflags {
() => {};
}
-// A helper macro to implement the `all` function.
-#[macro_export(local_inner_macros)]
-#[doc(hidden)]
-macro_rules! __impl_all_bitflags {
- (
- $BitFlags:ident: $T:ty {
- $(
- $(#[$attr:ident $($args:tt)*])*
- $Flag:ident = $value:expr;
- )+
- }
- ) => {
- // See `Debug::fmt` for why this approach is taken.
- #[allow(non_snake_case)]
- trait __BitFlags {
- $(
- #[allow(deprecated)]
- const $Flag: $T = 0;
- )+
- }
- #[allow(non_snake_case)]
- impl __BitFlags for $BitFlags {
- $(
- __impl_bitflags! {
- #[allow(deprecated)]
- $(? #[$attr $($args)*])*
- const $Flag: $T = Self::$Flag.bits;
- }
- )+
- }
- Self { bits: $(<Self as __BitFlags>::$Flag)|+ }
- };
- (
- $BitFlags:ident: $T:ty { }
- ) => {
- Self { bits: 0 }
- };
-}
-
#[macro_export(local_inner_macros)]
#[doc(hidden)]
macro_rules! __impl_bitflags {
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -455,7 +416,7 @@ macro_rules! __impl_bitflags {
// Append any extra bits that correspond to flags to the end of the format
let extra_bits = self.bits & !Self::all().bits();
- if extra_bits != 0 {
+ if extra_bits != <$T as $crate::__private::Bits>::EMPTY {
if !first {
f.write_str(" | ")?;
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -495,7 +456,14 @@ macro_rules! __impl_bitflags {
}
}
- #[allow(dead_code)]
+ #[allow(
+ dead_code,
+ deprecated,
+ unused_doc_comments,
+ unused_attributes,
+ unused_mut,
+ non_upper_case_globals
+ )]
impl $BitFlags {
$(
$(#[$attr $($args)*])*
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -505,20 +473,13 @@ macro_rules! __impl_bitflags {
/// Returns an empty set of flags.
#[inline]
pub const fn empty() -> Self {
- Self { bits: 0 }
+ Self { bits: <$T as $crate::__private::Bits>::EMPTY }
}
/// Returns the set containing all flags.
#[inline]
pub const fn all() -> Self {
- __impl_all_bitflags! {
- $BitFlags: $T {
- $(
- $(#[$attr $($args)*])*
- $Flag = $value;
- )*
- }
- }
+ Self::from_bits_truncate(<$T as $crate::__private::Bits>::ALL)
}
/// Returns the raw value of the flags currently stored.
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -532,8 +493,9 @@ macro_rules! __impl_bitflags {
#[inline]
pub const fn from_bits(bits: $T) -> $crate::__private::core::option::Option<Self> {
let truncated = Self::from_bits_truncate(bits).bits;
+
if truncated == bits {
- $crate::__private::core::option::Option::Some(Self{ bits })
+ $crate::__private::core::option::Option::Some(Self { bits })
} else {
$crate::__private::core::option::Option::None
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -543,15 +505,13 @@ macro_rules! __impl_bitflags {
/// that do not correspond to flags.
#[inline]
pub const fn from_bits_truncate(bits: $T) -> Self {
- if bits == 0 {
+ if bits == <$T as $crate::__private::Bits>::EMPTY {
return Self { bits }
}
- #[allow(unused_mut)]
- let mut truncated = 0;
+ let mut truncated = <$T as $crate::__private::Bits>::EMPTY;
$(
- #[allow(unused_doc_comments, unused_attributes)]
$(#[$attr $($args)*])*
if bits & Self::$Flag.bits == Self::$Flag.bits {
truncated |= Self::$Flag.bits
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -719,15 +679,13 @@ macro_rules! __impl_bitflags {
}
/// Returns an iterator over set flags and their names.
- pub fn iter(mut self) -> impl $crate::__private::core::iter::Iterator<Item = (&'static str, Self)> {
+ pub fn iter(self) -> impl $crate::__private::core::iter::Iterator<Item = (&'static str, Self)> {
use $crate::__private::core::iter::Iterator as _;
const NUM_FLAGS: usize = {
- #[allow(unused_mut)]
let mut num_flags = 0;
$(
- #[allow(unused_doc_comments, unused_attributes)]
$(#[$attr $($args)*])*
{
num_flags += 1;
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -739,13 +697,11 @@ macro_rules! __impl_bitflags {
const OPTIONS: [$BitFlags; NUM_FLAGS] = [
$(
- #[allow(unused_doc_comments, unused_attributes)]
$(#[$attr $($args)*])*
$BitFlags::$Flag,
)*
];
- #[allow(unused_doc_comments, unused_attributes)]
const OPTIONS_NAMES: [&'static str; NUM_FLAGS] = [
$(
$(#[$attr $($args)*])*
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -754,17 +710,29 @@ macro_rules! __impl_bitflags {
];
let mut start = 0;
+ let mut state = self;
$crate::__private::core::iter::from_fn(move || {
- if self.is_empty() || NUM_FLAGS == 0 {
+ if state.is_empty() || NUM_FLAGS == 0 {
$crate::__private::core::option::Option::None
} else {
for (flag, flag_name) in OPTIONS[start..NUM_FLAGS].iter().copied()
.zip(OPTIONS_NAMES[start..NUM_FLAGS].iter().copied())
{
start += 1;
+
+ // NOTE: We check whether the flag exists in self, but remove it from
+ // a different value. This ensure that overlapping flags are handled
+ // properly. Take the following example:
+ //
+ // const A: 0b00000001;
+ // const B: 0b00000101;
+ //
+ // Given the bits 0b00000101, both A and B are set. But if we removed A
+ // as we encountered it we'd be left with 0b00000100, which doesn't
+ // correspond to a valid flag on its own.
if self.contains(flag) {
- self.remove(flag);
+ state.remove(flag);
return $crate::__private::core::option::Option::Some((flag_name, flag))
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1854,7 +1828,6 @@ mod tests {
}
}
-
let flags = Flags::from_bits(0b00000100);
assert_eq!(flags, None);
let flags = Flags::from_bits(0b00000101);
diff --git a/tests/compile-fail/cfg/multi.stderr /dev/null
--- a/tests/compile-fail/cfg/multi.stderr
+++ /dev/null
@@ -1,17 +0,0 @@
-error[E0428]: the name `FOO` is defined multiple times
- --> tests/compile-fail/cfg/multi.rs:6:1
- |
-6 | / bitflags! {
-7 | | pub struct Flags: u32 {
-8 | | #[cfg(target_os = "linux")]
-9 | | const FOO = 1;
-... |
-12 | | }
-13 | | }
- | | ^
- | | |
- | |_`FOO` redefined here
- | previous definition of the value `FOO` here
- |
- = note: `FOO` must be defined only once in the value namespace of this trait
- = note: this error originates in the macro `__impl_all_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
diff --git a/tests/compile-fail/non_integer_base/all_defined.stderr b/tests/compile-fail/non_integer_base/all_defined.stderr
--- a/tests/compile-fail/non_integer_base/all_defined.stderr
+++ b/tests/compile-fail/non_integer_base/all_defined.stderr
@@ -1,22 +1,16 @@
-error[E0308]: mismatched types
- --> tests/compile-fail/non_integer_base/all_defined.rs:115:1
+error[E0277]: the trait bound `MyInt: Bits` is not satisfied
+ --> tests/compile-fail/non_integer_base/all_defined.rs:116:22
|
-115 | / bitflags! {
-116 | | struct Flags128: MyInt {
-117 | | const A = MyInt(0b0000_0001u8);
-118 | | const B = MyInt(0b0000_0010u8);
-119 | | const C = MyInt(0b0000_0100u8);
-120 | | }
-121 | | }
- | |_^ expected struct `MyInt`, found integer
+116 | struct Flags128: MyInt {
+ | ^^^^^ the trait `Bits` is not implemented for `MyInt`
|
- = note: this error originates in the macro `__impl_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
-help: try wrapping the expression in `MyInt`
+note: required by a bound in `bitflags::BitFlags::Bits`
+ --> src/bitflags_trait.rs
|
-458 | if extra_bits != MyInt(0) {
- | ++++++ +
+ | type Bits: Bits;
+ | ^^^^ required by this bound in `bitflags::BitFlags::Bits`
-error[E0308]: mismatched types
+error[E0277]: the trait bound `MyInt: Bits` is not satisfied
--> tests/compile-fail/non_integer_base/all_defined.rs:115:1
|
115 | / bitflags! {
diff --git a/tests/compile-fail/non_integer_base/all_defined.stderr b/tests/compile-fail/non_integer_base/all_defined.stderr
--- a/tests/compile-fail/non_integer_base/all_defined.stderr
+++ b/tests/compile-fail/non_integer_base/all_defined.stderr
@@ -26,15 +20,11 @@ error[E0308]: mismatched types
119 | | const C = MyInt(0b0000_0100u8);
120 | | }
121 | | }
- | |_^ expected struct `MyInt`, found integer
+ | |_^ the trait `Bits` is not implemented for `MyInt`
|
= note: this error originates in the macro `__impl_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
-help: try wrapping the expression in `MyInt`
- |
-508 | Self { bits: MyInt(0) }
- | ++++++ +
-error[E0308]: mismatched types
+error[E0277]: the trait bound `MyInt: Bits` is not satisfied
--> tests/compile-fail/non_integer_base/all_defined.rs:115:1
|
115 | / bitflags! {
diff --git a/tests/compile-fail/non_integer_base/all_defined.stderr b/tests/compile-fail/non_integer_base/all_defined.stderr
--- a/tests/compile-fail/non_integer_base/all_defined.stderr
+++ b/tests/compile-fail/non_integer_base/all_defined.stderr
@@ -44,15 +34,11 @@ error[E0308]: mismatched types
119 | | const C = MyInt(0b0000_0100u8);
120 | | }
121 | | }
- | |_^ expected struct `MyInt`, found integer
+ | |_^ the trait `Bits` is not implemented for `MyInt`
|
= note: this error originates in the macro `__impl_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
-help: try wrapping the expression in `MyInt`
- |
-546 | if bits == MyInt(0) {
- | ++++++ +
-error[E0277]: no implementation for `{integer} |= MyInt`
+error[E0277]: the trait bound `MyInt: Bits` is not satisfied
--> tests/compile-fail/non_integer_base/all_defined.rs:115:1
|
115 | / bitflags! {
diff --git a/tests/compile-fail/non_integer_base/all_defined.stderr b/tests/compile-fail/non_integer_base/all_defined.stderr
--- a/tests/compile-fail/non_integer_base/all_defined.stderr
+++ b/tests/compile-fail/non_integer_base/all_defined.stderr
@@ -62,12 +48,11 @@ error[E0277]: no implementation for `{integer} |= MyInt`
119 | | const C = MyInt(0b0000_0100u8);
120 | | }
121 | | }
- | |_^ no implementation for `{integer} |= MyInt`
+ | |_^ the trait `Bits` is not implemented for `MyInt`
|
- = help: the trait `BitOrAssign<MyInt>` is not implemented for `{integer}`
= note: this error originates in the macro `__impl_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
-error[E0308]: mismatched types
+error[E0277]: the trait bound `MyInt: Bits` is not satisfied
--> tests/compile-fail/non_integer_base/all_defined.rs:115:1
|
115 | / bitflags! {
diff --git a/tests/compile-fail/non_integer_base/all_defined.stderr b/tests/compile-fail/non_integer_base/all_defined.stderr
--- a/tests/compile-fail/non_integer_base/all_defined.stderr
+++ b/tests/compile-fail/non_integer_base/all_defined.stderr
@@ -77,28 +62,6 @@ error[E0308]: mismatched types
119 | | const C = MyInt(0b0000_0100u8);
120 | | }
121 | | }
- | |_^ expected struct `MyInt`, found integer
+ | |_^ the trait `Bits` is not implemented for `MyInt`
|
= note: this error originates in the macro `__impl_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
-help: try wrapping the expression in `MyInt`
- |
-561 | Self { bits: MyInt(truncated) }
- | ++++++ +
-
-error[E0308]: mismatched types
- --> tests/compile-fail/non_integer_base/all_defined.rs:115:1
- |
-115 | / bitflags! {
-116 | | struct Flags128: MyInt {
-117 | | const A = MyInt(0b0000_0001u8);
-118 | | const B = MyInt(0b0000_0010u8);
-119 | | const C = MyInt(0b0000_0100u8);
-120 | | }
-121 | | }
- | |_^ expected struct `MyInt`, found integer
- |
- = note: this error originates in the macro `__impl_all_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
-help: try wrapping the expression in `MyInt`
- |
-409 | const $Flag: $T = MyInt(0);
- | ++++++ +
diff --git a/tests/compile-fail/trait/custom_impl.stderr b/tests/compile-fail/trait/custom_impl.stderr
--- a/tests/compile-fail/trait/custom_impl.stderr
+++ b/tests/compile-fail/trait/custom_impl.stderr
@@ -1,11 +1,11 @@
error[E0277]: the trait bound `BootlegFlags: ImplementedByBitFlagsMacro` is not satisfied
- --> $DIR/custom_impl.rs:5:6
+ --> tests/compile-fail/trait/custom_impl.rs:5:6
|
5 | impl BitFlags for BootlegFlags {
| ^^^^^^^^ the trait `ImplementedByBitFlagsMacro` is not implemented for `BootlegFlags`
|
note: required by a bound in `BitFlags`
- --> $DIR/bitflags_trait.rs:7:21
+ --> src/bitflags_trait.rs
|
-7 | pub trait BitFlags: ImplementedByBitFlagsMacro {
+ | pub trait BitFlags: ImplementedByBitFlagsMacro {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ required by this bound in `BitFlags`
diff --git a/tests/compile-fail/cfg/multi.rs b/tests/compile-pass/cfg/redefined-value.rs
--- a/tests/compile-fail/cfg/multi.rs
+++ b/tests/compile-pass/cfg/redefined-value.rs
@@ -1,12 +1,11 @@
#[macro_use]
extern crate bitflags;
-// NOTE: Ideally this would work, but our treatment of CFGs
-// assumes flags may be missing but not redefined
bitflags! {
pub struct Flags: u32 {
#[cfg(target_os = "linux")]
const FOO = 1;
+
#[cfg(not(target_os = "linux"))]
const FOO = 2;
}
diff --git a/tests/compile-fail/cfg/multi.rs b/tests/compile-pass/cfg/redefined-value.rs
--- a/tests/compile-fail/cfg/multi.rs
+++ b/tests/compile-pass/cfg/redefined-value.rs
@@ -20,6 +19,6 @@ fn main() {
#[cfg(not(target_os = "linux"))]
{
- assert_eq!(1, Flags::FOO.bits());
+ assert_eq!(2, Flags::FOO.bits());
}
}
diff --git /dev/null b/tests/compile-pass/deprecated.rs
new file mode 100644
--- /dev/null
+++ b/tests/compile-pass/deprecated.rs
@@ -0,0 +1,14 @@
+#![deny(warnings)]
+
+#[macro_use]
+extern crate bitflags;
+
+bitflags! {
+ pub struct Flags: u32 {
+ #[deprecated = "Use something else"]
+ const A = 0b00000001;
+ const B = 0b00000010;
+ }
+}
+
+fn main() {}
diff --git /dev/null b/tests/compile-pass/non_snake_case.rs
new file mode 100644
--- /dev/null
+++ b/tests/compile-pass/non_snake_case.rs
@@ -0,0 +1,13 @@
+#![deny(warnings)]
+
+#[macro_use]
+extern crate bitflags;
+
+bitflags! {
+ pub struct Flags: u32 {
+ const CamelCase = 0b00000001;
+ const B = 0b00000010;
+ }
+}
+
+fn main() {}
|
810dc35aba3df7314de01b93c7aa137968e925d4
|
[
"tests::test_debug",
"tests::test_iter"
] |
[
"tests::submodule::test_private",
"tests::test_assignment_operators",
"tests::test_binary",
"tests::test_bits",
"tests::test_const_fn",
"tests::test_contains",
"tests::test_default",
"tests::test_deprecated",
"tests::test_disjoint_intersects",
"tests::test_empty_bitflags",
"tests::test_empty_does_not_intersect_with_full",
"tests::test_extend",
"tests::test_from_bits",
"tests::test_from_bits_edge_cases",
"tests::test_from_bits_truncate",
"tests::test_from_bits_truncate_edge_cases",
"tests::test_from_bits_unchecked",
"tests::test_from_iterator",
"tests::test_hash",
"tests::test_in_function",
"tests::test_insert",
"tests::test_is_all",
"tests::test_is_empty",
"tests::test_lowerhex",
"tests::test_lt",
"tests::test_octal",
"tests::test_operators",
"tests::test_operators_unchecked",
"tests::test_ord",
"tests::test_pub_crate",
"tests::test_overlapping_intersects",
"tests::test_pub_in_module",
"tests::test_public",
"tests::test_remove",
"tests::test_serde_bitflags_deserialize",
"tests::test_serde_bitflags_roundtrip",
"tests::test_serde_bitflags_serialize",
"tests::test_set",
"tests::test_set_ops_basic",
"tests::test_set_ops_const",
"tests::test_set_ops_unchecked",
"tests::test_two_empties_do_not_intersect",
"tests::test_u128_bitflags",
"tests::test_upperhex",
"tests::test_zero_value_flags",
"tests::test_set_ops_exhaustive",
"basic",
"tests/compile-pass/cfg/nested-value.rs",
"tests/compile-pass/impls/convert.rs",
"tests/compile-pass/impls/default.rs",
"tests/compile-pass/impls/fmt.rs",
"tests/compile-pass/impls/inherent_methods.rs",
"tests/compile-pass/no_prelude.rs",
"tests/compile-pass/redefinition/core.rs",
"tests/compile-pass/redefinition/macros.rs",
"tests/compile-pass/redefinition/result.rs",
"tests/compile-pass/repr/c.rs",
"tests/compile-pass/repr/transparent.rs",
"tests/compile-pass/trait/precedence.rs",
"tests/compile-pass/visibility/bits_field.rs",
"tests/compile-pass/visibility/pub_in.rs",
"pass",
"src/lib.rs - (line 121)",
"src/lib.rs - (line 251)",
"src/lib.rs - (line 202)",
"src/lib.rs - (line 223)",
"src/lib.rs - (line 48)",
"src/lib.rs - bitflags (line 323)",
"src/lib.rs - (line 89)",
"src/lib.rs - bitflags (line 298)",
"src/lib.rs - (line 20)"
] |
[] |
[] |
bitflags/bitflags
|
from_bits accepts non existing flags
```rs
#[test]
fn test_from_bits_edge_cases() {
bitflags! {
struct Flags: u8 {
const A = 0b00000001;
const BC = 0b00000110;
}
}
let flags = Flags::from_bits(0b00000100);
assert!(flags.is_none());
}
```
Unless I'm missing something this test should pass but it fails cause from_bits accepts flags that are not declared.
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6fd4adbddc8b8740cbd35af2306073ca
This is related to this issue in the implementation of iterators in this PR https://github.com/bitflags/bitflags/pull/204#issuecomment-950304444 Using `from_bits` instead of `from_bits_unchecked` should allow to produce any valid flags that are not a combination of other flags but at the moment `from_bits` seems to accept any flag that is included in a combination even if it's not declared. I can try to send a PR.
|
bitflags__bitflags-276
|
[
"275"
] |
0141a07e55184304857384b0093d00959f0acfa6
|
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1891,6 +1907,37 @@ mod tests {
}
}
+ #[test]
+ fn test_from_bits_edge_cases() {
+ bitflags! {
+ struct Flags: u8 {
+ const A = 0b00000001;
+ const BC = 0b00000110;
+ }
+ }
+
+
+ let flags = Flags::from_bits(0b00000100);
+ assert_eq!(flags, None);
+ let flags = Flags::from_bits(0b00000101);
+ assert_eq!(flags, None);
+ }
+
+ #[test]
+ fn test_from_bits_truncate_edge_cases() {
+ bitflags! {
+ struct Flags: u8 {
+ const A = 0b00000001;
+ const BC = 0b00000110;
+ }
+ }
+
+ let flags = Flags::from_bits_truncate(0b00000100);
+ assert_eq!(flags, Flags::empty());
+ let flags = Flags::from_bits_truncate(0b00000101);
+ assert_eq!(flags, Flags::A);
+ }
+
#[test]
fn test_iter() {
bitflags! {
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1924,22 +1971,4 @@ mod tests {
assert_eq!(iter.next().unwrap(), Flags::THREE);
assert_eq!(iter.next(), None);
}
-
- #[test]
- fn test_iter_edge_cases() {
- bitflags! {
- struct Flags: u8 {
- const A = 0b00000001;
- const BC = 0b00000110;
- }
- }
-
-
- let flags = Flags::all();
- assert_eq!(flags.iter().count(), 2);
- let mut iter = flags.iter();
- assert_eq!(iter.next().unwrap(), Flags::A);
- assert_eq!(iter.next().unwrap(), Flags::BC);
- assert_eq!(iter.next(), None);
- }
}
diff --git a/tests/compile-fail/non_integer_base/all_defined.stderr.beta b/tests/compile-fail/non_integer_base/all_defined.stderr.beta
--- a/tests/compile-fail/non_integer_base/all_defined.stderr.beta
+++ b/tests/compile-fail/non_integer_base/all_defined.stderr.beta
@@ -49,8 +49,41 @@ error[E0308]: mismatched types
= note: this error originates in the macro `__impl_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
help: try wrapping the expression in `MyInt`
|
-562 | if (bits & !Self::all().bits()) == MyInt(0) {
- | ++++++ +
+574 | if bits == MyInt(0) {
+ | ++++++ +
+
+error[E0277]: no implementation for `{integer} |= MyInt`
+ --> $DIR/all_defined.rs:115:1
+ |
+115 | / bitflags! {
+116 | | struct Flags128: MyInt {
+117 | | const A = MyInt(0b0000_0001u8);
+118 | | const B = MyInt(0b0000_0010u8);
+119 | | const C = MyInt(0b0000_0100u8);
+120 | | }
+121 | | }
+ | |_^ no implementation for `{integer} |= MyInt`
+ |
+ = help: the trait `BitOrAssign<MyInt>` is not implemented for `{integer}`
+ = note: this error originates in the macro `__impl_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
+
+error[E0308]: mismatched types
+ --> $DIR/all_defined.rs:115:1
+ |
+115 | / bitflags! {
+116 | | struct Flags128: MyInt {
+117 | | const A = MyInt(0b0000_0001u8);
+118 | | const B = MyInt(0b0000_0010u8);
+119 | | const C = MyInt(0b0000_0100u8);
+120 | | }
+121 | | }
+ | |_^ expected struct `MyInt`, found integer
+ |
+ = note: this error originates in the macro `__impl_bitflags` (in Nightly builds, run with -Z macro-backtrace for more info)
+help: try wrapping the expression in `MyInt`
+ |
+589 | Self { bits: MyInt(truncated) }
+ | ++++++ +
error[E0308]: mismatched types
--> $DIR/all_defined.rs:115:1
|
1.3
| 276
|
2022-04-19T09:54:30Z
|
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -559,10 +559,11 @@ macro_rules! __impl_bitflags {
/// representation contains bits that do not correspond to a flag.
#[inline]
pub const fn from_bits(bits: $T) -> $crate::_core::option::Option<Self> {
- if (bits & !Self::all().bits()) == 0 {
- $crate::_core::option::Option::Some(Self { bits })
+ let truncated = Self::from_bits_truncate(bits).bits;
+ if truncated == bits {
+ Some(Self{ bits })
} else {
- $crate::_core::option::Option::None
+ None
}
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -570,7 +571,22 @@ macro_rules! __impl_bitflags {
/// that do not correspond to flags.
#[inline]
pub const fn from_bits_truncate(bits: $T) -> Self {
- Self { bits: bits & Self::all().bits }
+ if bits == 0 {
+ return Self{ bits }
+ }
+
+ #[allow(unused_mut)]
+ let mut truncated = 0;
+
+ $(
+ #[allow(unused_doc_comments, unused_attributes)]
+ $(#[$attr $($args)*])*
+ if bits & Self::$Flag.bits == Self::$Flag.bits {
+ truncated |= Self::$Flag.bits
+ }
+ )*
+
+ Self { bits: truncated }
}
/// Convert from underlying bit representation, preserving all
|
810dc35aba3df7314de01b93c7aa137968e925d4
|
[
"tests::test_from_bits_edge_cases",
"tests::test_from_bits_truncate_edge_cases"
] |
[
"tests::submodule::test_private",
"tests::test_assignment_operators",
"tests::test_binary",
"tests::test_bits",
"tests::test_const_fn",
"tests::test_contains",
"tests::test_debug",
"tests::test_default",
"tests::test_deprecated",
"tests::test_disjoint_intersects",
"tests::test_empty_bitflags",
"tests::test_empty_does_not_intersect_with_full",
"tests::test_extend",
"tests::test_from_bits",
"tests::test_from_bits_truncate",
"tests::test_from_bits_unchecked",
"tests::test_from_iterator",
"tests::test_hash",
"tests::test_in_function",
"tests::test_insert",
"tests::test_is_all",
"tests::test_is_empty",
"tests::test_iter",
"tests::test_lowerhex",
"tests::test_lt",
"tests::test_octal",
"tests::test_operators",
"tests::test_operators_unchecked",
"tests::test_ord",
"tests::test_overlapping_intersects",
"tests::test_pub_crate",
"tests::test_pub_in_module",
"tests::test_public",
"tests::test_remove",
"tests::test_serde_bitflags_deserialize",
"tests::test_serde_bitflags_roundtrip",
"tests::test_serde_bitflags_serialize",
"tests::test_set",
"tests::test_set_ops_basic",
"tests::test_set_ops_const",
"tests::test_two_empties_do_not_intersect",
"tests::test_set_ops_unchecked",
"tests::test_u128_bitflags",
"tests::test_upperhex",
"tests::test_zero_value_flags",
"tests::test_set_ops_exhaustive",
"basic",
"tests/compile-pass/cfg/nested-value.rs",
"tests/compile-pass/impls/convert.rs",
"tests/compile-pass/impls/default.rs",
"tests/compile-pass/impls/fmt.rs",
"tests/compile-pass/impls/inherent_methods.rs",
"tests/compile-pass/redefinition/core.rs",
"tests/compile-pass/redefinition/macros.rs",
"tests/compile-pass/redefinition/result.rs",
"tests/compile-pass/repr/c.rs",
"tests/compile-pass/repr/transparent.rs",
"tests/compile-pass/trait/precedence.rs",
"tests/compile-pass/visibility/bits_field.rs",
"tests/compile-pass/visibility/pub_in.rs",
"pass",
"src/lib.rs - (line 251)",
"src/lib.rs - (line 121)",
"src/lib.rs - (line 202)",
"src/lib.rs - (line 223)",
"src/lib.rs - bitflags (line 300)",
"src/lib.rs - (line 48)",
"src/lib.rs - (line 89)",
"src/lib.rs - bitflags (line 325)",
"src/lib.rs - (line 20)"
] |
[
"fail"
] |
[] |
|
bitflags/bitflags
|
Bug: debug pretty-printing unknown flags display 0x0x
main.rs
```rust
use bitflags::bitflags;
bitflags! {
struct Flags: u8 {
const TWO = 0x2;
}
}
fn main() {
let value = 0b11;
let flags = unsafe { Flags::from_bits_unchecked(value) };
println!("{:?}", flags);
println!("-----------");
println!("{:#?}", flags);
}
```
will print the following:
```sh
TWO | 0x1
-----------
TWO | 0x0x1
```
the expected output would either be 0x1 for both, or 1, and 0x1 respectively
|
bitflags__bitflags-268
|
[
"267"
] |
1aa25e1b3baf35d3d3840f12fe7e8b55adc0164a
|
diff --git a/tests/compile-fail/trait/custom_impl.rs b/tests/compile-fail/trait/custom_impl.rs
--- a/tests/compile-fail/trait/custom_impl.rs
+++ b/tests/compile-fail/trait/custom_impl.rs
@@ -62,4 +62,4 @@ impl BitFlags for BootlegFlags {
}
}
-fn main() { }
+fn main() {}
diff --git a/tests/compile-pass/impls/convert.rs b/tests/compile-pass/impls/convert.rs
--- a/tests/compile-pass/impls/convert.rs
+++ b/tests/compile-pass/impls/convert.rs
@@ -12,6 +12,4 @@ impl From<u32> for Flags {
}
}
-fn main() {
-
-}
+fn main() {}
diff --git /dev/null b/tests/compile-pass/impls/fmt.rs
new file mode 100644
--- /dev/null
+++ b/tests/compile-pass/impls/fmt.rs
@@ -0,0 +1,14 @@
+use bitflags::bitflags;
+
+bitflags! {
+ struct Flags: u8 {
+ const TWO = 0x2;
+ }
+}
+
+fn main() {
+ // bug #267 (https://github.com/bitflags/bitflags/issues/267)
+ let flags = unsafe { Flags::from_bits_unchecked(0b11) };
+ assert_eq!(format!("{:?}", flags), "TWO | 0x1");
+ assert_eq!(format!("{:#?}", flags), "TWO | 0x1");
+}
diff --git a/tests/compile-pass/redefinition/stringify.rs b/tests/compile-pass/redefinition/macros.rs
--- a/tests/compile-pass/redefinition/stringify.rs
+++ b/tests/compile-pass/redefinition/macros.rs
@@ -7,6 +7,11 @@ macro_rules! stringify {
($($t:tt)*) => { "..." };
}
+#[allow(unused_macros)]
+macro_rules! write {
+ ($($t:tt)*) => { "..." };
+}
+
bitflags! {
struct Test: u8 {
const A = 1;
diff --git a/tests/compile-pass/redefinition/stringify.rs b/tests/compile-pass/redefinition/macros.rs
--- a/tests/compile-pass/redefinition/stringify.rs
+++ b/tests/compile-pass/redefinition/macros.rs
@@ -14,6 +19,6 @@ bitflags! {
}
fn main() {
- // Just make sure we don't call the redefined `stringify` macro
- assert_eq!(format!("{:?}", Test::A), "A");
+ // Just make sure we don't call the redefined `stringify` or `write` macro
+ assert_eq!(format!("{:?}", unsafe { Test::from_bits_unchecked(0b11) }), "A | 0x2");
}
|
1.3
| 268
|
2022-01-02T17:22:14Z
|
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -494,8 +494,7 @@ macro_rules! __impl_bitflags {
f.write_str(" | ")?;
}
first = false;
- f.write_str("0x")?;
- $crate::_core::fmt::LowerHex::fmt(&extra_bits, f)?;
+ $crate::_core::write!(f, "{:#x}", extra_bits)?;
}
if first {
f.write_str("(empty)")?;
|
810dc35aba3df7314de01b93c7aa137968e925d4
|
[
"tests/compile-pass/impls/fmt.rs",
"pass"
] |
[
"tests::submodule::test_private",
"tests::test_assignment_operators",
"tests::test_binary",
"tests::test_bits",
"tests::test_const_fn",
"tests::test_contains",
"tests::test_debug",
"tests::test_default",
"tests::test_deprecated",
"tests::test_disjoint_intersects",
"tests::test_empty_bitflags",
"tests::test_empty_does_not_intersect_with_full",
"tests::test_extend",
"tests::test_from_bits",
"tests::test_from_bits_truncate",
"tests::test_from_bits_unchecked",
"tests::test_from_iterator",
"tests::test_hash",
"tests::test_in_function",
"tests::test_insert",
"tests::test_is_all",
"tests::test_is_empty",
"tests::test_lowerhex",
"tests::test_lt",
"tests::test_octal",
"tests::test_operators",
"tests::test_operators_unchecked",
"tests::test_ord",
"tests::test_overlapping_intersects",
"tests::test_pub_crate",
"tests::test_pub_in_module",
"tests::test_public",
"tests::test_remove",
"tests::test_serde_bitflags_deserialize",
"tests::test_serde_bitflags_roundtrip",
"tests::test_set",
"tests::test_serde_bitflags_serialize",
"tests::test_set_ops_const",
"tests::test_set_ops_basic",
"tests::test_set_ops_unchecked",
"tests::test_two_empties_do_not_intersect",
"tests::test_upperhex",
"tests::test_u128_bitflags",
"tests::test_zero_value_flags",
"tests::test_set_ops_exhaustive",
"basic",
"tests/compile-pass/cfg/nested-value.rs",
"tests/compile-pass/impls/convert.rs",
"tests/compile-pass/impls/default.rs",
"tests/compile-pass/impls/inherent_methods.rs",
"tests/compile-pass/redefinition/core.rs",
"tests/compile-pass/redefinition/macros.rs",
"tests/compile-pass/repr/c.rs",
"tests/compile-pass/repr/transparent.rs",
"tests/compile-pass/trait/precedence.rs",
"tests/compile-pass/visibility/bits_field.rs",
"tests/compile-pass/visibility/pub_in.rs",
"src/lib.rs - (line 251)",
"src/lib.rs - (line 20)",
"src/lib.rs - (line 121)",
"src/lib.rs - bitflags (line 300)",
"src/lib.rs - (line 202)",
"src/lib.rs - (line 89)",
"src/lib.rs - (line 223)",
"src/lib.rs - (line 48)",
"src/lib.rs - bitflags (line 325)"
] |
[
"fail"
] |
[] |
|
bitflags/bitflags
|
The bitflags macro is not sanitary wrt. standard library types and enumerations
The `bitflags` macro, expanded in the prescence of a definition of the type/value `Ok` errors.
Reproduction code:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=3fda3e36c7c6a57e0f7a83c84e56df20
Interestingly, the relevant function, the `fmt` function from the Debug impl, does use `::bitflags::_core::fmt::Result`, however, it merely returns the value `Ok(())`.
|
bitflags__bitflags-266
|
[
"265"
] |
1aa25e1b3baf35d3d3840f12fe7e8b55adc0164a
|
diff --git /dev/null b/tests/compile-pass/redefinition/result.rs
new file mode 100644
--- /dev/null
+++ b/tests/compile-pass/redefinition/result.rs
@@ -0,0 +1,15 @@
+use bitflags::bitflags;
+
+// Checks for possible errors caused by overriding names used by `bitflags!` internally.
+
+// bug #265 (https://github.com/bitflags/bitflags/issues/265)
+
+pub struct Ok<T>(T);
+
+bitflags! {
+ pub struct Flags: u16{
+ const FOO = 0x0001;
+ }
+}
+
+fn main() {}
|
1.3
| 266
|
2021-12-16T09:38:14Z
|
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -500,7 +500,7 @@ macro_rules! __impl_bitflags {
if first {
f.write_str("(empty)")?;
}
- Ok(())
+ $crate::_core::fmt::Result::Ok(())
}
}
impl $crate::_core::fmt::Binary for $BitFlags {
|
810dc35aba3df7314de01b93c7aa137968e925d4
|
[
"tests/compile-pass/redefinition/result.rs",
"pass"
] |
[
"tests::submodule::test_private",
"tests::test_assignment_operators",
"tests::test_binary",
"tests::test_bits",
"tests::test_const_fn",
"tests::test_contains",
"tests::test_debug",
"tests::test_default",
"tests::test_deprecated",
"tests::test_disjoint_intersects",
"tests::test_empty_bitflags",
"tests::test_empty_does_not_intersect_with_full",
"tests::test_extend",
"tests::test_from_bits",
"tests::test_from_bits_truncate",
"tests::test_from_bits_unchecked",
"tests::test_from_iterator",
"tests::test_hash",
"tests::test_in_function",
"tests::test_insert",
"tests::test_is_all",
"tests::test_is_empty",
"tests::test_lowerhex",
"tests::test_lt",
"tests::test_octal",
"tests::test_operators",
"tests::test_operators_unchecked",
"tests::test_ord",
"tests::test_overlapping_intersects",
"tests::test_pub_crate",
"tests::test_pub_in_module",
"tests::test_public",
"tests::test_remove",
"tests::test_serde_bitflags_deserialize",
"tests::test_serde_bitflags_roundtrip",
"tests::test_serde_bitflags_serialize",
"tests::test_set",
"tests::test_set_ops_basic",
"tests::test_set_ops_const",
"tests::test_set_ops_unchecked",
"tests::test_two_empties_do_not_intersect",
"tests::test_u128_bitflags",
"tests::test_upperhex",
"tests::test_zero_value_flags",
"tests::test_set_ops_exhaustive",
"basic",
"tests/compile-pass/cfg/nested-value.rs",
"tests/compile-pass/impls/convert.rs",
"tests/compile-pass/impls/default.rs",
"tests/compile-pass/impls/inherent_methods.rs",
"tests/compile-pass/redefinition/core.rs",
"tests/compile-pass/redefinition/stringify.rs",
"tests/compile-pass/repr/c.rs",
"tests/compile-pass/repr/transparent.rs",
"tests/compile-pass/trait/precedence.rs",
"tests/compile-pass/visibility/bits_field.rs",
"tests/compile-pass/visibility/pub_in.rs",
"src/lib.rs - (line 121)",
"src/lib.rs - (line 251)",
"src/lib.rs - (line 89)",
"src/lib.rs - (line 20)",
"src/lib.rs - (line 202)",
"src/lib.rs - bitflags (line 300)",
"src/lib.rs - (line 223)",
"src/lib.rs - bitflags (line 325)",
"src/lib.rs - (line 48)"
] |
[
"fail"
] |
[] |
|
bitflags/bitflags
|
is_all() vs. from_bits_unchecked()
[`unsafe from_bits_unchecked()`](https://docs.rs/bitflags/1.2.1/bitflags/example_generated/struct.Flags.html#method.from_bits_unchecked) allows creating instances with extra bits. The caller of the `bitflags!` macro can decide if this is allowed for their type. Let's assume it is for `Example`. I checked the provided methods for surprising interactions with extra bits, and found (only) this:
`is_all()` returns **false** when there are *more* than "all" flags. This does not match the documentation:
> Returns true if all flags are currently set.
Should we update the documentation or the implementation?
---
```rust
use bitflags::bitflags;
bitflags! {
struct Example: u32 {
const A = 1;
}
}
fn main() {
unsafe {
assert!(Example::from_bits_unchecked(1).is_all()); // true
assert!(Example::from_bits_unchecked(3).is_all()); // false
}
}
```
https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=cda2672387dd0ff4ba629b1317a9c57c
|
bitflags__bitflags-211
|
[
"208"
] |
15e911c304d5bd8805af55d7e4f8c5324ed798ee
|
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1048,6 +1048,11 @@ mod tests {
assert!(!Flags::A.is_all());
assert!(Flags::ABC.is_all());
+ let extra = unsafe { Flags::from_bits_unchecked(0b1000) };
+ assert!(!extra.is_all());
+ assert!(!(Flags::A | extra).is_all());
+ assert!((Flags::ABC | extra).is_all());
+
assert!(AnotherSetOfFlags::ANOTHER_FLAG.is_all());
}
|
1.2
| 211
|
2020-02-04T10:52:16Z
|
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -664,7 +664,7 @@ macro_rules! __impl_bitflags {
/// Returns `true` if all flags are currently set.
#[inline]
pub const fn is_all(&self) -> bool {
- self.bits == $BitFlags::all().bits
+ $BitFlags::all().bits | self.bits == self.bits
}
}
|
bd24f9d8d266bfb2dfe4b8238b196ecf5e37dee1
|
[
"tests::test_is_all"
] |
[
"tests::submodule::test_private",
"tests::test_assignment_operators",
"tests::test_binary",
"tests::test_bits",
"tests::test_const_fn",
"tests::test_contains",
"tests::test_debug",
"tests::test_deprecated",
"tests::test_disjoint_intersects",
"tests::test_empty_does_not_intersect_with_full",
"tests::test_extend",
"tests::test_from_bits",
"tests::test_from_bits_truncate",
"tests::test_from_bits_unchecked",
"tests::test_from_iterator",
"tests::test_hash",
"tests::test_in_function",
"tests::test_insert",
"tests::test_is_empty",
"tests::test_lowerhex",
"tests::test_lt",
"tests::test_octal",
"tests::test_operators",
"tests::test_operators_unchecked",
"tests::test_ord",
"tests::test_overlapping_intersects",
"tests::test_pub_crate",
"tests::test_pub_in_module",
"tests::test_public",
"tests::test_remove",
"tests::test_set",
"tests::test_two_empties_do_not_intersect",
"tests::test_upperhex",
"tests::test_zero_value_flags",
"src/lib.rs - (line 91)",
"src/lib.rs - (line 227)",
"src/lib.rs - (line 20)",
"src/lib.rs - (line 176)",
"src/lib.rs - bitflags (line 271)",
"src/lib.rs - (line 198)",
"src/lib.rs - bitflags (line 297)",
"src/lib.rs - (line 49)"
] |
[] |
[] |
|
bitflags/bitflags
|
Cannot use `#[doc(alias)]`
The following code:
```rs
bitflags::bitflags! {
#[doc(alias = "SYMBOLIC_LINK_FLAGS")]
pub struct SymbolicLinkFlags:u32 {
#[doc(alias = "SYMBOLIC_LINK_FLAG_DIRECTORY")]
const DIRECTORY = 0x1;
#[doc(alias = "SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE")]
const ALLOW_UNPRIVILEGED_CREATE = 0x2;
}
}
```
Produces the error:
```
error: `#[doc(alias = "...")]` isn't allowed on expression
--> src\fs.rs:67:15
|
67 | #[doc(alias = "SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE")]
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
```
|
This is a general problem for attributes that can't be applied to expressions now. In methods like `from_name` we now generate code like this:
```rust
#[inline]
pub fn from_name(name: &str) -> ::bitflags::__private::core::option::Option<Self> {
match name {
#[doc(alias = "SYMBOLIC_LINK_FLAG_DIRECTORY")]
"DIRECTORY" => ::bitflags::__private::core::option::Option::Some(Self {
bits: SymbolicLinkFlags::DIRECTORY.bits(),
}),
#[doc(alias = "SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE")]
"ALLOW_UNPRIVILEGED_CREATE" => {
::bitflags::__private::core::option::Option::Some(Self {
bits: SymbolicLinkFlags::ALLOW_UNPRIVILEGED_CREATE.bits(),
})
}
_ => ::bitflags::__private::core::option::Option::None,
}
}
```
I think the quickest fix would be to introduce a helper macro that filtered out some attributes like `#[doc(..)]` for these match arms. Any more of these that come up in the future could be added to that macro.
What kinds of attributes would be useful to apply there other than `cfg`?
I can't really think of any besides maybe `#[allow]`, but we handle those on the item itself. I think it would be fair to flip this into an allow-list so only `cfg` attributes get propagated.
|
bitflags__bitflags-341
|
[
"308"
] |
dc971042c8132a5381ab3e2165983ee7f9d44c63
|
diff --git /dev/null b/tests/compile-pass/doc_alias.rs
new file mode 100644
--- /dev/null
+++ b/tests/compile-pass/doc_alias.rs
@@ -0,0 +1,14 @@
+#[macro_use]
+extern crate bitflags;
+
+bitflags! {
+ #[doc(alias = "FLAG")]
+ pub struct Flags: u8 {
+ #[doc(alias = "FLAG_A")]
+ const A = 1;
+ }
+}
+
+fn main() {
+
+}
|
2.1
| 341
|
2023-04-18T00:36:26Z
|
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -558,6 +558,315 @@ macro_rules! bitflags {
}
$($t:tt)*
+ ) => {
+ __declare_bitflags!(
+ $(#[$outer])*
+ $vis struct $BitFlags: $T {
+ $(
+ $(#[$inner $($args)*])*
+ const $Flag = $value;
+ )*
+ }
+ );
+
+ bitflags! {
+ $($t)*
+ }
+ };
+ () => {};
+}
+
+/// A macro that processed the input to `bitflags!` and shuffles attributes around
+/// based on whether or not they're "expression-safe".
+///
+/// This macro is a token-tree muncher that works on 2 levels:
+///
+/// 1. Each flag, like `#[cfg(true)] const A: 42`
+/// 2. Each attribute on that flag, like `#[cfg(true)]`
+///
+/// Flags and attributes start in an "unprocessed" list, and are shifted one token
+/// at a time into an appropriate processed list until the unprocessed lists are empty.
+///
+/// For each attribute, we explicitly match on its identifier, like `cfg` to determine
+/// whether or not it should be considered expression-safe.
+///
+/// If you find yourself with an attribute that should be considered expression-safe
+/// and isn't, it can be added here.
+#[macro_export(local_inner_macros)]
+#[doc(hidden)]
+macro_rules! __declare_bitflags {
+ // Entrypoint: Move all flags and all attributes into `unprocessed` lists
+ // where they'll be munched one-at-a-time
+ (
+ $(#[$outer:meta])*
+ $vis:vis struct $BitFlags:ident: $T:ty {
+ $(
+ $(#[$inner:ident $($args:tt)*])*
+ const $Flag:ident = $value:expr;
+ )*
+ }
+ ) => {
+ __declare_bitflags! {
+ decl: {
+ attrs: [$(#[$outer])*],
+ vis: $vis,
+ ident: $BitFlags,
+ ty: $T,
+ },
+ flags: {
+ // All flags start here
+ unprocessed: [
+ $(
+ {
+ ident: $Flag,
+ value: $value,
+ attrs: {
+ // All attributes start here
+ unprocessed: [$(#[$inner $($args)*])*],
+ processed: {
+ // Attributes that should be added to item declarations go here
+ decl: [],
+ // Attributes that are safe on expressions go here
+ expr: [],
+ }
+ },
+ },
+ )*
+ ],
+ // Flags that have had their attributes sorted are pushed here
+ processed: [],
+ }
+ }
+ };
+ // Process the next attribute on the current flag
+ // `cfg`: The next flag should be propagated to expressions
+ // NOTE: You can copy this rules block and replace `cfg` with
+ // your attribute name that should be considered expression-safe
+ (
+ decl: {
+ attrs: [$(#[$outer:meta])*],
+ vis: $vis:vis,
+ ident: $BitFlags:ident,
+ ty: $T:ty,
+ },
+ flags: {
+ unprocessed: [
+ {
+ ident: $Flag:ident,
+ value: $value:expr,
+ attrs: {
+ unprocessed: [
+ // cfg matched here
+ #[cfg $($args:tt)*]
+ $($attrs_rest:tt)*
+ ],
+ processed: {
+ decl: [$($decl:tt)*],
+ expr: [$($expr:tt)*],
+ }
+ },
+ },
+ $($flags_rest:tt)*
+ ],
+ processed: [
+ $($flags:tt)*
+ ],
+ }
+ ) => {
+ __declare_bitflags! {
+ decl: {
+ attrs: [$(#[$outer])*],
+ vis: $vis,
+ ident: $BitFlags,
+ ty: $T,
+ },
+ flags: {
+ unprocessed: [
+ {
+ ident: $Flag,
+ value: $value,
+ attrs: {
+ unprocessed: [
+ $($attrs_rest)*
+ ],
+ processed: {
+ decl: [
+ // cfg added here
+ #[cfg $($args)*]
+ $($decl)*
+ ],
+ expr: [
+ // cfg added here
+ #[cfg $($args)*]
+ $($expr)*
+ ],
+ }
+ },
+ },
+ $($flags_rest)*
+ ],
+ processed: [
+ $($flags)*
+ ],
+ }
+ }
+ };
+ // Process the next attribute on the current flag
+ // `$other`: The next flag should not be propagated to expressions
+ (
+ decl: {
+ attrs: [$(#[$outer:meta])*],
+ vis: $vis:vis,
+ ident: $BitFlags:ident,
+ ty: $T:ty,
+ },
+ flags: {
+ unprocessed: [
+ {
+ ident: $Flag:ident,
+ value: $value:expr,
+ attrs: {
+ unprocessed: [
+ // $other matched here
+ #[$other:ident $($args:tt)*]
+ $($attrs_rest:tt)*
+ ],
+ processed: {
+ decl: [$($decl:tt)*],
+ expr: [$($expr:tt)*],
+ }
+ },
+ },
+ $($flags_rest:tt)*
+ ],
+ processed: [
+ $($flags:tt)*
+ ],
+ }
+ ) => {
+ __declare_bitflags! {
+ decl: {
+ attrs: [$(#[$outer])*],
+ vis: $vis,
+ ident: $BitFlags,
+ ty: $T,
+ },
+ flags: {
+ unprocessed: [
+ {
+ ident: $Flag,
+ value: $value,
+ attrs: {
+ unprocessed: [
+ $($attrs_rest)*
+ ],
+ processed: {
+ decl: [
+ // $other added here
+ #[$other $($args)*]
+ $($decl)*
+ ],
+ expr: [
+ // $other not added here
+ $($expr)*
+ ],
+ }
+ },
+ },
+ $($flags_rest)*
+ ],
+ processed: [
+ $($flags)*
+ ],
+ }
+ }
+ };
+ // Complete the current flag once there are no unprocessed attributes left
+ (
+ decl: {
+ attrs: [$(#[$outer:meta])*],
+ vis: $vis:vis,
+ ident: $BitFlags:ident,
+ ty: $T:ty,
+ },
+ flags: {
+ unprocessed: [
+ {
+ ident: $Flag:ident,
+ value: $value:expr,
+ attrs: {
+ unprocessed: [],
+ processed: {
+ decl: [$($decl:tt)*],
+ expr: [$($expr:tt)*],
+ }
+ },
+ },
+ $($flags_rest:tt)*
+ ],
+ processed: [
+ $($flags:tt)*
+ ],
+ }
+ ) => {
+ __declare_bitflags! {
+ decl: {
+ attrs: [$(#[$outer])*],
+ vis: $vis,
+ ident: $BitFlags,
+ ty: $T,
+ },
+ flags: {
+ unprocessed: [
+ $($flags_rest)*
+ ],
+ processed: [
+ $($flags)*
+ {
+ ident: $Flag,
+ value: $value,
+ attrs: {
+ unprocessed: [],
+ processed: {
+ decl: [
+ $($decl)*
+ ],
+ expr: [
+ $($expr)*
+ ],
+ }
+ },
+ },
+ ],
+ }
+ }
+ };
+ // Once all attributes on all flags are processed, generate the actual code
+ (
+ decl: {
+ attrs: [$(#[$outer:meta])*],
+ vis: $vis:vis,
+ ident: $BitFlags:ident,
+ ty: $T:ty,
+ },
+ flags: {
+ unprocessed: [],
+ processed: [
+ $(
+ {
+ ident: $Flag:ident,
+ value: $value:expr,
+ attrs: {
+ unprocessed: [],
+ processed: {
+ decl: [$(#[$decl:ident $($declargs:tt)*])*],
+ expr: [$(#[$expr:ident $($exprargs:tt)*])*],
+ }
+ },
+ },
+ )*
+ ],
+ }
) => {
// Declared in the scope of the `bitflags!` call
// This type appears in the end-user's API
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -570,7 +879,7 @@ macro_rules! bitflags {
__impl_public_bitflags_consts! {
$BitFlags {
$(
- $(#[$inner $($args)*])*
+ $(#[$decl $($declargs)*])*
#[allow(
dead_code,
deprecated,
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -603,7 +912,7 @@ macro_rules! bitflags {
__impl_internal_bitflags! {
InternalBitFlags: $T, $BitFlags, Iter, IterRaw {
$(
- $(#[$inner $($args)*])*
+ $(#[$expr $($exprargs)*])*
$Flag;
)*
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -613,7 +922,7 @@ macro_rules! bitflags {
__impl_external_bitflags! {
InternalBitFlags: $T {
$(
- $(#[$inner $($args)*])*
+ $(#[$expr $($exprargs)*])*
$Flag;
)*
}
diff --git a/src/lib.rs b/src/lib.rs
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -623,12 +932,7 @@ macro_rules! bitflags {
$BitFlags: $T, InternalBitFlags, Iter, IterRaw;
}
};
-
- bitflags! {
- $($t)*
- }
- };
- () => {};
+ }
}
#[macro_use]
|
dc971042c8132a5381ab3e2165983ee7f9d44c63
|
[
"tests/compile-pass/doc_alias.rs",
"pass"
] |
[
"tests::bits_types",
"tests::submodule::test_private",
"tests::into_iter_from_iter_roundtrip",
"tests::test_assignment_operators",
"tests::test_binary",
"tests::test_bits",
"tests::test_const_fn",
"tests::test_contains",
"tests::test_debug",
"tests::test_default",
"tests::test_deprecated",
"tests::test_disjoint_intersects",
"tests::test_display_from_str_roundtrip",
"tests::test_empty_bitflags",
"tests::test_empty_does_not_intersect_with_full",
"tests::test_extend",
"tests::test_from_bits",
"tests::test_from_bits_edge_cases",
"tests::test_from_bits_retain",
"tests::test_from_bits_truncate",
"tests::test_from_bits_truncate_edge_cases",
"tests::test_from_iterator",
"tests::test_from_name",
"tests::test_from_str_err",
"tests::test_hash",
"tests::test_in_function",
"tests::test_insert",
"tests::test_is_all",
"tests::test_is_empty",
"tests::test_iter",
"tests::test_lowerhex",
"tests::test_lt",
"tests::test_octal",
"tests::test_operators",
"tests::test_operators_unchecked",
"tests::test_overlapping_intersects",
"tests::test_ord",
"tests::test_pub_in_module",
"tests::test_pub_crate",
"tests::test_remove",
"tests::test_public",
"tests::test_set",
"tests::test_set_ops_basic",
"tests::test_set_ops_const",
"tests::test_set_ops_unchecked",
"tests::test_two_empties_do_not_intersect",
"tests::test_u128_bitflags",
"tests::test_upperhex",
"tests::test_zero_value_flags",
"tests::test_set_ops_exhaustive",
"basic",
"tests/compile-pass/cfg/nested-value.rs",
"tests/compile-pass/cfg/redefined-value.rs",
"tests/compile-pass/deprecated.rs",
"tests/compile-pass/impls/convert.rs",
"tests/compile-pass/impls/copy.rs",
"tests/compile-pass/impls/default.rs",
"tests/compile-pass/impls/eq.rs",
"tests/compile-pass/impls/fmt.rs",
"tests/compile-pass/impls/inherent_methods.rs",
"tests/compile-pass/item_positions.rs",
"tests/compile-pass/no_prelude.rs",
"tests/compile-pass/non_snake_case.rs",
"tests/compile-pass/path_based_bits.rs",
"tests/compile-pass/redefinition/core.rs",
"tests/compile-pass/redefinition/macros.rs",
"tests/compile-pass/redefinition/result.rs",
"tests/compile-pass/repr/c.rs",
"tests/compile-pass/repr/transparent.rs",
"tests/compile-pass/trait/generic_iter.rs",
"tests/compile-pass/trait/precedence.rs",
"tests/compile-pass/trait/wrapped_iter.rs",
"tests/compile-pass/visibility/bits_field.rs",
"tests/compile-pass/visibility/pub_in.rs",
"src/lib.rs - (line 360)",
"src/lib.rs - (line 414)",
"src/lib.rs - (line 19)",
"src/lib.rs - bitflags (line 522)",
"src/lib.rs - (line 246)",
"src/lib.rs - (line 86)",
"src/lib.rs - (line 225)",
"src/lib.rs - (line 385)",
"src/lib.rs - (line 280)",
"src/lib.rs - (line 105)",
"src/lib.rs - (line 325)",
"src/lib.rs - bitflags (line 498)",
"src/lib.rs - (line 50)"
] |
[] |
[] |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.