Skip to content

Commit cbe64cb

Browse files
authored
checker: allow struct updates from struct aliases (#16567)
1 parent 18d98a5 commit cbe64cb

File tree

3 files changed

+37
-27
lines changed

3 files changed

+37
-27
lines changed

vlib/v/checker/struct.v

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -598,7 +598,7 @@ fn (mut c Checker) struct_init(mut node ast.StructInit) ast.Type {
598598
if node.has_update_expr {
599599
update_type := c.expr(node.update_expr)
600600
node.update_expr_type = update_type
601-
if c.table.sym(update_type).kind != .struct_ {
601+
if c.table.final_sym(update_type).kind != .struct_ {
602602
s := c.table.type_to_str(update_type)
603603
c.error('expected struct, found `${s}`', node.update_expr.pos())
604604
} else if update_type != node.typ {
Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
1-
vlib/v/checker/tests/struct_init_update_type_err.vv:11:6: error: expected struct, found `int`
2-
9 | i := 2
3-
10 | _ := Foo{
4-
11 | ...i
1+
vlib/v/checker/tests/struct_init_update_type_err.vv:13:6: error: expected struct, found `int`
2+
11 | i := 2
3+
12 | _ := Foo{
4+
13 | ...i
55
| ^
6-
12 | name: 'f2'
7-
13 | }
8-
vlib/v/checker/tests/struct_init_update_type_err.vv:16:6: error: expected struct, found `&int`
9-
14 | p := &i
10-
15 | _ = Foo{
11-
16 | ...p
6+
14 | name: 'f2'
7+
15 | }
8+
vlib/v/checker/tests/struct_init_update_type_err.vv:18:6: error: expected struct, found `&int`
9+
16 | p := &i
10+
17 | _ = Foo{
11+
18 | ...p
1212
| ^
13-
17 | }
14-
18 | f2 := Foo2{}
15-
vlib/v/checker/tests/struct_init_update_type_err.vv:20:6: error: struct `Foo2` is not compatible with struct `Foo`
16-
18 | f2 := Foo2{}
17-
19 | _ = Foo{
18-
20 | ...f2
13+
19 | }
14+
20 | f2 := Foo2{}
15+
vlib/v/checker/tests/struct_init_update_type_err.vv:22:6: error: struct `Foo2` is not compatible with struct `Foo`
16+
20 | f2 := Foo2{}
17+
21 | _ = Foo{
18+
22 | ...f2
1919
| ~~
20-
21 | }
21-
22 | _ = Foo{
22-
vlib/v/checker/tests/struct_init_update_type_err.vv:32:6: error: struct `Empty` is not compatible with struct `Foo`
23-
30 | e := Empty{}
24-
31 | _ = Foo{
25-
32 | ...e
20+
23 | }
21+
24 | _ = Foo{
22+
vlib/v/checker/tests/struct_init_update_type_err.vv:34:6: error: struct `Empty` is not compatible with struct `Foo`
23+
32 | e := Empty{}
24+
33 | _ = Foo{
25+
34 | ...e
2626
| ^
27-
33 | }
28-
34 | }
27+
35 | }
28+
36 | }

vlib/v/checker/tests/struct_init_update_type_err.vv

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
struct Foo {
22
name string
3-
age int
3+
age int
44
}
55

6-
struct Foo2 {b bool}
6+
struct Foo2 {
7+
b bool
8+
}
79

810
fn main() {
911
i := 2
@@ -33,3 +35,11 @@ fn empty() {
3335
}
3436
}
3537

38+
type AliasFoo = Foo
39+
40+
fn alias() {
41+
a := AliasFoo{}
42+
_ = AliasFoo{
43+
...a
44+
}
45+
}

0 commit comments

Comments
 (0)