diff --git a/vlib/v/gen/c/fn.v b/vlib/v/gen/c/fn.v index 073345e19f..d76e7dfd80 100644 --- a/vlib/v/gen/c/fn.v +++ b/vlib/v/gen/c/fn.v @@ -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') } } diff --git a/vlib/v/tests/shared_lock_2_test.v b/vlib/v/tests/shared_lock_2_test.v index 5070d1b93e..69b7511f00 100644 --- a/vlib/v/tests/shared_lock_2_test.v +++ b/vlib/v/tests/shared_lock_2_test.v @@ -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 + } +} +