Skip to content

Commit 0dc4e9b

Browse files
authored
checker: add T.typ and T.unaliased_typ checking to $match (fix #25200) (#25202)
1 parent 7c780ed commit 0dc4e9b

2 files changed

Lines changed: 39 additions & 0 deletions

File tree

‎vlib/v/checker/match.v‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
104104
if c.comptime.inside_comptime_for && node.cond.field_name in ['name', 'typ'] {
105105
// hack: `typ` is just for bypass the error test, because we don't know it is a type match or a value match righ now
106106
comptime_match_cond_value = c.comptime.comptime_for_field_value.name
107+
} else if mut node.cond.expr is ast.Ident
108+
&& node.cond.gkind_field in [.typ, .unaliased_typ] {
109+
left_type := c.get_expr_type(node.cond.expr)
110+
comptime_match_cond_value = c.table.type_to_str(left_type)
107111
} else {
108112
c.error('`${node.cond}` is not `\$for` field.name.', node.cond.pos)
109113
return ast.void_type
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
fn check_unaliased[T](t T) bool {
2+
$match T.unaliased_typ {
3+
int {
4+
return true
5+
}
6+
$else {
7+
return false
8+
}
9+
}
10+
}
11+
12+
fn check_typ[T](t T) bool {
13+
$match T.typ {
14+
int {
15+
return true
16+
}
17+
$else {
18+
return false
19+
}
20+
}
21+
}
22+
23+
type FooInt = int
24+
25+
fn test_main() {
26+
assert check_unaliased(1)
27+
assert !check_unaliased('')
28+
assert !check_unaliased(1.2)
29+
assert check_unaliased(FooInt(0))
30+
31+
assert check_typ(1)
32+
assert !check_typ('')
33+
assert !check_typ(1.2)
34+
assert !check_typ(FooInt(0))
35+
}

0 commit comments

Comments
 (0)