Skip to content

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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

Kivooeo
Copy link
Contributor

@Kivooeo Kivooeo commented May 23, 2025

fixes #141264

r? @Veykril

Unresolved questions:

  • Any edge cases?
  • How this works with rust-analyzer (because all I've did is prevent compiler from emitting error in &raw context)
  • Should we allow addr_of! and addr_of_mut! as well? In current version they both (&raw and addr_of!) are allowed

@rustbot
Copy link
Collaborator

rustbot commented May 23, 2025

Failed to set assignee to Veykril: invalid assignee

Note: Only org members with at least the repository "read" role, users with write permissions, or people who have commented on the PR may be assigned.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 23, 2025
@jieyouxu
Copy link
Member

cc @RalfJung

@fmease fmease added the I-lang-nominated Nominated for discussion during a lang team meeting. label May 23, 2025
@Kivooeo Kivooeo changed the title Allow &raw [mut | const] for union field in safe Allow &raw [mut | const] for union field in safe May 23, 2025
@fmease fmease added the needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. label May 23, 2025
@RalfJung
Copy link
Member

Uh, the unsafety checker is not actually code I know very well, sorry.
r? compiler

@rustbot rustbot assigned davidtwco and unassigned RalfJung May 23, 2025
@RalfJung
Copy link
Member

Should we allow addr_of! and addr_of_mut! as well? In current version they both (&raw and addr_of!) are allowed

The macros just expand to &raw, it's not even possible to treat them differently.

@rust-log-analyzer

This comment has been minimized.

@Kivooeo
Copy link
Contributor Author

Kivooeo commented May 23, 2025

I will debug this later today but this is very weird

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
Copy link
Member

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.

Copy link
Contributor Author

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

@fmease fmease added T-lang Relevant to the language team, which will review and decide on the PR/issue. S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed I-lang-nominated Nominated for discussion during a lang team meeting. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 24, 2025
@Kivooeo Kivooeo force-pushed the remove-usnsafegate branch from b006128 to 43892d3 Compare May 24, 2025 11:46
@rust-log-analyzer

This comment has been minimized.

@Kivooeo Kivooeo force-pushed the remove-usnsafegate branch from b60d3ff to fc03435 Compare May 24, 2025 12:03
@rust-log-analyzer

This comment has been minimized.

@Kivooeo Kivooeo force-pushed the remove-usnsafegate branch from 9ccf35d to cde9fa0 Compare May 24, 2025 12:48
@Kivooeo
Copy link
Contributor Author

Kivooeo commented May 24, 2025

@rustbot ready

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels May 24, 2025
Copy link
Member

@Veykril Veykril left a 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).

@Veykril
Copy link
Member

Veykril commented May 26, 2025

  • How this works with rust-analyzer (because all I've did is prevent compiler from emitting error in &raw context)

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)

@Kivooeo
Copy link
Contributor Author

Kivooeo commented May 26, 2025

@Veykril

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)

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 :)

rust-analyzer has its own unsafety check here

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?

@Veykril
Copy link
Member

Veykril commented May 26, 2025

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

@Kivooeo
Copy link
Contributor Author

Kivooeo commented May 26, 2025

I will try to found out what exactly closure captures later today, I guess that MIR or HIR could show me this information

@RalfJung
Copy link
Member

@lcnr @compiler-errors do you know the intended spec for closure captures around unions? We never do field capturing there, do we?

@Kivooeo
Copy link
Contributor Author

Kivooeo commented May 26, 2025

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;
    }

@compiler-errors
Copy link
Member

We truncate precise captures of union fields to the whole union here:

if place.base_ty.is_union() {
truncate_place_to_len_and_update_capture_kind(&mut place, &mut curr_mode, 0);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-fcp This change is insta-stable, or significant enough to need a team FCP to proceed. S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

&raw const some_union.field erroneously requires unsafe
9 participants