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

checker: check type in match range (fix #11337) (#11389)

This commit is contained in:
yuyi 2021-09-05 09:50:43 +08:00 committed by GitHub
parent 724942c4e6
commit 48e65a7bb2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 21 additions and 2 deletions

View File

@ -6232,14 +6232,15 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym
low_expr := expr.low
high_expr := expr.high
if low_expr is ast.IntegerLiteral {
if high_expr is ast.IntegerLiteral {
if high_expr is ast.IntegerLiteral
&& (cond_type_sym.is_int() || cond_type_sym.info is ast.Enum) {
low = low_expr.val.i64()
high = high_expr.val.i64()
} else {
c.error('mismatched range types', low_expr.pos)
}
} else if low_expr is ast.CharLiteral {
if high_expr is ast.CharLiteral {
if high_expr is ast.CharLiteral && cond_type_sym.kind in [.byte, .char, .rune] {
low = low_expr.val[0]
high = high_expr.val[0]
} else {

View File

@ -0,0 +1,7 @@
vlib/v/checker/tests/match_range_mismatch_type_err.vv:4:3: error: mismatched range types
2 | x := '1'
3 | match x {
4 | `0`...`9` {
| ~~~
5 | println('0-9')
6 | }

View File

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