From 0dc4e9b46a3d7c270bd4691e007f38ac675d2035 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Sun, 31 Aug 2025 04:17:47 -0300 Subject: [PATCH] checker: add `T.typ` and `T.unaliased_typ` checking to `$match` (fix #25200) (#25202) --- vlib/v/checker/match.v | 4 +++ .../comptime_match_unaliased_typ_test.v | 35 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 vlib/v/tests/comptime/comptime_match_unaliased_typ_test.v diff --git a/vlib/v/checker/match.v b/vlib/v/checker/match.v index 8d33203ca7..90780494c1 100644 --- a/vlib/v/checker/match.v +++ b/vlib/v/checker/match.v @@ -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 diff --git a/vlib/v/tests/comptime/comptime_match_unaliased_typ_test.v b/vlib/v/tests/comptime/comptime_match_unaliased_typ_test.v new file mode 100644 index 0000000000..5dc9e74fd3 --- /dev/null +++ b/vlib/v/tests/comptime/comptime_match_unaliased_typ_test.v @@ -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)) +}