From 0876cf86edf392e1f38d45158df548061d8a80ef Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 30 Aug 2022 14:22:14 +0800 Subject: [PATCH] cgen: fix struct init with update expr (fix #15595) (#15603) --- vlib/v/gen/c/struct.v | 2 ++ vlib/v/tests/struct_init_with_update_test.v | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+) diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index babd6cbe3..624536454 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -195,7 +195,9 @@ fn (mut g Gen) struct_init(node ast.StructInit) { if is_update_tmp_var { g.write(tmp_update_var) } else { + g.write('(') g.expr(node.update_expr) + g.write(')') } if node.update_expr_type.is_ptr() { g.write('->') diff --git a/vlib/v/tests/struct_init_with_update_test.v b/vlib/v/tests/struct_init_with_update_test.v index c2c398e04..cf1a1dd6c 100644 --- a/vlib/v/tests/struct_init_with_update_test.v +++ b/vlib/v/tests/struct_init_with_update_test.v @@ -30,3 +30,21 @@ fn test_struct_init_with_update_expr() { assert o.height == 175 assert o.age == 21 } + +struct Foo { + s string + n int +} + +fn test_struct_init_with_update_expr2() { + f := &Foo{ + s: 'AA' + } + b := Foo{ + ...*f + n: 3 + } + println(b) + assert b.s == 'AA' + assert b.n == 3 +} -- 2.30.2