mirror of
https://github.com/vlang/v.git
synced 2025-09-19 04:17:46 -04:00
checker: allow casted integeral types in match ranges (#19572)
This commit is contained in:
parent
eb82a72012
commit
44045c650b
@ -166,6 +166,11 @@ fn (mut c Checker) get_comptime_number_value(mut expr ast.Expr) ?i64 {
|
|||||||
if mut expr is ast.IntegerLiteral {
|
if mut expr is ast.IntegerLiteral {
|
||||||
return expr.val.i64()
|
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 {
|
if mut expr is ast.Ident {
|
||||||
has_expr_mod_in_name := expr.name.contains('.')
|
has_expr_mod_in_name := expr.name.contains('.')
|
||||||
expr_name := if has_expr_mod_in_name { expr.name } else { '${expr.mod}.${expr.name}' }
|
expr_name := if has_expr_mod_in_name { expr.name } else { '${expr.mod}.${expr.name}' }
|
||||||
|
@ -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
|
@ -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') }
|
||||||
|
}
|
10
vlib/v/checker/tests/match_ranges_not_same_cast_err.out
Normal file
10
vlib/v/checker/tests/match_ranges_not_same_cast_err.out
Normal 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
|
9
vlib/v/checker/tests/match_ranges_not_same_cast_err.vv
Normal file
9
vlib/v/checker/tests/match_ranges_not_same_cast_err.vv
Normal 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') }
|
||||||
|
}
|
@ -1,13 +1,16 @@
|
|||||||
const (
|
const (
|
||||||
start = 1
|
start = 1
|
||||||
start_2 = 4
|
start_2 = 4
|
||||||
end = 3
|
end = 3
|
||||||
end_2 = 8
|
end_2 = 8
|
||||||
//
|
//
|
||||||
start_rune = `a`
|
start_rune = `a`
|
||||||
start_2_rune = `d`
|
start_2_rune = `d`
|
||||||
end_rune = `c`
|
end_rune = `c`
|
||||||
end_2_rune = `i`
|
end_2_rune = `i`
|
||||||
|
//
|
||||||
|
start_cast_expr = u16(1)
|
||||||
|
end_cast_expr = u16(5)
|
||||||
)
|
)
|
||||||
|
|
||||||
fn test_match_int_const_ranges() {
|
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]
|
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 {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user