1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

cgen: fix shared map delete (#15431)

This commit is contained in:
yuyi 2022-08-16 04:42:57 +08:00 committed by GitHub
parent 26986eb2f8
commit 8be85e7590
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 6 deletions

View File

@ -827,24 +827,40 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
left_info := left_sym.info as ast.Map
elem_type_str := g.typ(left_info.key_type)
g.write('map_delete(')
if left_type.has_flag(.shared_f) {
if left_type.is_ptr() {
g.write('&')
}
g.expr(node.left)
g.write('->val')
} else {
if left_type.is_ptr() {
g.expr(node.left)
} else {
g.write('&')
g.expr(node.left)
}
}
g.write(', &($elem_type_str[]){')
g.expr(node.args[0].expr)
g.write('})')
return
} else if left_sym.kind == .array && node.name == 'delete' {
g.write('array_delete(')
if left_type.has_flag(.shared_f) {
if left_type.is_ptr() {
g.write('&')
}
g.expr(node.left)
g.write('->val')
} else {
if left_type.is_ptr() {
g.expr(node.left)
} else {
g.write('&')
g.expr(node.left)
}
}
g.write(', ')
g.expr(node.args[0].expr)
g.write(')')

View File

@ -0,0 +1,14 @@
fn test_shared_map_delete() {
shared store := map[string]int{}
lock store {
store['abc'] = 5
store['xyz'] = 10
}
lock store {
assert store.len == 2
store.delete('abc')
assert store.len == 1
assert store['xyz'] == 10
}
}