From 988779846f96fccf992c2ed6aa9375f1d78daa5c Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 2 Dec 2021 00:11:50 +0800 Subject: [PATCH] checker: fix map init with enum keys (#12637) --- vlib/v/checker/checker.v | 1 + vlib/v/tests/map_init_with_enum_keys_test.v | 20 ++++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 vlib/v/tests/map_init_with_enum_keys_test.v diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index ccd538412e..3cfef671ff 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -7744,6 +7744,7 @@ pub fn (mut c Checker) map_init(mut node ast.MapInit) ast.Type { continue } val := node.vals[i] + c.expected_type = key0_type key_type := c.expr(key) c.expected_type = val0_type val_type := c.expr(val) diff --git a/vlib/v/tests/map_init_with_enum_keys_test.v b/vlib/v/tests/map_init_with_enum_keys_test.v new file mode 100644 index 0000000000..067945126d --- /dev/null +++ b/vlib/v/tests/map_init_with_enum_keys_test.v @@ -0,0 +1,20 @@ +enum En { + ea + eb +} + +struct St { +mut: + m map[En]string +} + +fn test_map_init_with_enum_keys() { + mut st := St{} + + st.m = { + .ea: 'a' + } + + println(st.m) + assert '$st.m' == "{ea: 'a'}" +}