Repository navigation
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-help-wantedCall for participation: Help is requested to fix this issue.Call for participation: Help is requested to fix this issue.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.Call for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
It'd be nice to smartly hint about missing tuple parentheses in patterns, especially in positions where parentheses nesting is required.
Example of incorrect code:
fn main() {
match Some((1, 2)) {
Some(x, y) => {}
None => {}
}
}The error:
error[E0023]: this pattern has 2 fields, but the corresponding tuple variant has 1 field
--> src/main.rs:3:9
|
3 | Some(x, y) => {}
| ^^^^^^^^^^ expected 1 field, found 2This error is confusing, especially since Some is a "tuple pattern" which is confusable with its tuple field. An error that suggests missing tuple parentheses would be helpful.
This issue has been assigned to @sam09 via this comment.
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`Area: Suggestions generated by the compiler applied by `cargo fix`C-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.E-help-wantedCall for participation: Help is requested to fix this issue.Call for participation: Help is requested to fix this issue.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.Call for participation: Medium difficulty. Experience needed to fix: Intermediate.E-mentorCall for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.Call for participation: This issue has a mentor. Use #t-compiler/help on Zulip for discussion.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Activity
Centril commentedon Aug 27, 2019
I think the right approach here would be to check if the scrutinee type (expected type in this context) has a single field of a tuple type with as many fields as the tuple struct pattern has.
Place to change:
rust/src/librustc_typeck/check/pat.rs
Lines 678 to 688 in 53df91a
If you tackle this, first please also refactor the snippet highlighted above into a diagnostics-only method and then do your changes in there.
sam09 commentedon Aug 31, 2019
Can I pick up this one? Doesn't look like anybody working on this.
zackmdavis commentedon Aug 31, 2019
@sam09 Please do.
@rustbot assign @sam09
sam09 commentedon Sep 1, 2019
variant.fields.len()gives me the number of fields of expected type, which should be 1 here. I have a condition,varaints.fields.len()==1. However I can't see how many fieldsvaraints.field[0]has, when trying to compare to the fields the tuple struct pattern has (subpats.len()in this case)Maybe I misunderstood it, or am missing something here
sam09 commentedon Sep 7, 2019
This #64161 seems to have changed some things here. Is this issue still relevant?
Centril commentedon Sep 7, 2019
cc @estebank
Ostensibly it is still relevant as we could emit a specific suggestion in this case.
estebank commentedon Sep 7, 2019
Yes, that PR only adds a bit of context that should generally be useful, but this is about adding a structured suggestion for this case in particular. This is more focused and more useful for this case in particular :)
Looking forward to it!
sam09 commentedon Sep 8, 2019
Thanks @Centril @estebank. I have one more question though,
rust/src/librustc_typeck/check/pat.rs
Lines 686 to 709 in 24d0a01
This
fieldsparameter in the above function gives the number of fields of expected type, whilesubpatsgives me the number of fields in the tuple pattern.The condition to show a missing parenthesis message should be something like:
But fields[0] is not a vector and I cannot seem to figure out how to find the number of elements in the expected type
Centril commentedon Sep 8, 2019
You'll need to look at
expected: Ty<'tcx>to see that it resolves to anTyKind::Adt(adt_def, substs)and then you'll need to match up thevariantwith the right one in theAdtDef. Once you have the rightvariant, you'll need to check that it has one field and extract it. You'll need to check that the single field is also a tuple and match the length of that tuple up withsubpats.len(). You'll also need to handle the possibility that the enum is generic, e.g. as in the case ofOption<T>and usesubststo account for this.Rollup merge of rust-lang#64619 - sam09:fix-63962, r=Centril
Auto merge of #64666 - Centril:rollup-tp98vlr, r=Centril