Skip to content
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

Rust mutable union allows write to field w/o needing unsafe block in stable ( Documentation issue? ) #2095

Closed
DanielJoyce opened this issue Aug 3, 2017 · 3 comments
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.

Comments

@DanielJoyce
Copy link

https://play.rust-lang.org/?gist=beaba7cc10b93f3e6ac1d97717099f26&version=undefined

The code produces a warning:


   Compiling playground v0.0.1 (file:///playground)
warning: unnecessary `unsafe` block
 --> src/main.rs:9:5
  |
9 |     unsafe { u.f1 = 5 };
  |     ^^^^^^^^^^^^^^^^^^^ unnecessary `unsafe` block
  |
  = note: #[warn(unused_unsafe)] on by default

    Finished dev [unoptimized + debuginfo] target(s) in 0.55 secs
     Running `target/debug/playground`

Saying the unsafe block is not needed for a write.

But the rfc says writes to union mutable fields require a unsafe block

https://github.com/rust-lang/rfcs/blob/master/text/1444-union.md#writing-fields

Found here:

https://www.reddit.com/r/rust/comments/6rcegm/unsafe_field_accesses_for_unions/

On second reading, the RFC says "May" which is not the same as "Shall". Which seems weird. Do you HAVE to use unsafe code to write mutable union fields? Is it optional? When is it needed/not needed?

@petrochenkov
Copy link
Contributor

This is expected behavior, 1444 is outdated.
(I'll will write some details later.)

@petrochenkov
Copy link
Contributor

The change was done as a part of stabilization of Copy unions (rust-lang/rust#42083).
It was previously discussed in the tracking issue for unions (rust-lang/rust#32836 (comment) and below).
The motivation is that for Copy fields unsafe on writes is a false positive, field assignments u1.a = a are equivalent to whole union assignments u1 = u2 which are safe.
This was done before stabilization rather than later to avoid every write of a union field becoming a warning in stable code (possibly an error with deny(warnings)).

(Up-to-date documentation for unions (or at least their implementation) is #1897, the safe field writes are in Future directions / Unsafe blocks.)

@petrochenkov
Copy link
Contributor

The reference is up-to-date (https://doc.rust-lang.org/nightly/reference/items.html#unions), so this is not a documentation issue as well.

@DanielJoyce
You can send a PR to fix the release notes and the blog post, or you can just open issues in https://github.com/rust-lang/rust and https://github.com/rust-lang/blog.rust-lang.org respectively so someone else fixes it.
I'll close the issue here in the RFC repo because it's not an RFC issue.

@Ixrec Ixrec mentioned this issue Aug 5, 2017
@Centril Centril added the T-lang Relevant to the language team, which will review and decide on the RFC. label Feb 23, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
T-lang Relevant to the language team, which will review and decide on the RFC.
Projects
None yet
Development

No branches or pull requests

3 participants