Make option-like-enum.rs UB-free and portable

This commit is contained in:
Ben Kimock 2024-08-27 19:10:22 -04:00
parent 1a94d839be
commit 1a2ec2634e

View File

@ -8,22 +8,22 @@
// gdb-command:run // gdb-command:run
// gdb-command:print some // gdb-command:print some
// gdb-check:$1 = core::option::Option<&u32>::Some(0x12345678) // gdb-check:$1 = core::option::Option<&u32>::Some(0x[...])
// gdb-command:print none // gdb-command:print none
// gdb-check:$2 = core::option::Option<&u32>::None // gdb-check:$2 = core::option::Option<&u32>::None
// gdb-command:print full // gdb-command:print full
// gdb-check:$3 = option_like_enum::MoreFields::Full(454545, 0x87654321, 9988) // gdb-check:$3 = option_like_enum::MoreFields::Full(454545, 0x[...], 9988)
// gdb-command:print empty_gdb.discr // gdb-command:print empty
// gdb-check:$4 = (*mut isize) 0x1 // gdb-check:$4 = option_like_enum::MoreFields::Empty
// gdb-command:print droid // gdb-command:print droid
// gdb-check:$5 = option_like_enum::NamedFields::Droid{id: 675675, range: 10000001, internals: 0x43218765} // gdb-check:$5 = option_like_enum::NamedFields::Droid{id: 675675, range: 10000001, internals: 0x[...]}
// gdb-command:print void_droid_gdb.internals // gdb-command:print void_droid
// gdb-check:$6 = (*mut isize) 0x1 // gdb-check:$6 = option_like_enum::NamedFields::Void
// gdb-command:print nested_non_zero_yep // gdb-command:print nested_non_zero_yep
// gdb-check:$7 = option_like_enum::NestedNonZero::Yep(10.5, option_like_enum::NestedNonZeroField {a: 10, b: 20, c: 0x[...]}) // gdb-check:$7 = option_like_enum::NestedNonZero::Yep(10.5, option_like_enum::NestedNonZeroField {a: 10, b: 20, c: 0x[...]})
@ -39,19 +39,19 @@
// lldb-command:run // lldb-command:run
// lldb-command:v some // lldb-command:v some
// lldb-check:[...] Some(&0x12345678) // lldb-check:[...] Some(&0x[...])
// lldb-command:v none // lldb-command:v none
// lldb-check:[...] None // lldb-check:[...] None
// lldb-command:v full // lldb-command:v full
// lldb-check:[...] Full(454545, &0x87654321, 9988) // lldb-check:[...] Full(454545, &0x[...], 9988)
// lldb-command:v empty // lldb-command:v empty
// lldb-check:[...] Empty // lldb-check:[...] Empty
// lldb-command:v droid // lldb-command:v droid
// lldb-check:[...] Droid { id: 675675, range: 10000001, internals: &0x43218765 } // lldb-check:[...] Droid { id: 675675, range: 10000001, internals: &0x[...] }
// lldb-command:v void_droid // lldb-command:v void_droid
// lldb-check:[...] Void // lldb-check:[...] Void
@ -76,11 +76,6 @@
// contains a non-nullable pointer, then this value is used as the discriminator. // contains a non-nullable pointer, then this value is used as the discriminator.
// The test cases in this file make sure that something readable is generated for // The test cases in this file make sure that something readable is generated for
// this kind of types. // this kind of types.
// If the non-empty variant contains a single non-nullable pointer than the whole
// item is represented as just a pointer and not wrapped in a struct.
// Unfortunately (for these test cases) the content of the non-discriminant fields
// in the null-case is not defined. So we just read the discriminator field in
// this case (by casting the value to a memory-equivalent struct).
enum MoreFields<'a> { enum MoreFields<'a> {
Full(u32, &'a isize, i16), Full(u32, &'a isize, i16),
@ -120,32 +115,26 @@ fn main() {
let some_str: Option<&'static str> = Some("abc"); let some_str: Option<&'static str> = Some("abc");
let none_str: Option<&'static str> = None; let none_str: Option<&'static str> = None;
let some: Option<&u32> = Some(unsafe { std::mem::transmute(0x12345678_usize) }); let some: Option<&u32> = Some(&1234);
let none: Option<&u32> = None; let none: Option<&u32> = None;
let full = MoreFields::Full(454545, unsafe { std::mem::transmute(0x87654321_usize) }, 9988); let full = MoreFields::Full(454545, &1234, 9988);
let empty = MoreFields::Empty; let empty = MoreFields::Empty;
let empty_gdb: &MoreFieldsRepr = unsafe { std::mem::transmute(&MoreFields::Empty) };
let droid = NamedFields::Droid { let droid = NamedFields::Droid {
id: 675675, id: 675675,
range: 10000001, range: 10000001,
internals: unsafe { std::mem::transmute(0x43218765_usize) } internals: &1234,
}; };
let void_droid = NamedFields::Void; let void_droid = NamedFields::Void;
let void_droid_gdb: &NamedFieldsRepr = unsafe { std::mem::transmute(&NamedFields::Void) };
let x = 'x';
let nested_non_zero_yep = NestedNonZero::Yep( let nested_non_zero_yep = NestedNonZero::Yep(
10.5, 10.5,
NestedNonZeroField { NestedNonZeroField {
a: 10, a: 10,
b: 20, b: 20,
c: &x c: &'x',
}); });
let nested_non_zero_nope = NestedNonZero::Nope; let nested_non_zero_nope = NestedNonZero::Nope;
zzz(); // #break zzz(); // #break