parser: disallow match expr at certain places (#6490)
parent
a9da4dd437
commit
7e13518cc2
|
@ -933,6 +933,8 @@ Note that the ranges use `...` (three dots) rather than `..` (two dots). This is
|
||||||
because the range is *inclusive* of the last element, rather than exclusive
|
because the range is *inclusive* of the last element, rather than exclusive
|
||||||
(as `..` ranges are). Using `..` in a match branch will throw an error.
|
(as `..` ranges are). Using `..` in a match branch will throw an error.
|
||||||
|
|
||||||
|
Note: `match` as an expression is not usable in `for` loop and `if` statements.
|
||||||
|
|
||||||
### Defer
|
### Defer
|
||||||
|
|
||||||
A defer statement defers the execution of a block of statements until the surrounding function returns.
|
A defer statement defers the execution of a block of statements until the surrounding function returns.
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
vlib/v/checker/tests/for_match_err.vv:3:7: error: cannot use `match` in `for` loop
|
||||||
|
1 | fn main() {
|
||||||
|
2 | mut a := 2
|
||||||
|
3 | for match a {
|
||||||
|
| ~~~~~
|
||||||
|
4 | 2 {
|
||||||
|
5 | println('a == 2')
|
|
@ -0,0 +1,15 @@
|
||||||
|
fn main() {
|
||||||
|
mut a := 2
|
||||||
|
for match a {
|
||||||
|
2 {
|
||||||
|
println('a == 2')
|
||||||
|
a = 0
|
||||||
|
}
|
||||||
|
0 {
|
||||||
|
println('a == 0')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
println('unexpected branch')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,7 @@
|
||||||
|
vlib/v/checker/tests/if_match_expr_err.vv:3:8: error: cannot use `match` with `if` statements
|
||||||
|
1 | fn main() {
|
||||||
|
2 | a := 0
|
||||||
|
3 | if match a {
|
||||||
|
| ~~~~~
|
||||||
|
4 | 0 {
|
||||||
|
5 | println('a is zero')
|
|
@ -0,0 +1,13 @@
|
||||||
|
fn main() {
|
||||||
|
a := 0
|
||||||
|
if match a {
|
||||||
|
0 {
|
||||||
|
println('a is zero')
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
println('unreachable branch')
|
||||||
|
}
|
||||||
|
} 5 < 7 {
|
||||||
|
println('5 is less than 7')
|
||||||
|
}
|
||||||
|
}
|
|
@ -11,6 +11,9 @@ fn (mut p Parser) for_stmt() ast.Stmt {
|
||||||
pos := p.tok.position()
|
pos := p.tok.position()
|
||||||
p.open_scope()
|
p.open_scope()
|
||||||
p.inside_for = true
|
p.inside_for = true
|
||||||
|
if p.tok.kind == .key_match {
|
||||||
|
p.error('cannot use `match` in `for` loop')
|
||||||
|
}
|
||||||
// defer { p.close_scope() }
|
// defer { p.close_scope() }
|
||||||
// Infinite loop
|
// Infinite loop
|
||||||
if p.tok.kind == .lcbr {
|
if p.tok.kind == .lcbr {
|
||||||
|
|
|
@ -34,6 +34,9 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
|
||||||
comments << p.eat_comments()
|
comments << p.eat_comments()
|
||||||
p.check(.key_else)
|
p.check(.key_else)
|
||||||
comments << p.eat_comments()
|
comments << p.eat_comments()
|
||||||
|
if p.tok.kind == .key_match {
|
||||||
|
p.error('cannot use `match` with `if` statements')
|
||||||
|
}
|
||||||
if p.tok.kind == .lcbr {
|
if p.tok.kind == .lcbr {
|
||||||
// else {
|
// else {
|
||||||
has_else = true
|
has_else = true
|
||||||
|
@ -72,6 +75,9 @@ fn (mut p Parser) if_expr(is_comptime bool) ast.IfExpr {
|
||||||
}
|
}
|
||||||
// `if` or `else if`
|
// `if` or `else if`
|
||||||
p.check(.key_if)
|
p.check(.key_if)
|
||||||
|
if p.tok.kind == .key_match {
|
||||||
|
p.error('cannot use `match` with `if` statements')
|
||||||
|
}
|
||||||
comments << p.eat_comments()
|
comments << p.eat_comments()
|
||||||
// `if mut name is T`
|
// `if mut name is T`
|
||||||
mut mut_name := false
|
mut mut_name := false
|
||||||
|
|
|
@ -1,24 +1,24 @@
|
||||||
fn test_for_match() {
|
fn test_for_match() {
|
||||||
mut a := 2
|
mut a := 2
|
||||||
mut b := 0
|
mut b := 0
|
||||||
for match a {
|
for {
|
||||||
2 {
|
match a {
|
||||||
println('a == 2')
|
2 {
|
||||||
a = 0
|
println('a == 2')
|
||||||
true
|
a = 0
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
0 {
|
||||||
|
println('a == 0')
|
||||||
|
a = 5
|
||||||
|
b++
|
||||||
|
break
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
println('unexpected branch')
|
||||||
|
break
|
||||||
|
}
|
||||||
}
|
}
|
||||||
0 {
|
|
||||||
println('a == 0')
|
|
||||||
a = 5
|
|
||||||
false
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
println('unexpected branch')
|
|
||||||
false
|
|
||||||
}
|
|
||||||
} {
|
|
||||||
b++
|
|
||||||
println('${b}. run')
|
|
||||||
}
|
}
|
||||||
assert a == 5
|
assert a == 5
|
||||||
assert b == 1
|
assert b == 1
|
||||||
|
@ -34,7 +34,7 @@ fn test_for_select() {
|
||||||
x := <-ch1 {
|
x := <-ch1 {
|
||||||
a += x
|
a += x
|
||||||
}
|
}
|
||||||
y := <- ch2 {
|
y := <-ch2 {
|
||||||
a += int(y)
|
a += int(y)
|
||||||
}
|
}
|
||||||
} {
|
} {
|
||||||
|
|
Loading…
Reference in New Issue