From d23e70f546846c42129f45e8d0febec50e084c2d Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Thu, 16 Jan 2025 18:04:57 -0300 Subject: [PATCH] cgen: fix option unwrapping on heap var (#23489) --- vlib/v/gen/c/cgen.v | 9 +++++++-- vlib/v/tests/options/option_heap_var_test.v | 21 +++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 vlib/v/tests/options/option_heap_var_test.v diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 7688fff78d117f..94c38ba25fbb9c 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -5115,8 +5115,13 @@ fn (mut g Gen) ident(node ast.Ident) { } } } - if node.obj.ct_type_var != .smartcast && node.obj.is_unwrapped { - g.write('.data') + if i == 0 && node.obj.ct_type_var != .smartcast && node.obj.is_unwrapped { + dot := if !node.obj.orig_type.is_ptr() && obj_sym.is_heap() { + '->' + } else { + '.' + } + g.write('${dot}data') } g.write(')') } diff --git a/vlib/v/tests/options/option_heap_var_test.v b/vlib/v/tests/options/option_heap_var_test.v new file mode 100644 index 00000000000000..c4b879653431dc --- /dev/null +++ b/vlib/v/tests/options/option_heap_var_test.v @@ -0,0 +1,21 @@ +@[heap] +struct Foo { + a int +} + +struct EdgeService { + foo Foo +} + +fn t[S](s S) { +} + +fn test_main() { + mut svc := ?EdgeService(EdgeService{}) + if svc != none { + t(svc) + assert true + } else { + assert false + } +}