1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: improve mismatched range types error messages

This commit is contained in:
Delyan Angelov 2022-09-17 09:14:49 +03:00
parent fda39bfb82
commit a3b60e6b55
No known key found for this signature in database
GPG Key ID: 66886C0F12D595ED
3 changed files with 21 additions and 3 deletions

View File

@ -162,7 +162,8 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
c.error('start value is higher than end value', branch.pos) c.error('start value is higher than end value', branch.pos)
} }
} else { } else {
c.error('mismatched range types', low_expr.pos) c.error('mismatched range types - $expr.low is an integer, but $expr.high is not',
low_expr.pos)
} }
} else if low_expr is ast.CharLiteral { } else if low_expr is ast.CharLiteral {
if high_expr is ast.CharLiteral && final_cond_sym.kind in [.u8, .char, .rune] { if high_expr is ast.CharLiteral && final_cond_sym.kind in [.u8, .char, .rune] {
@ -172,7 +173,9 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
c.error('start value is higher than end value', branch.pos) c.error('start value is higher than end value', branch.pos)
} }
} else { } else {
c.error('mismatched range types', low_expr.pos) typ := c.table.type_to_str(c.expr(node.cond))
c.error('mismatched range types - trying to match `$node.cond`, which has type `$typ`, to a range of `rune`',
low_expr.pos)
} }
} else { } else {
typ := c.table.type_to_str(c.expr(expr.low)) typ := c.table.type_to_str(c.expr(expr.low))

View File

@ -1,7 +1,14 @@
vlib/v/checker/tests/match_range_mismatch_type_err.vv:4:3: error: mismatched range types vlib/v/checker/tests/match_range_mismatch_type_err.vv:4:3: error: mismatched range types - trying to match `x`, which has type `string`, to a range of `rune`
2 | x := '1' 2 | x := '1'
3 | match x { 3 | match x {
4 | `0`...`9` { 4 | `0`...`9` {
| ~~~ | ~~~
5 | println('0-9') 5 | println('0-9')
6 | } 6 | }
vlib/v/checker/tests/match_range_mismatch_type_err.vv:16:3: error: mismatched range types - 0 is an integer, but `9` is not
14 | x := 1
15 | match x {
16 | 0...`9` {}
| ^
17 | else {}
18 | }

View File

@ -9,3 +9,11 @@ fn main() {
} }
} }
} }
fn abc() {
x := 1
match x {
0...`9` {}
else {}
}
}