Rust: Improve error message for missing parens in tuple destructure in for loop

Created on 24 Feb 2018  ·  3Comments  ·  Source: rust-lang/rust

Given:

    for foo, bar in [(1, 2), (3, 4)] {}

rustc produces:

error: missing `in` in `for` loop
  --> src/main.rs:44:12
   |
44 |     for foo, bar in [(1, 2)] {}
   |            ^ help: try adding `in` here

error: expected expression, found `,`
  --> src/main.rs:44:12
   |
44 |     for foo, bar in [(1, 2)] {}
   |            ^

…but it would be better if it figured out that the user presumably intended for (foo, bar) in ..., and gave a more specific error message.

This is probably a somewhat common error coming from Python, which has similar syntax for for loops but allows omitting the parentheses.

A-diagnostics

Most helpful comment

If this is something Python-inspired, then these diagnostics/recovery should ideally apply to let as well.

let a, b = (10, 11);

=>

let (a, b) = (10, 11);

I suspect we can even implement this recovery as "auto-tupling" in all expression and pattern contexts.

illegal a, b, c

=>

(a, b, c)

All 3 comments

If this is something Python-inspired, then these diagnostics/recovery should ideally apply to let as well.

let a, b = (10, 11);

=>

let (a, b) = (10, 11);

I suspect we can even implement this recovery as "auto-tupling" in all expression and pattern contexts.

illegal a, b, c

=>

(a, b, c)

:+1: Not just Python, this will be helpful for Go users as well.

for k, v := range myMap {
    x, err = /* ... */
}

(I've made some progress on this; PR forthcoming)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

GuillaumeGomez picture GuillaumeGomez  ·  300Comments

withoutboats picture withoutboats  ·  213Comments

thestinger picture thestinger  ·  234Comments

nikomatsakis picture nikomatsakis  ·  259Comments

aturon picture aturon  ·  417Comments