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

Compiler suggesting unstable or-pattern in for loop #79357

Closed
ritobanrc opened this issue Nov 23, 2020 · 5 comments · Fixed by #79364
Closed

Compiler suggesting unstable or-pattern in for loop #79357

ritobanrc opened this issue Nov 23, 2020 · 5 comments · Fixed by #79364
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug.

Comments

@ritobanrc
Copy link

ritobanrc commented Nov 23, 2020

When forgetting to include parenthesis around a tuple pattern in a for loop, the compiler gives a suggestion to use an or pattern. I would expect this, a. because it's not what most would expect if you use a comma to separate two values in a pattern, and b. because or patterns in for loops are still an unstable feature.

I tried this code:

    let a = vec![1, 2, 3];
    let b = vec![3, 4, 5];
    
    for foo, bar in a.into_iter().zip(b) {    }

As expected, the compiler gives an unexpected , in pattern error, because I forgot the parenthesis around (foo, bar). However, the compiler also suggests that I add a "vertical bar to match on multiple alternatives".

error: unexpected `,` in pattern
 --> src/main.rs:5:13
  |
5 |     for foo, bar in a.into_iter().zip(b) {
  |             ^
  |
help: try adding parentheses to match on a tuple...
  |
5 |     for (foo, bar) in a.into_iter().zip(b) {
  |         ^^^^^^^^^^^
help: ...or a vertical bar to match on multiple alternatives
  |
5 |     for foo | bar in a.into_iter().zip(b) {
  |         ^^^^^^^^^^

Playground Link

While it does provide the correct answer (adding parenthesis), it also provides the vertical bar option, which is an unstable feature in this RFC.

While the RFC does not explicitly single out for loops as a situation where this is necessary, I believe it does apply here, because in this situation (credit /u/ehuss for the example, which is a situation where the or pattern syntax is being used correctly, I get a or-patterns syntax is experimental error, as expected, and including #![feature(or_patterns)] resolves the error.

Meta

I tested this on stable, beta, and nightly in the rust playground. The output is identical for all 3 of them.

@ritobanrc ritobanrc added the C-bug Category: This is a bug. label Nov 23, 2020
@nico-abram
Copy link
Contributor

nico-abram commented Nov 23, 2020

I would like to work on this, if possible

Would it just be only giving this suggestion if self.sess.gated_spans.is_ungated(sym::or_patterns) like here?

@jyn514 jyn514 added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` labels Nov 23, 2020
@jyn514
Copy link
Member

jyn514 commented Nov 23, 2020

@nico-abram that seems like the right approach :)

@nico-abram
Copy link
Contributor

@rustbot claim

@nico-abram
Copy link
Contributor

Looking at this more closely I'm not sure the fix is that simple, since some top level or patterns are stable and the suggestion is correct (And given) for those

The nested case seems to not get the suggestion (Which matches the comment for maybe_recover_unexpected_comma)

@nico-abram
Copy link
Contributor

nico-abram commented Nov 23, 2020

Looking at usages of GateOr::{Yes, No}, it looks like the only top level things (Which are the only ones that give the suggestion AIUI) that are GateOr::Yes are local variable declaration patterns and for patterns

EDIT: I'm gonna try just propagating the GateOr into maybe_recover_unexpected_comma

bors added a commit to rust-lang-ci/rust that referenced this issue Dec 2, 2020
@bors bors closed this as completed in de3a0bd Dec 2, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants