You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
error:UndefinedBehavior: constructing invalid value at .<enum-tag>: encountered 0x01, but expected a valid enum tag
--> src/main.rs:5:13
|
5 | unsafe{std::mem::transmute::<_,One>(one)};
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ constructing invalid value at .<enum-tag>: encountered 0x01, but expected a valid enum tag
|
= help: this indicates a bug in the program: it performed an invalid operation, and caused UndefinedBehavior
= help: see https://doc.rust-lang.org/nightly/reference/behavior-considered-undefined.html for further information
= note:BACKTRACE:
= note: inside `main` at src/main.rs:5:13
note: some details are omitted, run with `MIRIFLAGS=-Zmiri-backtrace=full` for a verbose backtrace
For both layouts, it is defined for Rust programs to cast/reinterpret/transmute such an enum into the equivalent Repr definition. Separately manipulating the tag and payload is also defined. The tag and payload need only be in a consistent/initialized state when the value is matched on (which includes Dropping it).
The text was updated successfully, but these errors were encountered:
RFCs generally document the consensus at the time, but are not updated when things change later. The Reference is the source for current information, though it is unfortunately rather incomplete on unsafe code details. That is something we are working on, but writing a proper spec takes time. The reference however is pretty clear on this particular topic, see "producing an invalid value" on the UB page.
Thanks for the report though! It's good to know that we have an RFC that contradicts today's understanding of validity invariants. I'm bringing this up for discussion in Zulip, too.
Note that the parse_my_enum_from example in the RFC is actually fine under the current understanding of the rules, though this is somewhat pending on rust-lang/unsafe-code-guidelines#84.
See https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=d382170468c9e33175776afb6e9c3df3
Gives the error:
But per RFC 2195, this is valid:
The text was updated successfully, but these errors were encountered: