From 02f791d9fee1829b640525b1b6fcff1ff00c4c14 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 10 Jan 2022 15:12:46 +0800 Subject: [PATCH] checker: fix returning optional empty map (#13113) --- vlib/v/checker/containers.v | 2 +- vlib/v/tests/option_empty_map_test.v | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/option_empty_map_test.v diff --git a/vlib/v/checker/containers.v b/vlib/v/checker/containers.v index 17bfa99aab..8db74f7501 100644 --- a/vlib/v/checker/containers.v +++ b/vlib/v/checker/containers.v @@ -202,7 +202,7 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type { sym := c.table.sym(c.expected_type) if sym.kind == .map { info := sym.map_info() - node.typ = c.expected_type + node.typ = c.expected_type.clear_flag(.optional) node.key_type = info.key_type node.value_type = info.value_type return node.typ diff --git a/vlib/v/tests/option_empty_map_test.v b/vlib/v/tests/option_empty_map_test.v new file mode 100644 index 0000000000..fd5ad743e7 --- /dev/null +++ b/vlib/v/tests/option_empty_map_test.v @@ -0,0 +1,13 @@ +fn opt() ?map[string]string { + return {} +} + +fn test_option_empty_map() { + x := opt() or { + assert false + return + } + + dump(x) + assert '$x' == '{}' +}