Closed
Description
Is this by design?
pub unsafe fn foo() -> u8 {
union U {
v: u8,
c: u8,
}
// const C: u8 = unsafe { U { v: 0 }.c }; // OK
const C: u8 = U { v: 0 }.c; // ERROR
C
}
it produces:
error[E0133]: access to union field is unsafe and requires unsafe function or block
--> src/main.rs:9:19
|
9 | const C: u8 = U { v: 0 }.c;
| ^^^^^^^^^^^^ access to union field
|
= note: the field may not be properly initialized: using uninitialized data will cause undefined behavior
Wrapping the access in an unsafe
block compiles, but since the function is already unsafe
, that feels a bit redundant.
In non-unsafe
functions one doesn't need to open two unsafe blocks to perform union field access unsafe { unsafe { ...union access... } }
:D Is this doubly-unsafe ? :D
Metadata
Metadata
Assignees
Labels
No labels