mirror of
https://github.com/vlang/v.git
synced 2023-08-10 21:13:21 +03:00
cgen: fix struct embedding method when receiver is ptr (#7710)
This commit is contained in:
parent
13cd7e88ef
commit
b8af81240a
@ -436,7 +436,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
||||
} else {
|
||||
g.write('${name}(')
|
||||
}
|
||||
if node.receiver_type.is_ptr() && !node.left_type.is_ptr() {
|
||||
if node.receiver_type.is_ptr() && (!node.left_type.is_ptr() || node.from_embed_type != 0) {
|
||||
// The receiver is a reference, but the caller provided a value
|
||||
// Add `&` automatically.
|
||||
// TODO same logic in call_args()
|
||||
@ -455,7 +455,12 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
|
||||
g.expr(node.left)
|
||||
if node.from_embed_type != 0 {
|
||||
embed_name := typ_sym.embed_name()
|
||||
g.write('.$embed_name')
|
||||
if node.left_type.is_ptr() {
|
||||
g.write('->')
|
||||
} else {
|
||||
g.write('.')
|
||||
}
|
||||
g.write(embed_name)
|
||||
}
|
||||
}
|
||||
if has_cast {
|
||||
|
@ -79,3 +79,18 @@ fn test_assign() {
|
||||
h.x = 5
|
||||
assert h.x == 5
|
||||
}
|
||||
|
||||
struct Eggs {}
|
||||
|
||||
fn (f &Eggs) test(x int) int {
|
||||
return x
|
||||
}
|
||||
|
||||
struct Breakfast {
|
||||
Eggs
|
||||
}
|
||||
|
||||
fn test_embed_method_receiver_ptr() {
|
||||
b := Breakfast{}
|
||||
assert b.test(5) == 5
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user