checker: allow casted integeral types in match ranges (#19572)

This commit is contained in:
Swastik Baranwal 2023-10-16 05:05:30 +05:30 committed by GitHub
parent eb82a72012
commit 44045c650b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 64 additions and 8 deletions

View File

@ -166,6 +166,11 @@ fn (mut c Checker) get_comptime_number_value(mut expr ast.Expr) ?i64 {
if mut expr is ast.IntegerLiteral {
return expr.val.i64()
}
if mut expr is ast.CastExpr {
if mut expr.expr is ast.IntegerLiteral {
return expr.expr.val.i64()
}
}
if mut expr is ast.Ident {
has_expr_mod_in_name := expr.name.contains('.')
expr_name := if has_expr_mod_in_name { expr.name } else { '${expr.mod}.${expr.name}' }

View File

@ -0,0 +1,10 @@
vlib/v/checker/tests/match_cast_cond_not_same_range_cast_type_err.vv:7:2: error: the range type and the match condition type should match
5 | c := u8(3)
6 | match c {
7 | a...b { println('1...5') }
| ~~~~~
8 | else { println('not 1...5') }
9 | }
Details:
match condition type: u8
range type: u16

View File

@ -0,0 +1,9 @@
const (
a = u16(1)
b = u16(5)
)
c := u8(3)
match c {
a...b { println('1...5') }
else { println('not 1...5') }
}

View File

@ -0,0 +1,10 @@
vlib/v/checker/tests/match_ranges_not_same_cast_err.vv:7:2: error: the low and high parts of a range expression, should have matching types
5 | c := u16(3)
6 | match c {
7 | a...b { println('1...5') }
| ~~~~~
8 | else { println('not 1...5') }
9 | }
Details:
low part type: u16
high part type: u8

View File

@ -0,0 +1,9 @@
const (
a = u16(1)
b = u8(5)
)
c := u16(3)
match c {
a...b { println('1...5') }
else { println('not 1...5') }
}

View File

@ -1,13 +1,16 @@
const (
start = 1
start_2 = 4
end = 3
end_2 = 8
start = 1
start_2 = 4
end = 3
end_2 = 8
//
start_rune = `a`
start_2_rune = `d`
end_rune = `c`
end_2_rune = `i`
start_rune = `a`
start_2_rune = `d`
end_rune = `c`
end_2_rune = `i`
//
start_cast_expr = u16(1)
end_cast_expr = u16(5)
)
fn test_match_int_const_ranges() {
@ -63,3 +66,13 @@ fn test_match_expr_rune_const_ranges() {
}
assert results == [1, 1, 1, 2, 2, 3, 3, 3, 3, 4, 4]
}
fn test_match_expr_integer_cast_const_ranges() {
c := u16(3)
match c {
start_cast_expr...end_cast_expr {
assert c == u16(3)
}
else {}
}
}