From eecaa64e9d82ea81ee2dcd8d4234440da7293400 Mon Sep 17 00:00:00 2001 From: Felipe Pena Date: Tue, 4 Feb 2025 10:23:15 -0300 Subject: [PATCH] cgen: fix codegen for option value on `map_set` (fix #23650) (#23652) --- vlib/v/gen/c/assign.v | 2 +- vlib/v/tests/options/option_map_set_test.v | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/options/option_map_set_test.v diff --git a/vlib/v/gen/c/assign.v b/vlib/v/gen/c/assign.v index c6d38dca3d6bc5..94989b7528c0a8 100644 --- a/vlib/v/gen/c/assign.v +++ b/vlib/v/gen/c/assign.v @@ -138,7 +138,7 @@ fn (mut g Gen) expr_with_opt(expr ast.Expr, expr_typ ast.Type, ret_typ ast.Type) defer { g.inside_opt_or_res = old_inside_opt_or_res } - if expr_typ.has_flag(.option) && ret_typ.has_flag(.option) + if expr_typ.has_flag(.option) && ret_typ.has_flag(.option) && !g.is_arraymap_set && expr in [ast.SelectorExpr, ast.DumpExpr, ast.Ident, ast.ComptimeSelector, ast.AsCast, ast.CallExpr, ast.MatchExpr, ast.IfExpr, ast.IndexExpr, ast.UnsafeExpr, ast.CastExpr] { if expr in [ast.Ident, ast.CastExpr] { if expr_typ.idx() != ret_typ.idx() { diff --git a/vlib/v/tests/options/option_map_set_test.v b/vlib/v/tests/options/option_map_set_test.v new file mode 100644 index 00000000000000..ffbf7cf05396e6 --- /dev/null +++ b/vlib/v/tests/options/option_map_set_test.v @@ -0,0 +1,12 @@ +type Any = ?int | ?string + +struct TestConfig { + timeout ?int +} + +fn test_main() { + mut r := TestConfig{} + mut m := map[string]?Any{} + m['timeout'] = r.timeout + assert m.str() == "{'timeout': Option(Any(Option(none)))}" +}