checker: improve error message for match branch type mismatch (#6588)
parent
a6f7f0ac8a
commit
f734f8167b
|
@ -3173,7 +3173,7 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, type_sym table.TypeSymbol
|
||||||
} else if !c.check_types(expr_type, c.expected_type) {
|
} else if !c.check_types(expr_type, c.expected_type) {
|
||||||
expr_str := c.table.type_to_str(expr_type)
|
expr_str := c.table.type_to_str(expr_type)
|
||||||
expect_str := c.table.type_to_str(c.expected_type)
|
expect_str := c.table.type_to_str(c.expected_type)
|
||||||
c.error('cannot use type `$expect_str` as type `$expr_str`', node.pos)
|
c.error('cannot match `$expr_str` with `$expect_str` condition', branch.pos)
|
||||||
}
|
}
|
||||||
branch_exprs[key] = val + 1
|
branch_exprs[key] = val + 1
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,13 +1,14 @@
|
||||||
vlib/v/checker/tests/match_expr_and_expected_type_error.vv:2:1: error: cannot use type `rune` as type `string`
|
vlib/v/checker/tests/match_expr_and_expected_type_error.vv:3:3: error: cannot match `string` with `rune` condition
|
||||||
1 | ch := `a`
|
1 | ch := `a`
|
||||||
2 | match ch {
|
2 | match ch {
|
||||||
| ~~~~~~~~~~
|
|
||||||
3 | 'a' {}
|
3 | 'a' {}
|
||||||
|
| ~~~~~
|
||||||
4 | else {}
|
4 | else {}
|
||||||
vlib/v/checker/tests/match_expr_and_expected_type_error.vv:8:1: error: cannot use type `int` as type `string`
|
5 | }
|
||||||
6 |
|
vlib/v/checker/tests/match_expr_and_expected_type_error.vv:9:3: error: cannot match `string` with `int` condition
|
||||||
7 | char := 123
|
7 | i := 123
|
||||||
8 | match char {
|
8 | match i {
|
||||||
| ~~~~~~~~~~~~
|
|
||||||
9 | 'a' {}
|
9 | 'a' {}
|
||||||
|
| ~~~~~
|
||||||
10 | else {}
|
10 | else {}
|
||||||
|
11 | }
|
||||||
|
|
|
@ -4,8 +4,8 @@ match ch {
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
|
|
||||||
char := 123
|
i := 123
|
||||||
match char {
|
match i {
|
||||||
'a' {}
|
'a' {}
|
||||||
else {}
|
else {}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,21 @@
|
||||||
|
vlib/v/checker/tests/match_invalid_type.vv:5:3: error: cannot match `byte` with `IoS` condition
|
||||||
|
3 | fn sum() {
|
||||||
|
4 | match IoS(1) {
|
||||||
|
5 | byte {
|
||||||
|
| ~~~~~~
|
||||||
|
6 | println('not cool')
|
||||||
|
7 | }
|
||||||
|
vlib/v/checker/tests/match_invalid_type.vv:4:2: error: match must be exhaustive (add match branches for: `int`, `string` or `else {}` at the end)
|
||||||
|
2 |
|
||||||
|
3 | fn sum() {
|
||||||
|
4 | match IoS(1) {
|
||||||
|
| ~~~~~~~~~~~~~~
|
||||||
|
5 | byte {
|
||||||
|
6 | println('not cool')
|
||||||
|
vlib/v/checker/tests/match_invalid_type.vv:24:3: error: `Cat` doesn't implement method `speak`
|
||||||
|
22 | a := Animal(Dog{})
|
||||||
|
23 | match a {
|
||||||
|
24 | Cat {
|
||||||
|
| ~~~~~
|
||||||
|
25 | println('not cool either')
|
||||||
|
26 | }
|
|
@ -1,5 +1,13 @@
|
||||||
type IoS = int | string
|
type IoS = int | string
|
||||||
|
|
||||||
|
fn sum() {
|
||||||
|
match IoS(1) {
|
||||||
|
byte {
|
||||||
|
println('not cool')
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
interface Animal {
|
interface Animal {
|
||||||
speak()
|
speak()
|
||||||
}
|
}
|
||||||
|
@ -10,12 +18,7 @@ fn (d Dog) speak() {}
|
||||||
|
|
||||||
struct Cat {}
|
struct Cat {}
|
||||||
|
|
||||||
fn main() {
|
fn iface() {
|
||||||
match IoS(1) {
|
|
||||||
byte {
|
|
||||||
println('not cool')
|
|
||||||
}
|
|
||||||
}
|
|
||||||
a := Animal(Dog{})
|
a := Animal(Dog{})
|
||||||
match a {
|
match a {
|
||||||
Cat {
|
Cat {
|
|
@ -1,14 +0,0 @@
|
||||||
vlib/v/checker/tests/match_sumtype_type_invalid.vv:14:2: error: cannot use type `IoS` as type `byte`
|
|
||||||
12 |
|
|
||||||
13 | fn main() {
|
|
||||||
14 | match IoS(1) {
|
|
||||||
| ~~~~~~~~~~~~~~
|
|
||||||
15 | byte {
|
|
||||||
16 | println('not cool')
|
|
||||||
vlib/v/checker/tests/match_sumtype_type_invalid.vv:21:3: error: `Cat` doesn't implement method `speak`
|
|
||||||
19 | a := Animal(Dog{})
|
|
||||||
20 | match a {
|
|
||||||
21 | Cat {
|
|
||||||
| ~~~~~
|
|
||||||
22 | println('not cool either')
|
|
||||||
23 | }
|
|
|
@ -5,3 +5,17 @@ vlib/v/checker/tests/match_undefined_cond.vv:4:15: error: undefined ident: `Asd`
|
||||||
| ~~~
|
| ~~~
|
||||||
5 | 1 { 'foo' }
|
5 | 1 { 'foo' }
|
||||||
6 | 2 { 'test' }
|
6 | 2 { 'test' }
|
||||||
|
vlib/v/checker/tests/match_undefined_cond.vv:5:3: error: cannot match `any_int` with `void` condition
|
||||||
|
3 | fn main() {
|
||||||
|
4 | res := match Asd {
|
||||||
|
5 | 1 { 'foo' }
|
||||||
|
| ~~~
|
||||||
|
6 | 2 { 'test' }
|
||||||
|
7 | else { '' }
|
||||||
|
vlib/v/checker/tests/match_undefined_cond.vv:6:3: error: cannot match `any_int` with `void` condition
|
||||||
|
4 | res := match Asd {
|
||||||
|
5 | 1 { 'foo' }
|
||||||
|
6 | 2 { 'test' }
|
||||||
|
| ~~~
|
||||||
|
7 | else { '' }
|
||||||
|
8 | }
|
||||||
|
|
Loading…
Reference in New Issue