rust-lang / rust-lang/rust

Under feature `unsafe_fields`, `unsafe` union fields are semantically permitted thereby contradicting the RFC

Open
#157,019 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug F-unsafe_fields T-compiler
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

RFC 3458 states:

The use of unsafe fields on unions shall remain [semantically] forbidden while the impact of this feature on unions is decided.

"impact of this feature on unions" links to Future possibility "Safe Unions", so it doesn't seem "decided".

However, the compiler accepts this code:

#![feature(unsafe_fields)]

union U { unsafe f: () }

That's most likely the case because the reviewer of the initial implementation seemingly requested this behavior in https://github.com/rust-lang/rust/pull/132915#issuecomment-2486369964 (but it's unclear whether they were referring to the syntax or the semantics which might have been the cause of this confusion).

The current behavior is exercised in tests/ui/unsafe-fields/unsafe-fields.rs.

I've grepped through the PR comments of the RFC and of the initial implementation but couldn't find anything that would explain this discrepancy.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with RFC 3458, the linked Safe Unions discussion, and the initial implementation review comment. Then inspect tests/ui/unsafe-fields/unsafe-fields.rs and the compiler behavior for union U { unsafe f: () }. Done means the intended semantic rule is resolved and the implementation and test coverage agree with it.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
compilers
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.