hecatia-elegua / bilge

Use bitsized types as if they were a feature of rust.
Apache License 2.0
171 stars 17 forks source link

Panic when creating struct with single enum field #36

Closed sfleener closed 1 year ago

sfleener commented 1 year ago

Triggering this panic also seems to require the struct and enum width to match the width of the underlying integer representation.

Here's the most minimal reproduction I could create of the issue, I can also reproduce this with 16, 32, and 64 bits:

#[bitsize(8)]
#[derive(TryFromBits, PartialEq, DebugBits)]
struct Wrapper {
    foo: FillsU8
}

#[bitsize(8)]
#[derive(TryFromBits, PartialEq, Debug)]
enum FillsU8 {
    Foo = 0
}

let foo = Wrapper::try_from(0);
assert_eq!(foo, Ok(Wrapper::new(FillsU8::Foo)))
---- instruction::immediate::tests::test_overflow stdout ----
thread 'sandbox::tests::test_overflow' panicked at 'attempt to shift right with overflow', src\lib.rs:7:18
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library\std\src\panicking.rs:578
   1: core::panicking::panic_fmt
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library\core\src\panicking.rs:67
   2: core::panicking::panic
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library\core\src\panicking.rs:117
   3: sandbox::tests::test_overflow::impl$1::try_from
             at .\src\lib.rs:5
   4: sandbox::tests::test_overflow
             at .\src\lib.rs:18
   5: sandbox::tests::test_overflow::closure$0
             at .\src\instruction\immediate.rs:5
   6: core::ops::function::FnOnce::call_once<sandbox::tests::test_overflow::closure_env$0,tuple$<> >
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca\library\core\src\ops\function.rs:250
   7: core::ops::function::FnOnce::call_once
             at /rustc/90c541806f23a127002de5b4038be731ba1458ca/library\core\src\ops\function.rs:250
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
hecatia-elegua commented 1 year ago

Hi there, thank you for the bug report. Would you want to pick this up? I can guide you a bit if you want to. The error is in our try_from_bits generation code, where I just used the wrong shift operator (>> instead of wrapping_shr).

sfleener commented 1 year ago

Sure I can take a look, seems fairly straightforward