Skip to content

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

Description

@DanielJoyce

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?

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-langRelevant to the language team, which will review and decide on the RFC.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions