Rust: 改进 for 循环中元组解构中缺少括号的错误消息

创建于 2018-02-24  ·  3评论  ·  资料来源: rust-lang/rust

鉴于:

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

rustc 产生:

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)] {}
   |            ^

…但如果它发现用户可能想要for (foo, bar) in ...并给出更具体的错误消息,那就更好了。

这可能是来自 Python 的一个比较常见的错误,它对for循环具有类似的语法,但允许省略括号。

A-diagnostics

最有用的评论

如果这是受 Python 启发的东西,那么理想情况下这些诊断/恢复也应该适用于let

let a, b = (10, 11);

=>

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

我怀疑我们甚至可以在所有表​​达式和模式上下文中将这种恢复实现为“自动元组”。

illegal a, b, c

=>

(a, b, c)

所有3条评论

如果这是受 Python 启发的东西,那么理想情况下这些诊断/恢复也应该适用于let

let a, b = (10, 11);

=>

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

我怀疑我们甚至可以在所有表​​达式和模式上下文中将这种恢复实现为“自动元组”。

illegal a, b, c

=>

(a, b, c)

:+1: 不仅仅是 Python,这对 Go 用户也有帮助。

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

(我在这方面取得了一些进展;公关即将推出)

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

drewcrawford picture drewcrawford  ·  3评论

defuz picture defuz  ·  3评论

SharplEr picture SharplEr  ·  3评论

mcarton picture mcarton  ·  3评论

modsec picture modsec  ·  3评论