diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c6ec90b7e1..95615c2e0d 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -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 { diff --git a/vlib/v/checker/tests/match_range_mismatch_type_err.out b/vlib/v/checker/tests/match_range_mismatch_type_err.out new file mode 100644 index 0000000000..1c880101f7 --- /dev/null +++ b/vlib/v/checker/tests/match_range_mismatch_type_err.out @@ -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 | } diff --git a/vlib/v/checker/tests/match_range_mismatch_type_err.vv b/vlib/v/checker/tests/match_range_mismatch_type_err.vv new file mode 100644 index 0000000000..d6961715b2 --- /dev/null +++ b/vlib/v/checker/tests/match_range_mismatch_type_err.vv @@ -0,0 +1,11 @@ +fn main() { + x := '1' + match x { + `0`...`9` { + println('0-9') + } + else { + println('!0-9') + } + } +}