cgen: fix interface embedding method call (#13553)

pull/13559/head
yuyi 2022-02-21 23:49:38 +08:00 committed by GitHub
parent b842e89acc
commit 07e9ed1a1d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 1 deletions

View File

@ -755,7 +755,9 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
}
left_is_shared := node.left_type.has_flag(.shared_f)
g.write('${c_name(receiver_type_name)}_name_table[')
left_cc_type := g.cc_type(node.left_type, false)
left_type_name := util.no_dots(left_cc_type)
g.write('${c_name(left_type_name)}_name_table[')
g.expr(node.left)
dot := if left_is_shared {
'->val.'

View File

@ -0,0 +1,59 @@
fn test_interface_embedding_method_call() {
mut window := &Window{}
btn := &Button{}
window.initables << btn
window.run()
}
[heap]
pub struct Window {
mut:
initables []Initable
popview PopView = DummyPopup{}
}
interface Initable {
mut:
init(&Window)
}
interface Drawable {
draw()
}
pub fn (mut window Window) run() {
for mut i in window.initables {
i.init(window)
}
for wd in window.initables {
if wd is Drawable {
d := wd as Drawable
d.draw()
}
}
window.popview.draw()
}
struct DummyPopup {}
fn (d DummyPopup) draw() {}
interface PopView {
Drawable
}
[heap]
pub struct Button {
mut:
window &Window = voidptr(0)
}
pub fn (mut b Button) init(window &Window) {
b.window = window
}
pub fn (b Button) draw() {
g := b.window.initables
println(g.len)
assert g.len == 1
}