From 2d381d69ea92500fdbf8583c4eb8662110e13c2f Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 25 Apr 2022 19:42:43 +0800 Subject: [PATCH] checker: fix error for match aliases (#14157) --- vlib/v/checker/match.v | 5 +++-- vlib/v/tests/match_aliases_test.v | 9 +++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/match_aliases_test.v diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index da305a00fc..916b903079 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -162,9 +162,10 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym c.expected_type = node.expected_type low_expr := expr.low high_expr := expr.high + final_cond_sym := c.table.final_sym(node.cond_type) if low_expr is ast.IntegerLiteral { if high_expr is ast.IntegerLiteral - && (cond_type_sym.is_int() || cond_type_sym.info is ast.Enum) { + && (final_cond_sym.is_int() || final_cond_sym.info is ast.Enum) { low = low_expr.val.i64() high = high_expr.val.i64() if low > high { @@ -174,7 +175,7 @@ fn (mut c Checker) match_exprs(mut node ast.MatchExpr, cond_type_sym ast.TypeSym c.error('mismatched range types', low_expr.pos) } } else if low_expr is ast.CharLiteral { - if high_expr is ast.CharLiteral && cond_type_sym.kind in [.u8, .char, .rune] { + if high_expr is ast.CharLiteral && final_cond_sym.kind in [.u8, .char, .rune] { low = low_expr.val[0] high = high_expr.val[0] if low > high { diff --git a/vlib/v/tests/match_aliases_test.v b/vlib/v/tests/match_aliases_test.v new file mode 100644 index 0000000000..88b4a39590 --- /dev/null +++ b/vlib/v/tests/match_aliases_test.v @@ -0,0 +1,9 @@ +fn test_match_aliases() { + a := byte(97) + ret := match a { + `0`...`9`, `a`...`f` { 'OK' } + else { 'NOT OK' } + } + println(ret) + assert ret == 'OK' +}