From 305a272067557a19b9f5a2abf2cc6e8fa4bebb32 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 21 Jan 2025 01:50:42 -0300 Subject: [PATCH] cgen: fix unwrapping option interface field (fix #23540) (#23541) --- vlib/v/gen/c/cgen.v | 5 +++- .../options/option_unwrap_selector_test.v | 29 +++++++++++++++++++ 2 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/options/option_unwrap_selector_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index fd6b367f6d94ea..e312755d249629 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -4013,6 +4013,9 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { if i == 0 && (is_option_unwrap || nested_unwrap) { deref := if g.inside_selector { '*'.repeat(field.smartcasts.last().nr_muls() + 1) + } else if sym.kind == .interface && !typ.is_ptr() + && field.orig_type.has_flag(.option) { + '' } else { '*' } @@ -4196,7 +4199,7 @@ fn (mut g Gen) selector_expr(node ast.SelectorExpr) { } g.write(field_name) if is_option_unwrap { - if field_typ.is_ptr() && g.table.final_sym(node.expr_type).kind in [.sum_type, .interface] { + if g.table.final_sym(node.expr_type).kind in [.sum_type, .interface] { g.write('->') } else { g.write('.') diff --git a/vlib/v/tests/options/option_unwrap_selector_test.v b/vlib/v/tests/options/option_unwrap_selector_test.v new file mode 100644 index 00000000000000..68821a2dce9783 --- /dev/null +++ b/vlib/v/tests/options/option_unwrap_selector_test.v @@ -0,0 +1,29 @@ +interface IBar { + opt ?string +} + +struct Bar implements IBar { + opt ?string +} + +struct Foo { + field IBar +} + +fn (f &Foo) t() { + if f.field.opt != none { + assert f.field.opt == 'foo' + } +} + +fn test_main() { + a := Foo{ + field: Bar{ + opt: 'foo' + } + } + if a.field.opt != none { + assert a.field.opt == 'foo' + } + a.t() +}