&raw const some_union.field
erroneously requires unsafe
#141264
Labels
C-discussion
Category: Discussion or questions that doesn't represent real issues.
T-lang
Relevant to the language team
T-opsem
Relevant to the opsem team
Uh oh!
There was an error while loading. Please reload this page.
I tried this code:
I expected to see this happen:
&raw mut self.x
should be a safe operation, because it consists only of in-bounds pointer arithmetic and does not construct a reference or value to uninit. An equivalent usingoffset_of!
is safe:Instead, this happened:
This indicates that Rust is blanket-banning access to the place
self.x
without consideration for how that place is used. This seems to be an oversight in addingptr::addr_of!
and the&raw
operation. It isunsafe
to construct a reference or move from such a place, but it should be safe to spell the place and get a raw pointer to it.This should instead act more like access to packed fields: spelling
packed_val.unaligned_field
only errors when constructing a reference to that field - merely spelling the place, moving, or using&raw const packed_val.unaligned_field
is accepted in safe code.Meta
rustc --version --verbose
:Occurs on current stable, beta, and nightly (
2025-05-18 4d051fb306e661654d08
).No backtrace was emitted.
The text was updated successfully, but these errors were encountered: