checker: fix error for match aliases (#14157)

yuyi 2022-04-25 19:42:43 +08:00 committed by Jef Roosens
parent 549735fbff
commit 2d381d69ea
Signed by: Jef Roosens
GPG Key ID: B75D4F293C7052DB
2 changed files with 12 additions and 2 deletions

View File

@ -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 {

View File

@ -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'
}