Skip to content

Commit 48e65a7

Browse files
authored
checker: check type in match range (fix #11337) (#11389)
1 parent 724942c commit 48e65a7

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

vlib/v/checker/checker.v

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6232,14 +6232,15 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
62326232
low_expr := expr.low
62336233
high_expr := expr.high
62346234
if low_expr is ast.IntegerLiteral {
6235-
if high_expr is ast.IntegerLiteral {
6235+
if high_expr is ast.IntegerLiteral
6236+
&& (cond_type_sym.is_int() || cond_type_sym.info is ast.Enum) {
62366237
low = low_expr.val.i64()
62376238
high = high_expr.val.i64()
62386239
} else {
62396240
c.error('mismatched range types', low_expr.pos)
62406241
}
62416242
} else if low_expr is ast.CharLiteral {
6242-
if high_expr is ast.CharLiteral {
6243+
if high_expr is ast.CharLiteral && cond_type_sym.kind in [.byte, .char, .rune] {
62436244
low = low_expr.val[0]
62446245
high = high_expr.val[0]
62456246
} else {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
vlib/v/checker/tests/match_range_mismatch_type_err.vv:4:3: error: mismatched range types
2+
2 | x := '1'
3+
3 | match x {
4+
4 | `0`...`9` {
5+
| ~~~
6+
5 | println('0-9')
7+
6 | }
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
fn main() {
2+
x := '1'
3+
match x {
4+
`0`...`9` {
5+
println('0-9')
6+
}
7+
else {
8+
println('!0-9')
9+
}
10+
}
11+
}

0 commit comments

Comments
 (0)