Repository navigation
Document which union patterns are unsafe #2350
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -124,6 +124,19 @@ fn f(u: MyUnion) { | |
| } | ||
| ``` | ||
|
|
||
| `unsafe` is only required if the union field is accessed, in whole or in part, by the pattern, including any of its sub-patterns. For the purpose of this requirement, the following patterns are considered to perform an access: | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. So if we add a new pattern kind and forget to add it here, then we're implicitly specifying that as being allowed? That seems like a dangerous default.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is, but on the other hand:
I think we should just add a comment to
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Why? isn't it something like, a pattern matching on a
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think reasoning in terms of what code doesn't do is inherently more fraught than reasoning about what it does. That's the approach we take in https://doc.rust-lang.org/reference/unsafety.html: we list all the unsafe operations, not all the operations that aren't unsafe.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the opposite is true, it's much more reliable to enumerate what is safe than what isn't. For Rust generally this isn't a great default because most operations are safe, but when we are talking about unions it is exactly the right default IMO. |
||
|
|
||
| - [Literal patterns](../patterns.md#literal-patterns) | ||
| - [Identifier patterns](../patterns.md#identifier-patterns) | ||
| - [Range patterns](../patterns.md#range-patterns) | ||
| - [Reference patterns](../patterns.md#reference-patterns) | ||
|
Jules-Bertholet marked this conversation as resolved.
|
||
| - [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]`. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
|
|
||
| > [!NOTE] | ||
| > [Slice patterns](../patterns.md#slice-patterns) other than `[..]`, when matching against slices of dynamic size, could also be considered to access the union. However, union fields must implement `Sized`, so the rules regarding patterns matching reference values already account for this case. | ||
|
|
||
| > [!WARNING] | ||
| > The order in which the subpatterns of a pattern are tested is not specified. A union field named in a pattern may be read even when the pattern as a whole does not match. Reading a union field is undefined behavior unless it holds a valid value of its type (see [items.union.fields.validity]). Nothing else in the pattern can be relied on to prevent the read. | ||
| > | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.