Skip to content
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

Fix diagnostics for @ .. binding pattern in tuples and tuple structs #72677

Merged
merged 1 commit into from
May 30, 2020

Conversation

chrissimpkins
Copy link
Member

@chrissimpkins chrissimpkins commented May 28, 2020

Fixes #72574
Associated #72534 #72373

Includes a new suggestion with Applicability::MaybeIncorrect confidence level.

Before

tuple

error: `..` patterns are not allowed here
 --> src/main.rs:4:19
  |
4 |         (_a, _x @ ..) => {}
  |                   ^^
  |
  = note: only allowed in tuple, tuple struct, and slice patterns

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
3 |     match x {
  |           - this expression has type `({integer}, {integer}, {integer})`
4 |         (_a, _x @ ..) => {}
  |         ^^^^^^^^^^^^^ expected a tuple with 3 elements, found one with 2 elements
  |
  = note: expected tuple `({integer}, {integer}, {integer})`
             found tuple `(_, _)`

error: aborting due to 2 previous errors

tuple struct

error: `..` patterns are not allowed here
 --> src/main.rs:6:25
  |
6 |         Binder(_a, _x @ ..) => {}
  |                         ^^
  |
  = note: only allowed in tuple, tuple struct, and slice patterns

error[E0023]: this pattern has 2 fields, but the corresponding tuple struct has 3 fields
 --> src/main.rs:6:9
  |
1 | struct Binder(i32, i32, i32);
  | ----------------------------- tuple struct defined here
...
6 |         Binder(_a, _x @ ..) => {}
  |         ^^^^^^^^^^^^^^^^^^^ expected 3 fields, found 2

error: aborting due to 2 previous errors

After

Note: final output edited during source review discussion, see thread for details

tuple

error: `_x @` is not allowed in a tuple
 --> src/main.rs:4:14
  |
4 |         (_a, _x @ ..) => {}
  |              ^^^^^^^ is only allowed in a slice
  |
help: replace with `..` or use a different valid pattern
  |
4 |         (_a, ..) => {}
  |              ^^

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
3 |     match x {
  |           - this expression has type `({integer}, {integer}, {integer})`
4 |         (_a, _x @ ..) => {}
  |         ^^^^^^^^^^^^^ expected a tuple with 3 elements, found one with 1 element
  |
  = note: expected tuple `({integer}, {integer}, {integer})`
             found tuple `(_,)`

error: aborting due to 2 previous errors

tuple struct

error: `_x @` is not allowed in a tuple struct
 --> src/main.rs:6:20
  |
6 |         Binder(_a, _x @ ..) => {}
  |                    ^^^^^^^ is only allowed in a slice
  |
help: replace with `..` or use a different valid pattern
  |
6 |         Binder(_a, ..) => {}
  |                    ^^

error[E0023]: this pattern has 1 field, but the corresponding tuple struct has 3 fields
 --> src/main.rs:6:9
  |
1 | struct Binder(i32, i32, i32);
  | ----------------------------- tuple struct defined here
...
6 |         Binder(_a, _x @ ..) => {}
  |         ^^^^^^^^^^^^^^^^^^^ expected 3 fields, found 1

error: aborting due to 2 previous errors

r? @estebank

@chrissimpkins
Copy link
Member Author

chrissimpkins commented May 28, 2020

Modified the suggestion slightly to better address attempts to define a BindingMode in the binding.

e.g.,

error: `_x @` is not allowed in a tuple
 --> src/main.rs:4:14
  |
4 |         (_a, ref mut _x @ ..) => {}
  |              ^^^^^^^^^^^^^^^ is only allowed in a slice
  |
help: replace with `..` or use a different valid pattern
  |
4 |         (_a, ..) => {}
  |              ^^

error[E0308]: mismatched types
 --> src/main.rs:4:9
  |
3 |     match x {
  |           - this expression has type `({integer}, {integer}, {integer})`
4 |         (_a, ref mut _x @ ..) => {}
  |         ^^^^^^^^^^^^^^^^^^^^^ expected a tuple with 3 elements, found one with 1 element
  |
  = note: expected tuple `({integer}, {integer}, {integer})`
             found tuple `(_,)`

error: aborting due to 2 previous errors

Copy link
Contributor

@estebank estebank left a comment

Choose a reason for hiding this comment

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

A few nitpicks, let me know what you think.

src/librustc_ast_lowering/pat.rs Outdated Show resolved Hide resolved
src/test/ui/issues/issue-72574-1.stderr Outdated Show resolved Hide resolved
src/librustc_ast_lowering/pat.rs Outdated Show resolved Hide resolved
src/librustc_ast_lowering/pat.rs Show resolved Hide resolved
src/test/ui/issues/issue-72574-1.rs Outdated Show resolved Hide resolved
src/test/ui/issues/issue-72574-2.rs Outdated Show resolved Hide resolved
@chrissimpkins
Copy link
Member Author

chrissimpkins commented May 29, 2020

Thanks Esteban! I committed the two new suggestions and will push updated test files.

@chrissimpkins
Copy link
Member Author

Tests updated with the latest round of changes in dbb2d6b

@chrissimpkins
Copy link
Member Author

@estebank I'll wait for this to pass the CI tests and then will squash. Let me know if you feel that any other changes are needed. This looks good 👍

error: `_x @` is not allowed in a tuple
 --> src/main.rs:4:14
  |
4 |         (_a, ref mut _x @ ..) => {}
  |              ^^^^^^^^^^^^^^^ this is only allowed in slice patterns
  |
  = help: remove this and bind each tuple field independently
help: if you don't need to use the contents of _x, discard the tuple's remaining fields
  |
4 |         (_a, ..) => {}
  |              ^^

error: aborting due to previous error

fix comment


add newline for tidy fmt error...


edit suggestion message


change the suggestion message to better handle cases with binding modes


Apply suggestions from estebank code review

Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
edits to address source review


Apply suggestions from estebank code review rust-lang#2

Co-authored-by: Esteban Kuber <estebank@users.noreply.github.com>
update test files
@chrissimpkins
Copy link
Member Author

Squashed to 27ed143

All set on my end. Please let me know if there is anything else that I need to address. Thanks again Esteban.

@estebank
Copy link
Contributor

@bors r+ rollup

@bors
Copy link
Contributor

bors commented May 29, 2020

📌 Commit 27ed143 has been approved by estebank

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels May 29, 2020
bors added a commit to rust-lang-ci/rust that referenced this pull request May 30, 2020
Rollup of 10 pull requests

Successful merges:

 - rust-lang#72033 (Update RELEASES.md for 1.44.0)
 - rust-lang#72162 (Add Extend::{extend_one,extend_reserve})
 - rust-lang#72419 (Miri read_discriminant: return a scalar instead of raw underlying bytes)
 - rust-lang#72621 (Don't bail out of trait selection when predicate references an error)
 - rust-lang#72677 (Fix diagnostics for `@ ..` binding pattern in tuples and tuple structs)
 - rust-lang#72710 (Add test to make sure -Wunused-crate-dependencies works with tests)
 - rust-lang#72724 (Revert recursive `TokenKind::Interpolated` expansion for now)
 - rust-lang#72741 (Remove unused mut from long-linker-command-lines test)
 - rust-lang#72750 (Remove remaining calls to `as_local_node_id`)
 - rust-lang#72752 (remove mk_bool)

Failed merges:

r? @ghost
@bors bors merged commit ca8640e into rust-lang:master May 30, 2020
@chrissimpkins chrissimpkins deleted the fix-72574 branch May 30, 2020 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Diagnostics for @ .. idiom use in tuple and tuple struct patterns is incorrect
4 participants