From 9dce8194a8a632f194557aaf9658285997366272 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 27 Jan 2022 19:20:56 +0800 Subject: [PATCH] cgen: fix error of method calls on nested embedded structs (#13292) --- vlib/v/gen/c/fn.v | 9 +++-- .../struct_multi_embed_method_call_test.v | 33 +++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/struct_multi_embed_method_call_test.v diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index f0c7680435..073345e19f 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -1047,10 +1047,15 @@ fn (mut g Gen) method_call(node ast.CallExpr) { } else { g.expr(node.left) } - for embed in node.from_embed_types { + for i, embed in node.from_embed_types { embed_sym := g.table.sym(embed) embed_name := embed_sym.embed_name() - if node.left_type.is_ptr() { + is_left_ptr := if i == 0 { + node.left_type.is_ptr() + } else { + node.from_embed_types[i - 1].is_ptr() + } + if is_left_ptr { g.write('->') } else { g.write('.') diff --git a/vlib/v/tests/struct_multi_embed_method_call_test.v b/vlib/v/tests/struct_multi_embed_method_call_test.v new file mode 100644 index 0000000000..5db5428c50 --- /dev/null +++ b/vlib/v/tests/struct_multi_embed_method_call_test.v @@ -0,0 +1,33 @@ +pub struct Boundary {} + +pub fn (b Boundary) contains(x int, y int) bool { + return false +} + +pub struct Base { + Boundary +} + +pub fn (mut b Base) on_event(x int, y int) { + if b.Boundary.contains(x, y) { + } + if b.contains(x, y) { + } +} + +pub struct ListBox { + Base +} + +pub fn (mut lb ListBox) on_event(x int, y int) { + if lb.Base.Boundary.contains(x, y) { + } + if lb.contains(x, y) { + } +} + +fn test_struct_multi_embed_method_call() { + mut list_box := ListBox{} + list_box.on_event(11, 22) + assert true +}