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

checker: allow struct updates from struct aliases (#16567)

This commit is contained in:
Tim Marston 2022-12-01 22:28:10 +00:00 committed by GitHub
parent 18d98a5e16
commit cbe64cb543
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 27 deletions

View File

@ -598,7 +598,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
if node.has_update_expr { if node.has_update_expr {
update_type := c.expr(node.update_expr) update_type := c.expr(node.update_expr)
node.update_expr_type = update_type node.update_expr_type = update_type
if c.table.sym(update_type).kind != .struct_ { if c.table.final_sym(update_type).kind != .struct_ {
s := c.table.type_to_str(update_type) s := c.table.type_to_str(update_type)
c.error('expected struct, found `${s}`', node.update_expr.pos()) c.error('expected struct, found `${s}`', node.update_expr.pos())
} else if update_type != node.typ { } else if update_type != node.typ {

View File

@ -1,28 +1,28 @@
vlib/v/checker/tests/struct_init_update_type_err.vv:11:6: error: expected struct, found `int` vlib/v/checker/tests/struct_init_update_type_err.vv:13:6: error: expected struct, found `int`
9 | i := 2 11 | i := 2
10 | _ := Foo{ 12 | _ := Foo{
11 | ...i 13 | ...i
| ^ | ^
12 | name: 'f2' 14 | name: 'f2'
13 | } 15 | }
vlib/v/checker/tests/struct_init_update_type_err.vv:16:6: error: expected struct, found `&int` vlib/v/checker/tests/struct_init_update_type_err.vv:18:6: error: expected struct, found `&int`
14 | p := &i 16 | p := &i
15 | _ = Foo{ 17 | _ = Foo{
16 | ...p 18 | ...p
| ^ | ^
17 | } 19 | }
18 | f2 := Foo2{} 20 | f2 := Foo2{}
vlib/v/checker/tests/struct_init_update_type_err.vv:20:6: error: struct `Foo2` is not compatible with struct `Foo` vlib/v/checker/tests/struct_init_update_type_err.vv:22:6: error: struct `Foo2` is not compatible with struct `Foo`
18 | f2 := Foo2{} 20 | f2 := Foo2{}
19 | _ = Foo{ 21 | _ = Foo{
20 | ...f2 22 | ...f2
| ~~ | ~~
21 | } 23 | }
22 | _ = Foo{ 24 | _ = Foo{
vlib/v/checker/tests/struct_init_update_type_err.vv:32:6: error: struct `Empty` is not compatible with struct `Foo` vlib/v/checker/tests/struct_init_update_type_err.vv:34:6: error: struct `Empty` is not compatible with struct `Foo`
30 | e := Empty{} 32 | e := Empty{}
31 | _ = Foo{ 33 | _ = Foo{
32 | ...e 34 | ...e
| ^ | ^
33 | } 35 | }
34 | } 36 | }

View File

@ -3,7 +3,9 @@ struct Foo {
age int age int
} }
struct Foo2 {b bool} struct Foo2 {
b bool
}
fn main() { fn main() {
i := 2 i := 2
@ -33,3 +35,11 @@ fn empty() {
} }
} }
type AliasFoo = Foo
fn alias() {
a := AliasFoo{}
_ = AliasFoo{
...a
}
}