checker: add T.typ and T.unaliased_typ checking to $match (fix #25200) (#25202)

This commit is contained in:
Felipe Pena 2025-08-31 04:17:47 -03:00 committed by GitHub
parent 7c780ed8fa
commit 0dc4e9b46a
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 39 additions and 0 deletions

View file

@ -104,6 +104,10 @@ fn (mut c Checker) match_expr(mut node ast.MatchExpr) ast.Type {
if c.comptime.inside_comptime_for && node.cond.field_name in ['name', 'typ'] {
// 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
comptime_match_cond_value = c.comptime.comptime_for_field_value.name
} else if mut node.cond.expr is ast.Ident
&& node.cond.gkind_field in [.typ, .unaliased_typ] {
left_type := c.get_expr_type(node.cond.expr)
comptime_match_cond_value = c.table.type_to_str(left_type)
} else {
c.error('`${node.cond}` is not `\$for` field.name.', node.cond.pos)
return ast.void_type

View file

@ -0,0 +1,35 @@
fn check_unaliased[T](t T) bool {
$match T.unaliased_typ {
int {
return true
}
$else {
return false
}
}
}
fn check_typ[T](t T) bool {
$match T.typ {
int {
return true
}
$else {
return false
}
}
}
type FooInt = int
fn test_main() {
assert check_unaliased(1)
assert !check_unaliased('')
assert !check_unaliased(1.2)
assert check_unaliased(FooInt(0))
assert check_typ(1)
assert !check_typ('')
assert !check_typ(1.2)
assert !check_typ(FooInt(0))
}