8000 Suggest `_` and `..` if a pattern has too few fields by camelid · Pull Request #80017 · rust-lang/rust · GitHub
[go: up one dir, main page]

Skip to content

Suggest _ and .. if a pattern has too few fields #80017

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
merged 9 commits into from
Jan 14, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Always show suggestions in their own subwindows
  • Loading branch information
camelid committed Jan 13, 2021
commit d7307a71f5e1893f7bd69ff160ce38dc97b5d195
6 changes: 3 additions & 3 deletions compiler/rustc_typeck/src/check/pat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
wildcard_sugg = String::from(", ") + &wildcard_sugg;
}

err.span_suggestion_short(
err.span_suggestion_verbose(
after_fields_span,
"use `_` to explicitly ignore each field",
wildcard_sugg,
Expand All @@ -1071,14 +1071,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// Only suggest `..` if more than one field is missing.
if fields.len() - subpats.len() > 1 {
if subpats.is_empty() || all_wildcards {
err.span_suggestion_short(
err.span_suggestion_verbose(
all_fields_span,
"use `..` to ignore all fields",
String::from(".."),
Applicability::MaybeIncorrect,
);
} else {
err.span_suggestion_short(
err.span_suggestion_verbose(
after_fields_span,
"use `..` to ignore the rest of the fields",
String::from(", .."),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ LL | struct TupleStruct<S, T>(S, T);
| ------------------------------- tuple struct defined here
...
LL | TupleStruct(_) = TupleStruct(1, 2);
| ^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1
| ^^^^^^^^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | TupleStruct(_, _) = TupleStruct(1, 2);
| ^^^
Comment on lines +34 to +38
Copy link
Contributor

Choose a reason for hiding this comment

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

These two cases should also be suggesting .. as an alternative, no?

Copy link
Member Author

Choose a reason for hiding this comment

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

Well, if someone is already explicitly listing out wildcards, should we suggest that they use ..? On the other hand, someone may not know that .. is an option and would rather that over listing out wildcards manually. So I think overall I agree with you that we should be giving it as an alternative :)

I'll change it, but for other things we can always adjust the behavior later; I'm hoping to have this PR finished soon.

Copy link
Contributor

Choose a reason for hiding this comment

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

Sounds reasonable, I don't foresee any big changes. I think that the code itself looks good but want to nail down the exact behavior we want.

Copy link
Member Author

Choose a reason for hiding this comment

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

So is the behavior you're suggesting that we also suggest S(..) if it's currently of the form S(_, _, _)?

Copy link
Contributor
@estebank estebank Jan 13, 2021

Choose a reason for hiding this comment

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

Yes, I think that if it is currently S(_, _) and we suggest S(_, _, _) we should also suggest S(..). Having said that, if you find that it will be too much work, only create a follow-up ticket and r=me.


error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
--> $DIR/tuple_struct_destructure_fail.rs:34:5
Expand All @@ -51,10 +53,12 @@ LL | SingleVariant(S, T)
| ------------------- tuple variant defined here
...
LL | Enum::SingleVariant(_) = Enum::SingleVariant(1, 2);
| ^^^^^^^^^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1
| ^^^^^^^^^^^^^^^^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | Enum::SingleVariant(_, _) = Enum::SingleVariant(1, 2);
| ^^^

error[E0070]: invalid left-hand side of assignment
--> $DIR/tuple_struct_destructure_fail.rs:40:12
Expand Down
8000
10 changes: 6 additions & 4 deletions src/test/ui/error-codes/E0023.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ LL | Apple(String, String),
| --------------------- tuple variant defined here
...
LL | Fruit::Apple(a) => {},
| ^^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1
| ^^^^^^^^^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | Fruit::Apple(a, _) => {},
| ^^^

error[E0023]: this pattern has 3 fields, but the corresponding tuple variant has 2 fields
--> $DIR/E0023.rs:12:9
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ LL | struct P<T>(T); // 1 type parameter wanted
| --------------- tuple struct defined here
...
LL | let P() = U {};
| ^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 1 field, found 0
| ^^^ expected 1 field, found 0
|
help: use `_` to explicitly ignore each field
|
LL | let P(_) = U {};
| ^

error: aborting due to 2 previous errors

Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/issues/issue-72574-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ LL | struct Binder(i32, i32, i32);
| ----------------------------- tuple struct defined here
...
LL | Binder(_a, _x @ ..) => {}
| ^^^^^^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 3 fields, found 2
| ^^^^^^^^^^^^^^^^^^^ expected 3 fields, found 2
|
help: use `_` to explicitly ignore each field
|
LL | Binder(_a, _x @ .., _) => {}
| ^^^

error: aborting due to 3 previous errors

Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/match/match-pattern-field-mismatch.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ LL | Rgb(usize, usize, usize),
| ------------------------ tuple variant defined here
...
LL | Color::Rgb(_, _) => { }
| ^^^^^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 3 fields, found 2
| ^^^^^^^^^^^^^^^^ expected 3 fields, found 2
|
help: use `_` to explicitly ignore each field
|
LL | Color::Rgb(_, _, _) => { }
| ^^^

error: aborting due to previous error

Expand Down
10 changes: 6 additions & 4 deletions src/test/ui/pattern/issue-74539.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@ LL | A(u8, u8),
| --------- tuple variant defined here
...
LL | E::A(x @ ..) => {
| ^^^^^^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1
| ^^^^^^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | E::A(x @ .., _) => {
| ^^^

error: aborting due to 3 previous errors

Expand Down
40 changes: 24 additions & 16 deletions src/test/ui/pattern/pat-tuple-underfield.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ LL | struct S(i32, f32);
| ------------------- tuple struct defined here
...
LL | S(x) => {}
| ^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1
| ^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | S(x, _) => {}
| ^^^

error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 2 fields
--> $DIR/pat-tuple-underfield.rs:14:9
Expand All @@ -26,10 +28,12 @@ LL | struct S(i32, f32);
| ------------------- tuple struct defined here
...
LL | S(_) => {}
| ^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1
| ^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | S(_, _) => {}
| ^^^

error[E0023]: this pattern has 0 fields, but the corresponding tuple struct has 2 fields
--> $DIR/pat-tuple-underfield.rs:19:9
Expand All @@ -56,10 +60,12 @@ LL | S(i32, f32),
| ----------- tuple variant defined here
...
LL | E::S(x) => {}
| ^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1
| ^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | E::S(x, _) => {}
| ^^^

error[E0023]: this pattern has 1 field, but the corresponding tuple variant has 2 fields
--> $DIR/pat-tuple-underfield.rs:31:9
Expand All @@ -68,10 +74,12 @@ LL | S(i32, f32),
| ----------- tuple variant defined here
...
LL | E::S(_) => {}
| ^^^^^^-
| | |
| | help: use `_` to explicitly ignore each field
| expected 2 fields, found 1
| ^^^^^^^ expected 2 fields, found 1
|
help: use `_` to explicitly ignore each field
|
LL | E::S(_, _) => {}
| ^^^

error[E0023]: this pattern has 0 fields, but the corresponding tuple variant has 2 fields
--> $DIR/pat-tuple-underfield.rs:36:9
Expand Down
0