cgen: fix in map_literal (fix #8868) (#8871)

pull/8879/head
yuyi 2021-02-21 17:54:30 +08:00 committed by GitHub
parent 3907a1ab49
commit 9a744b6750
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 3 deletions

View File

@ -131,7 +131,7 @@ fn test_map_init() {
one := 'one' one := 'one'
three := 'three' three := 'three'
m := map{ m := map{
one: 1 one: 1
'two': 2 'two': 2
three: 1 + 2 three: 1 + 2
} }
@ -626,3 +626,9 @@ fn test_map_assign_empty_map_init() {
assert a == map[string]int{} assert a == map[string]int{}
assert '$a' == '{}' assert '$a' == '{}'
} }
fn test_in_map_literal() {
assert 1 in map{
1: 'one'
}
}

View File

@ -745,11 +745,12 @@ pub fn (mut c Checker) infix_expr(mut infix_expr ast.InfixExpr) table.Type {
} }
} }
.map { .map {
elem_type := right.map_info().key_type map_info := right.map_info()
c.check_expected(left_type, elem_type) or { 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', c.error('left operand to `$infix_expr.op` does not match the map key type: $err',
left_right_pos) left_right_pos)
} }
infix_expr.left_type = map_info.key_type
} }
.string { .string {
c.check_expected(left_type, right_type) or { c.check_expected(left_type, right_type) or {