cgen: fix mut map with relation op in fn (#5642)

pull/5644/head
Ruofan XU 2020-07-04 00:07:11 +08:00 committed by GitHub
parent 9e949622d3
commit a2395ff3e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 35 additions and 0 deletions

View File

@ -313,3 +313,35 @@ fn test_map_in_mut() {
map_in_mut(mut m)
assert m['one'] == 2
}
fn mut_map_with_relation_op_in_fn(mut m map[string]int) {
if m['one'] == 1 {
m['three'] = 3
}
if m['two'] != 1 {
m['four'] = 4
}
if m['one'] > 0 {
m['five'] = 5
}
if m['one'] < 2 {
m['six'] = 6
}
if m['two'] >= 2 {
m['seven'] = 7
}
if m['two'] <= 2 {
m['eight'] = 8
}
}
fn test_mut_map_with_relation_op_in_fn() {
mut m := {'one':1, 'two':2}
mut_map_with_relation_op_in_fn(mut m)
assert 'three' in m
assert 'four' in m
assert 'five' in m
assert 'six' in m
assert 'seven' in m
assert 'eight' in m
}

View File

@ -2360,6 +2360,9 @@ fn (mut g Gen) index_expr(node ast.IndexExpr) {
} else {
zero := g.type_default(info.value_type)
g.write('(*($elem_type_str*)map_get(')
if node.left_type.is_ptr() {
g.write('*')
}
g.expr(node.left)
g.write(', ')
g.expr(node.index)