Skip to content

Commit dffd611

Browse files
committed
Improve error message of generated static assertions
1 parent bdfe727 commit dffd611

File tree

2 files changed

+27
-6
lines changed

2 files changed

+27
-6
lines changed

enumflags_derive/src/lib.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,12 +147,13 @@ fn verify_flag_values<'a>(
147147
Span::call_site()); // call_site because def_site is unstable
148148
// adapted from static-assertions-rs by nvzqz (MIT/Apache-2.0)
149149
deferred_checks.push(quote_spanned!(variant.span() =>
150-
#[allow(unknown_lints, eq_op)]
151-
const #assertion_name: [(); 0 - !(
152-
(#_type_name::#variant_name as u64).wrapping_sub(1) &
153-
(#_type_name::#variant_name as u64) == 0 &&
154-
(#_type_name::#variant_name as u64) != 0
155-
) as usize] = [];
150+
const #assertion_name: fn() = || {
151+
::enumflags2::_internal::assert_exactly_one_bit_set::<[(); (
152+
(#_type_name::#variant_name as u64).wrapping_sub(1) &
153+
(#_type_name::#variant_name as u64) == 0 &&
154+
(#_type_name::#variant_name as u64) != 0
155+
) as usize]>();
156+
};
156157
));
157158
}
158159
Err(why) => return Err(why.into()),

src/lib.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,26 @@ pub mod _internal {
214214
pub mod core {
215215
pub use core::{convert, option, ops};
216216
}
217+
218+
pub struct AssertionSucceeded;
219+
pub struct AssertionFailed;
220+
pub trait ExactlyOneBitSet {}
221+
impl ExactlyOneBitSet for AssertionSucceeded {}
222+
223+
pub trait AssertionHelper {
224+
type Status;
225+
}
226+
227+
impl AssertionHelper for [(); 1] {
228+
type Status = AssertionSucceeded;
229+
}
230+
231+
impl AssertionHelper for [(); 0] {
232+
type Status = AssertionFailed;
233+
}
234+
235+
pub fn assert_exactly_one_bit_set<T: AssertionHelper>()
236+
where T::Status: ExactlyOneBitSet {}
217237
}
218238

219239
// Internal debug formatting implementations

0 commit comments

Comments
 (0)