From 9569c0504c1c0aab07523e46a044266da5d815bc Mon Sep 17 00:00:00 2001 From: shove Date: Wed, 12 Oct 2022 12:54:29 +0800 Subject: [PATCH] cgen: fix map with optional or result on return (#16044) --- vlib/v/gen/c/index.v | 6 +++++- vlib/v/tests/map_value_with_optional_result_test.v | 4 ++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/vlib/v/gen/c/index.v b/vlib/v/gen/c/index.v index 634552a076..60cee5011f 100644 --- a/vlib/v/gen/c/index.v +++ b/vlib/v/gen/c/index.v @@ -378,7 +378,11 @@ fn (mut g Gen) index_of_map(node ast.IndexExpr, sym ast.TypeSymbol) { elem_type_str := if elem_sym.kind == .function { 'voidptr' } else { - g.typ(elem_type.clear_flag(.optional).clear_flag(.result)) + if g.inside_return { + g.typ(elem_type) + } else { + g.typ(elem_type.clear_flag(.optional).clear_flag(.result)) + } } get_and_set_types := elem_sym.kind in [.struct_, .map] if g.is_assign_lhs && !g.is_arraymap_set && !get_and_set_types { diff --git a/vlib/v/tests/map_value_with_optional_result_test.v b/vlib/v/tests/map_value_with_optional_result_test.v index 047f67cf60..71f7c1b0ed 100644 --- a/vlib/v/tests/map_value_with_optional_result_test.v +++ b/vlib/v/tests/map_value_with_optional_result_test.v @@ -9,3 +9,7 @@ fn test_map_value_with_optional_result() { _ := os.max_path_len assert true } + +fn foo(arg map[string]?string) ?string { + return arg['akey'] +}