From 9ddf1ec327a1a4e9f69c62a471e89526a89a75fe Mon Sep 17 00:00:00 2001 From: spaceface Date: Wed, 19 May 2021 20:28:17 +0200 Subject: [PATCH] cgen: fix accessing fields in propagated optional subexpressions (#10136) --- vlib/v/gen/c/fn.v | 2 +- vlib/v/tests/option_2_test.v | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 6c1bb41b38..3daeb2e313 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -462,7 +462,7 @@ fn (mut g Gen) call_expr(node ast.CallExpr) { g.write('\n $cur_line $tmp_opt /*U*/') } else { if !g.inside_const { - g.write('\n $cur_line *($unwrapped_styp*)${tmp_opt}.data') + g.write('\n $cur_line (*($unwrapped_styp*)${tmp_opt}.data)') } else { g.write('\n $cur_line $tmp_opt') } diff --git a/vlib/v/tests/option_2_test.v b/vlib/v/tests/option_2_test.v index 40db899fa4..ebb9c7635c 100644 --- a/vlib/v/tests/option_2_test.v +++ b/vlib/v/tests/option_2_test.v @@ -56,3 +56,11 @@ fn test_nested_opt() { a := f(f(f(-3) or { -7 }) or { 4 }) or { 17 } assert a == 4 } + +fn foo() ?string { + return 'hi' +} + +fn test_opt_subexp_field() ? { + assert foo() ?.len == 2 +}