Skip to content

Commit 0876cf8

Browse files
authored
cgen: fix struct init with update expr (fix #15595) (#15603)
1 parent e355ae7 commit 0876cf8

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

vlib/v/gen/c/struct.v

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,9 @@ fn (mut g Gen) struct_init(node ast.StructInit) {
195195
if is_update_tmp_var {
196196
g.write(tmp_update_var)
197197
} else {
198+
g.write('(')
198199
g.expr(node.update_expr)
200+
g.write(')')
199201
}
200202
if node.update_expr_type.is_ptr() {
201203
g.write('->')

vlib/v/tests/struct_init_with_update_test.v

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,21 @@ fn test_struct_init_with_update_expr() {
3030
assert o.height == 175
3131
assert o.age == 21
3232
}
33+
34+
struct Foo {
35+
s string
36+
n int
37+
}
38+
39+
fn test_struct_init_with_update_expr2() {
40+
f := &Foo{
41+
s: 'AA'
42+
}
43+
b := Foo{
44+
...*f
45+
n: 3
46+
}
47+
println(b)
48+
assert b.s == 'AA'
49+
assert b.n == 3
50+
}

0 commit comments

Comments
 (0)