1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

checker: check sumtype in unknown type (#17225)

This commit is contained in:
yuyi 2023-02-05 19:02:30 +08:00 committed by GitHub
parent c40e25b028
commit fc65de9bba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 0 deletions

View File

@ -165,6 +165,12 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
c.error('left operand to `${node.op}` does not match the array element type: ${err.msg()}',
left_right_pos)
}
} else {
if mut node.right is ast.ArrayInit {
for i, typ in node.right.expr_types {
c.ensure_type_exists(typ, node.right.exprs[i].pos()) or {}
}
}
}
}
.map {

View File

@ -0,0 +1,8 @@
vlib/v/checker/tests/sumtype_in_unknown_types_err.vv:11:25: error: unknown type `FooD`.
Did you mean `Foo`?
9 | fn main() {
10 | foo := Foo(FooA{})
11 | if foo in [FooB, FooC, FooD] {
| ~~~~
12 | panic('not possible')
13 | } else {

View File

@ -0,0 +1,16 @@
type Foo = FooA | FooB | FooC
struct FooA {}
struct FooB {}
struct FooC {}
fn main() {
foo := Foo(FooA{})
if foo in [FooB, FooC, FooD] {
panic('not possible')
} else {
println('ok')
}
}