From eea46c4e1aa9017cb2a225c97422be69d249f92d Mon Sep 17 00:00:00 2001 From: Delyan Angelov Date: Thu, 7 Apr 2022 17:20:14 +0300 Subject: [PATCH] cgen: fix error, when a struct with over 8 fields, is used as a method receiver directly. --- vlib/v/gen/c/struct.v | 4 +++ ...ding_on_struct_with_too_many_fields_test.v | 27 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/vlib/v/gen/c/struct.v b/vlib/v/gen/c/struct.v index a2e85d416c..fa3600c33d 100644 --- a/vlib/v/gen/c/struct.v +++ b/vlib/v/gen/c/struct.v @@ -22,6 +22,10 @@ fn (mut g Gen) struct_init(node ast.StructInit) { if is_amp { g.out.go_back(1) // delete the `&` already generated in `prefix_expr() } + g.write('(') + defer { + g.write(')') + } if g.is_shared && !g.inside_opt_data && !g.is_arraymap_set { mut shared_typ := node.typ.set_flag(.shared_f) shared_styp = g.typ(shared_typ) diff --git a/vlib/v/tests/operator_overloading_on_struct_with_too_many_fields_test.v b/vlib/v/tests/operator_overloading_on_struct_with_too_many_fields_test.v index ed495d287a..ccce689f5a 100644 --- a/vlib/v/tests/operator_overloading_on_struct_with_too_many_fields_test.v +++ b/vlib/v/tests/operator_overloading_on_struct_with_too_many_fields_test.v @@ -28,3 +28,30 @@ fn test_op() { dump(a) dump(b) } + +struct ManyFields { +mut: + f01 int + f02 int + f03 int + f04 int + f05 int + f06 int + f07 int + f08 int + f09 int + f10 int + f11 int + f12 int +} + +fn (mf ManyFields) inc() ManyFields { + mut res := mf + res.f01 += 1 + return res +} + +fn test_a_struct_with_many_fields_can_be_used_as_receiver_directly_without_assigning_to_an_intermediate_variable() { + x := ManyFields{}.inc() + assert x.f01 == 1 +}