Skip to content

Commit

Permalink
add regression test for Self in flags values
Browse files Browse the repository at this point in the history
  • Loading branch information
KodrAus committed May 17, 2023
1 parent 31d3e4a commit 4aa03b8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
6 changes: 3 additions & 3 deletions tests/compile-fail/bitflags_custom_bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{
},
};

use bitflags::{bitflags, Bits, parser::{ParseError, FromHex}};
use bitflags::{bitflags, Bits, parser::{ParseError, 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
Expand Down Expand Up @@ -117,8 +117,8 @@ 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))?))
}
}
Expand Down
32 changes: 32 additions & 0 deletions tests/compile-fail/bitflags_custom_bits.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,38 @@ note: required by a bound in `PublicFlags::Primitive`
| type Primitive: Primitive;
| ^^^^^^^^^ required by this bound in `PublicFlags::Primitive`

error[E0277]: the trait bound `MyInt: WriteHex` 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 | | }
| | ^
| | |
| |_the trait `WriteHex` is not implemented for `MyInt`
| required by a bound introduced by this call
|
= help: the following other types implement trait `WriteHex`:
i128
i16
i32
i64
i8
isize
u128
u16
and $N others
note: required by a bound in `to_writer`
--> src/parser.rs
|
| B::Bits: WriteHex,
| ^^^^^^^^ required by this bound in `to_writer`
= note: this error originates in the macro `__impl_internal_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
|
Expand Down
15 changes: 15 additions & 0 deletions tests/compile-pass/bitflags_self_in_value.rs
Original file line number Diff line number Diff line change
@@ -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() {

}

0 comments on commit 4aa03b8

Please sign in to comment.