Skip to content

Commit

Permalink
Show a better error on wrong struct size
Browse files Browse the repository at this point in the history
  • Loading branch information
hecatia-elegua committed Jul 23, 2023
1 parent c273569 commit 9528b98
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 8 deletions.
10 changes: 2 additions & 8 deletions bilge-impl/src/bitsize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,14 +145,8 @@ fn generate_struct(item: &ItemStruct, declared_bitsize: u8) -> TokenStream {
quote! {
#vis struct #ident #fields_def

// constness: when we get const blocks evaluated at compile time, add a const computed_bitsize
const _: () = assert!(
(#computed_bitsize) == (#declared_bitsize),
concat!("struct size and declared bit size differ: ",
// stringify!(#computed_bitsize),
" != ",
stringify!(#declared_bitsize))
);
// constness: assert_eq!, format! are not const
const _: () = ::bilge::AssertEquals::<{#computed_bitsize}, {#declared_bitsize}>::EQUAL;
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,14 @@ pub fn give_me_error() -> BitsError {
BitsError
}

/// This is used in `bitsize.rs` gen, for showing a compile error.
/// Only needed because const contexts can't use `format!` and `COMPUTED_SIZE` is not a plain number.
/// `evaluation of `bilge::AssertEquals::<19, 18>::EQUAL` failed` will tell you what's wrong.
pub struct AssertEquals<const COMPUTED_SIZE: usize, const DECLARED_SIZE: usize>;
impl<const COMPUTED_SIZE: usize, const DECLARED_SIZE: usize> AssertEquals<COMPUTED_SIZE, DECLARED_SIZE> {
pub const EQUAL: () = assert!(COMPUTED_SIZE == DECLARED_SIZE, "computed bitsize and declared bitsize differ");
}

/// Only basing this on Number did not work, as bool and others are not Number.
/// We could remove the whole macro_rules thing if it worked, though.
/// Maybe there is some way to do this, I'm not deep into types.
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/attr-value-does-not-match-size.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
use bilge::prelude::*;

#[bitsize(18)]
struct Wrong {
field1: u6,
field2: u13,
}

fn main() {}
15 changes: 15 additions & 0 deletions tests/ui/attr-value-does-not-match-size.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
error[E0080]: evaluation of `bilge::AssertEquals::<19, 18>::EQUAL` failed
--> src/lib.rs
|
| pub const EQUAL: () = assert!(COMPUTED_SIZE == DECLARED_SIZE, "computed bitsize and declared bitsize differ");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the evaluated program panicked at 'computed bitsize and declared bitsize differ', $DIR/src/lib.rs:52:27
|
= note: this error originates in the macro `$crate::panic::panic_2021` which comes from the expansion of the macro `assert` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0080]: evaluation of constant value failed
--> tests/ui/attr-value-does-not-match-size.rs:3:1
|
3 | #[bitsize(18)]
| ^^^^^^^^^^^^^^ referenced constant has errors
|
= note: this error originates in the attribute macro `bitsize` (in Nightly builds, run with -Z macro-backtrace for more info)

0 comments on commit 9528b98

Please sign in to comment.