Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show a better error on wrong struct size #62

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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>;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

consider #[doc(hidden)] :)

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:55: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)