-
Notifications
You must be signed in to change notification settings - Fork 13.4k
Allow &raw [mut | const]
for union field in safe
#141469
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
base: master
Are you sure you want to change the base?
Conversation
Failed to set assignee to
|
cc @RalfJung |
&raw [mut | const]
for union field in safe
Uh, the unsafety checker is not actually code I know very well, sorry. |
The macros just expand to |
This comment has been minimized.
This comment has been minimized.
I will debug this later today but this is very weird |
tests/ui/union/union-unsafe.rs
Outdated
let u4 = U5 { a: 2 }; | ||
let vec = vec![1, 2, 3]; | ||
let _a = &raw const vec[u4.a]; //~ ERROR access to union field is unsafe | ||
//~^ ERROR access to union field is unsafe |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something is wrong here, the error should not be emitted twice.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, this should be WAY better now, also added more comments for tests and code and also removed this global state
b006128
to
43892d3
Compare
This comment has been minimized.
This comment has been minimized.
b60d3ff
to
fc03435
Compare
This comment has been minimized.
This comment has been minimized.
9ccf35d
to
cde9fa0
Compare
@rustbot ready |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As was brought up in the zulip thread, there should probably be a test for raw borrowing a union field in a closure capture like || &raw u2.a
or so. Iirc the question there was whether the closure captures u2
(which would be fine), or u2.a
(which would itself still be unsafe as its a read, be it a borrow or move/copy).
rust-analyzer has its own unsafety check here: https://github.com/rust-lang/rust/blob/46264e6dfd8f0bacae05c520b4617e054d6ef990/src/tools/rust-analyzer/crates/hir-ty/src/diagnostics/unsafe_check.rs with tests living here https://github.com/rust-lang/rust/blob/46264e6dfd8f0bacae05c520b4617e054d6ef990/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/missing_unsafe.rs If you can look into that together with this PR that would be appreciated, but if not that's also okay (iirc you can't really run specific tests for r-a in the rust repo yet, correct me if I am wrong @jieyouxu) |
In current state this is not emitting error, but, I'm unsure if it should, this is pretty tricky quesiton I have no answer for, this is actually my first interaction with unions :)
Sure! I will check it, would it be better to open PR (if I found a way to fix this or just issue if not) in RA's repo? |
It would probably be easier to work in the rust-analyzer repo directly yes |
I will try to found out what exactly closure captures later today, I guess that MIR or HIR could show me this information |
@lcnr @compiler-errors do you know the intended spec for closure captures around unions? We never do field capturing there, do we? |
Well, if I get this right, this capturing all union bb0: {
_1 = U { a: const 42_i32 };
_3 = &_1;
_2 = {closure@/Users/meow/projects/wtf/src/main.rs:10:13: 10:15} { u: move _3 };
return;
} |
We truncate precise captures of union fields to the whole union here: rust/compiler/rustc_hir_typeck/src/upvar.rs Lines 2155 to 2157 in 95a2212
|
fixes #141264
r? @Veykril
Unresolved questions:
&raw
context)addr_of!
andaddr_of_mut!
as well? In current version they both (&raw
andaddr_of!
) are allowed