gen/c: Fix shared variable are not passed correctly to methods with shared type in recevier (#13320)

pull/13338/head
Naoki MATSUMOTO 2022-02-01 21:04:54 +09:00
parent 09bb0af59c
commit 004ee0ac25
2 changed files with 28 additions and 2 deletions

View File

@ -1006,7 +1006,8 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
if !node.left.is_lvalue() {
g.write('ADDR($rec_cc_type, ')
has_cast = true
} else {
// if receiver has shared_f, the struct ptr itself is passed directly.
} else if !node.receiver_type.has_flag(.shared_f) {
g.write('&')
}
}
@ -1062,7 +1063,8 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
}
g.write(embed_name)
}
if node.left_type.has_flag(.shared_f) {
// if receiver has shared_f, the struct ptr itself is passed directly.
if node.left_type.has_flag(.shared_f) && !node.receiver_type.has_flag(.shared_f) {
g.write('->val')
}
}

View File

@ -50,3 +50,27 @@ fn test_shared_receiver_lock() {
assert x.a == 7 && y.a == 5
}
}
struct St2 {
mut:
a map[string]int
}
fn (shared x St2) f() {
lock x {
x.a["a"] = 123
}
}
fn test_shared_receiver_lock_2() {
shared x := St2 {
a: map[string]int
}
x.f()
rlock x {
assert x.a["a"] == 123
}
}