From 9a744b6750f03a0e2ce27daac93bc58bcdf435d5 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 21 Feb 2021 17:54:30 +0800 Subject: [PATCH] cgen: fix in map_literal (fix #8868) (#8871) --- vlib/builtin/map_test.v | 8 +++++++- vlib/v/checker/checker.v | 5 +++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/vlib/builtin/map_test.v b/vlib/builtin/map_test.v index e18142c692..14d6b66ff7 100644 --- a/vlib/builtin/map_test.v +++ b/vlib/builtin/map_test.v @@ -131,7 +131,7 @@ fn test_map_init() { one := 'one' three := 'three' m := map{ - one: 1 + one: 1 'two': 2 three: 1 + 2 } @@ -626,3 +626,9 @@ fn test_map_assign_empty_map_init() { assert a == map[string]int{} assert '$a' == '{}' } + +fn test_in_map_literal() { + assert 1 in map{ + 1: 'one' + } +} diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 5c78a68db8..f351ac1797 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -745,11 +745,12 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type { } } .map { - elem_type := right.map_info().key_type - c.check_expected(left_type, elem_type) or { + map_info := right.map_info() + c.check_expected(left_type, map_info.key_type) or { c.error('left operand to `$infix_expr.op` does not match the map key type: $err', left_right_pos) } + infix_expr.left_type = map_info.key_type } .string { c.check_expected(left_type, right_type) or {