Skip to content

Document which union patterns are unsafe - #2350

Draft
Jules-Bertholet wants to merge 4 commits into
rust-lang:masterfrom
Jules-Bertholet:safe-union-leaf-patterns
Draft

Jules-Bertholet wants to merge 4 commits into
rust-lang:masterfrom
Jules-Bertholet:safe-union-leaf-patterns

Conversation

@Jules-Bertholet

@Jules-Bertholet Jules-Bertholet commented Sep 2, 2026

Copy link
Copy Markdown
Contributor

@theemathas

Copy link
Copy Markdown
Contributor

I think this kind of definition of what patterns "access" a thing could also be useful for implementing a lint for rust-lang/rust#158387 (comment). cc @Nadrieril

Comment thread src/items/unions.md
Comment thread src/items/unions.md
Comment thread src/items/unions.md
- [Reference patterns](../patterns.md#reference-patterns)
- [Non-reference patterns](../patterns.md#r-patterns.ident.binding.non-reference) matching reference values
- [Struct](../patterns.md#struct-patterns) and [tuple struct](../patterns.md#tuple-struct-patterns) patterns which correspond to an enum variant
- [Path patterns](../patterns.md#path-patterns), if the result of expanding the constant into a pattern contains one of the above, or if the expanded pattern could not have been written directly at the location where it is used due to field privacy or `#[non_exhaustive]`.

@Nadrieril Nadrieril Sep 21, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd like to add constant patterns there, because it should be allowed to turn a constant patter nmatch into a call to PartialEq::eq for performance. This conflicts with https://doc.rust-lang.org/reference/patterns.html?highlight=patterns#r-patterns.const.translation a bit so we conceptually are saying "unsafety checking is done before we expand constant patterns"

View changes since the review

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants