From b4c5fa8ca030fb834730aa8a6a0dc3f31017d05b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Wed, 30 Dec 2020 21:50:58 +0100 Subject: [PATCH] cgen: fix embed field access from reference (#7724) --- vlib/v/gen/cgen.v | 9 +++++++-- vlib/v/tests/struct_embed_test.v | 13 ++++++++++++- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/vlib/v/gen/cgen.v b/vlib/v/gen/cgen.v index ffa5fd1768..892fd531c2 100644 --- a/vlib/v/gen/cgen.v +++ b/vlib/v/gen/cgen.v @@ -2824,10 +2824,15 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { if node.from_embed_type != 0 { embed_sym := g.table.get_type_symbol(node.from_embed_type) embed_name := embed_sym.embed_name() - g.write('.$embed_name') + if node.expr_type.is_ptr() { + g.write('->') + } else { + g.write('.') + } + g.write(embed_name) } } - if node.expr_type.is_ptr() || sym.kind == .chan { + if (node.expr_type.is_ptr() || sym.kind == .chan) && node.from_embed_type == 0 { g.write('->') } else { // g.write('. /*typ= $it.expr_type */') // ${g.typ(it.expr_type)} /') diff --git a/vlib/v/tests/struct_embed_test.v b/vlib/v/tests/struct_embed_test.v index cf18f2b02b..ac0165165f 100644 --- a/vlib/v/tests/struct_embed_test.v +++ b/vlib/v/tests/struct_embed_test.v @@ -86,7 +86,9 @@ fn test_embed_is_public() { assert a.Context.name == '' } -struct Eggs {} +struct Eggs { + name string +} fn (f &Eggs) test(x int) int { return x @@ -96,11 +98,20 @@ struct Breakfast { Eggs } +fn (b &Breakfast) name() string { + return b.name +} + fn test_embed_method_receiver_ptr() { b := Breakfast{} assert b.test(5) == 5 } +fn test_embed_field_receiver_ptr() { + b := Breakfast{} + assert b.name() == '' +} + fn test_embed_mutable() { mut a := field_publicity.App{} a.Context = field_publicity.Context{}