-
-
Notifications
You must be signed in to change notification settings - Fork 14.9k
Syntactically reject tuple index shorthands in struct patterns to fix a correctness regression #155698
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
Merged
rust-bors
merged 1 commit into
rust-lang:main
from
fmease:no-struct-pat-tuple-index-shorthand
Apr 24, 2026
Merged
Syntactically reject tuple index shorthands in struct patterns to fix a correctness regression #155698
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| // Check that tuple indices in struct exprs & pats don't have a shorthand. | ||
| // If they did it would be possible to bind and reference numeric identifiers | ||
| // which is undesirable. | ||
|
|
||
| struct Rgb(u8, u8, u8); | ||
|
|
||
| #[cfg(false)] // ensures that this is a *syntax* error, not just a semantic one! | ||
| fn scope() { | ||
| // FIXME: Better recover and also report a diagnostic for the other two fields. | ||
| let Rgb { 0, 1, 2 }; | ||
| //~^ ERROR expected identifier, found `0` | ||
|
|
||
| let _ = Rgb { 0, 1, 2 }; | ||
| //~^ ERROR expected identifier, found `0` | ||
| //~| ERROR expected identifier, found `1` | ||
| //~| ERROR expected identifier, found `2` | ||
| } | ||
|
|
||
| fn main() {} |
16 changes: 12 additions & 4 deletions
16
...ser/struct-field-numeric-shorthand.stderr → ...uct-expr-pat-tuple-index-shorthand.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,34 @@ | ||
| error: expected identifier, found `0` | ||
| --> $DIR/struct-field-numeric-shorthand.rs:4:19 | ||
| --> $DIR/struct-expr-pat-tuple-index-shorthand.rs:10:15 | ||
| | | ||
| LL | let Rgb { 0, 1, 2 }; | ||
| | --- ^ expected identifier | ||
| | | | ||
| | while parsing the fields for this pattern | ||
|
|
||
| error: expected identifier, found `0` | ||
| --> $DIR/struct-expr-pat-tuple-index-shorthand.rs:13:19 | ||
| | | ||
| LL | let _ = Rgb { 0, 1, 2 }; | ||
| | --- ^ expected identifier | ||
| | | | ||
| | while parsing this struct | ||
|
|
||
| error: expected identifier, found `1` | ||
| --> $DIR/struct-field-numeric-shorthand.rs:4:22 | ||
| --> $DIR/struct-expr-pat-tuple-index-shorthand.rs:13:22 | ||
| | | ||
| LL | let _ = Rgb { 0, 1, 2 }; | ||
| | --- ^ expected identifier | ||
| | | | ||
| | while parsing this struct | ||
|
|
||
| error: expected identifier, found `2` | ||
| --> $DIR/struct-field-numeric-shorthand.rs:4:25 | ||
| --> $DIR/struct-expr-pat-tuple-index-shorthand.rs:13:25 | ||
| | | ||
| LL | let _ = Rgb { 0, 1, 2 }; | ||
| | --- ^ expected identifier | ||
| | | | ||
| | while parsing this struct | ||
|
|
||
| error: aborting due to 3 previous errors | ||
| error: aborting due to 4 previous errors | ||
|
|
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| // On unmentioned tuple indices in struct patterns, don't suggest turning the pattern into a | ||
| // tuple struct pattern and keep the struct pattern in the suggestion. | ||
| // issue: <https://github.com/rust-lang/rust/issues/108284> | ||
|
|
||
| struct S(i32, f32); | ||
| enum E { V(i32, f32) } | ||
|
|
||
| fn main() { | ||
| let S { 0: _ } = S(1, 2.2); //~ ERROR: pattern does not mention field `1` | ||
| let E::V { 0: _ } = E::V(1, 2.2); //~ ERROR: pattern does not mention field `1` | ||
| } |
41 changes: 41 additions & 0 deletions
41
tests/ui/structs/struct-pat-unmentioned-tuple-indices.stderr
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| error[E0027]: pattern does not mention field `1` | ||
| --> $DIR/struct-pat-unmentioned-tuple-indices.rs:9:9 | ||
| | | ||
| LL | let S { 0: _ } = S(1, 2.2); | ||
| | ^^^^^^^^^^ missing field `1` | ||
| | | ||
| help: include the missing field in the pattern | ||
| | | ||
| LL | let S { 0: _, 1: _ } = S(1, 2.2); | ||
| | ++++++ | ||
| help: if you don't care about this missing field, you can explicitly ignore it | ||
| | | ||
| LL | let S { 0: _, 1: _ } = S(1, 2.2); | ||
| | ++++++ | ||
| help: or always ignore missing fields here | ||
| | | ||
| LL | let S { 0: _, .. } = S(1, 2.2); | ||
| | ++++ | ||
|
|
||
| error[E0027]: pattern does not mention field `1` | ||
| --> $DIR/struct-pat-unmentioned-tuple-indices.rs:10:9 | ||
| | | ||
| LL | let E::V { 0: _ } = E::V(1, 2.2); | ||
| | ^^^^^^^^^^^^^ missing field `1` | ||
| | | ||
| help: include the missing field in the pattern | ||
| | | ||
| LL | let E::V { 0: _, 1: _ } = E::V(1, 2.2); | ||
| | ++++++ | ||
| help: if you don't care about this missing field, you can explicitly ignore it | ||
| | | ||
| LL | let E::V { 0: _, 1: _ } = E::V(1, 2.2); | ||
| | ++++++ | ||
| help: or always ignore missing fields here | ||
| | | ||
| LL | let E::V { 0: _, .. } = E::V(1, 2.2); | ||
| | ++++ | ||
|
|
||
| error: aborting due to 2 previous errors | ||
|
|
||
| For more information about this error, try `rustc --explain E0027`. |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
7 changes: 7 additions & 0 deletions
7
tests/ui/tuple/tuple-variant-written-as-empty-struct-variant.rs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| struct S(i32); | ||
| enum E { V(i32) } | ||
|
|
||
| fn main() { | ||
| let S {} = S(0); //~ ERROR tuple variant `S` written as struct variant | ||
| let E::V {} = E::V(0); //~ ERROR tuple variant `E::V` written as struct variant | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This was the culprit.
Prior to the regressing PR #81235 this used to be
let fieldname = self.parse_ident()?;.Changing it (back) to
parse_ident_commonmakes it consistent again with its expression counterpart, cf:rust/compiler/rustc_parse/src/parser/expr.rs
Line 4046 in 36ba2c7
View changes since the review