parser: improve error messages of 'for val in array' (#14459)
parent
c5933aa3c5
commit
7c7d59acee
|
@ -125,7 +125,8 @@ fn (mut p Parser) for_stmt() ast.Stmt {
|
|||
is_stack_obj: true
|
||||
})
|
||||
} else if p.scope.known_var(val_var_name) {
|
||||
return p.error('redefinition of value iteration variable `$val_var_name`')
|
||||
return p.error_with_pos('redefinition of value iteration variable `$val_var_name`, use `for ($val_var_name in array) {` if you want to check for a condition instead',
|
||||
val_var_pos)
|
||||
}
|
||||
p.check(.key_in)
|
||||
if p.tok.kind == .name && p.tok.lit in [key_var_name, val_var_name] {
|
||||
|
|
|
@ -0,0 +1,7 @@
|
|||
vlib/v/parser/tests/for_val_in_array_err.vv:5:6: error: redefinition of value iteration variable `val`, use `for (val in array) {` if you want to check for a condition instead
|
||||
3 | fn main() {
|
||||
4 | val := `+`
|
||||
5 | for val in [TokenValue(`+`), TokenValue(`-`)] {
|
||||
| ~~~
|
||||
6 | println("ok")
|
||||
7 | break
|
|
@ -0,0 +1,9 @@
|
|||
type TokenValue = rune | u64
|
||||
|
||||
fn main() {
|
||||
val := `+`
|
||||
for val in [TokenValue(`+`), TokenValue(`-`)] {
|
||||
println("ok")
|
||||
break
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue