-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Accessing union field inside unsafe function requires extra unsafe block #53193
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
Comments
huh... has it been this way since unions were introduced or is this a regression? |
No idea. I use unions a lot and this is the first time I see this. I did not managed to find an open issue about it. |
Ah! I overlooked the constant. Yea, the body of the constant is its own scope and does require its own unsafe blocks. This is working as intended to the best of my knowledge |
if you declare a safe function inside an unsafe block you don't expect to be able to use unsafe things inside that safe function either. pub unsafe fn foo() -> fn(*const u8) -> u8 {
fn helper(ptr: *const u8) -> u8 {
*ptr
}
helper
} |
Fun fact: this worked before Rust 1.6.0 (not for unions, of course) - #47864 (comment). |
duplicate of #35716 |
Thank you for the clarification @oli-obk - maybe that note ("the body of the constant is its own scope and requires its own unsafe block, just like safe functions inside unsafe functions") could be added to the error message in this case ? |
Playground
Is this by design?
it produces:
Wrapping the access in an
unsafe
block compiles, but since the function is alreadyunsafe
, that feels a bit redundant.In non-
unsafe
functions one doesn't need to open two unsafe blocks to perform union field accessunsafe { unsafe { ...union access... } }
:D Is this doubly-unsafe ? :Dcc @oli-obk @RalfJung @eddyb
The text was updated successfully, but these errors were encountered: