mirror of
https://github.com/vlang/v.git
synced 2025-09-13 22:42:26 +03:00
This commit is contained in:
parent
7c780ed8fa
commit
0dc4e9b46a
2 changed files with 39 additions and 0 deletions
|
@ -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'] {
|
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
|
// 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
|
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 {
|
} else {
|
||||||
c.error('`${node.cond}` is not `\$for` field.name.', node.cond.pos)
|
c.error('`${node.cond}` is not `\$for` field.name.', node.cond.pos)
|
||||||
return ast.void_type
|
return ast.void_type
|
||||||
|
|
35
vlib/v/tests/comptime/comptime_match_unaliased_typ_test.v
Normal file
35
vlib/v/tests/comptime/comptime_match_unaliased_typ_test.v
Normal 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))
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue