From 44c3fcecd9776c06b89bdb1ae09330cde50ad299 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 22 Sep 2022 18:53:11 +0800 Subject: [PATCH] cgen: fix reference embed method call (#15842) --- vlib/v/gen/c/fn.v | 10 ++++++++++ vlib/v/tests/embed_method_call_test.v | 23 +++++++++++++++++++++++ 2 files changed, 33 insertions(+) create mode 100644 vlib/v/tests/embed_method_call_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 2e3f40e8b5..7ce5e05a89 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1086,6 +1086,16 @@ fn (mut g Gen) method_call(node ast.CallExpr) { g.write('(map[]){') g.expr(node.left) g.write('}[0]') + } else if node.from_embed_types.len > 0 { + n_ptr := node.left_type.nr_muls() - 1 + if n_ptr > 0 { + g.write('(') + g.write('*'.repeat(n_ptr)) + g.expr(node.left) + g.write(')') + } else { + g.expr(node.left) + } } else { g.expr(node.left) } diff --git a/vlib/v/tests/embed_method_call_test.v b/vlib/v/tests/embed_method_call_test.v new file mode 100644 index 0000000000..c1cc7afe33 --- /dev/null +++ b/vlib/v/tests/embed_method_call_test.v @@ -0,0 +1,23 @@ +struct Access {} + +fn (access &Access) acc() bool { + return true +} + +struct Field { + Access +} + +fn test_embed_method_call() { + mut fields := []&Field{} + fields << &Field{} + + mut rets := []bool{} + for mut field in fields { + println(field.acc()) + rets << field.acc() + } + + assert rets.len == 1 + assert rets[0] +}