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

Panic when creating struct with single enum field #36

Closed
sfleener opened this issue Jun 11, 2023 · 2 comments · Fixed by #38
Closed

Panic when creating struct with single enum field #36

sfleener opened this issue Jun 11, 2023 · 2 comments · Fixed by #38

Comments

@sfleener
Copy link
Contributor

sfleener commented Jun 11, 2023

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
Copy link
Owner

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
Copy link
Contributor Author

Sure I can take a look, seems fairly straightforward

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants