Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ast, checker, cgen: fix interface embeded methods call(fix #16496) #19936

Merged
merged 1 commit into from
Nov 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 29 additions & 13 deletions vlib/v/ast/ast.v
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,12 @@ pub mut:
pos token.Pos // function declaration position
}

pub fn (f &FnDecl) new_method_with_receiver_type(new_type Type) FnDecl {
pub fn (f &FnDecl) new_method_with_receiver_type(new_type_ Type) FnDecl {
new_type := if f.params[0].typ.is_ptr() && !new_type_.is_ptr() {
new_type_.ref()
} else {
new_type_
}
unsafe {
mut new_method := f
new_method.params = f.params.clone()
Expand Down Expand Up @@ -624,17 +629,18 @@ pub:
pos token.Pos
return_type_pos token.Pos
pub mut:
return_type Type
receiver_type Type // != 0, when .is_method == true
name string
params []Param
source_fn voidptr // set in the checker, while processing fn declarations // TODO get rid of voidptr
usages int
generic_names []string
dep_names []string // globals or consts dependent names
attrs []Attr // all fn attributes
is_conditional bool // true for `[if abc]fn(){}`
ctdefine_idx int // the index of the attribute, containing the compile time define [if mytag]
return_type Type
receiver_type Type // != 0, when .is_method == true
name string
params []Param
source_fn voidptr // set in the checker, while processing fn declarations // TODO get rid of voidptr
usages int
generic_names []string
dep_names []string // globals or consts dependent names
attrs []Attr // all fn attributes
is_conditional bool // true for `[if abc]fn(){}`
ctdefine_idx int // the index of the attribute, containing the compile time define [if mytag]
from_embeded_type Type // for interface only, fn from the embedded interface
}

fn (f &Fn) method_equals(o &Fn) bool {
Expand Down Expand Up @@ -672,7 +678,12 @@ pub fn (p &Param) specifier() string {
}
}

pub fn (f &Fn) new_method_with_receiver_type(new_type Type) Fn {
pub fn (f &Fn) new_method_with_receiver_type(new_type_ Type) Fn {
new_type := if f.params[0].typ.is_ptr() && !new_type_.is_ptr() {
new_type_.ref()
} else {
new_type_
}
unsafe {
mut new_method := f
new_method.params = f.params.clone()
Expand All @@ -681,6 +692,11 @@ pub fn (f &Fn) new_method_with_receiver_type(new_type Type) Fn {
new_method.params[i].typ = new_type
}
}
new_method.from_embeded_type = if f.from_embeded_type != 0 {
f.from_embeded_type
} else {
f.params[0].typ
}
new_method.params[0].typ = new_type

return *new_method
Expand Down
4 changes: 4 additions & 0 deletions vlib/v/checker/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1762,6 +1762,10 @@ fn (mut c Checker) method_call(mut node ast.CallExpr) ast.Type {
if m := c.table.find_method(left_sym, method_name) {
method = m
has_method = true
if left_sym.kind == .interface_ && m.from_embeded_type != 0 {
is_method_from_embed = true
node.from_embed_types = [m.from_embeded_type]
}
} else {
if final_left_sym.kind in [.struct_, .sum_type, .interface_, .alias, .array] {
mut parent_type := ast.void_type
Expand Down
63 changes: 49 additions & 14 deletions vlib/v/gen/c/fn.v
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,8 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
}
is_array_method_first_last_repeat := final_left_sym.kind == .array
&& node.name in ['first', 'last', 'repeat']
is_interface := left_sym.kind == .interface_
&& g.table.sym(node.receiver_type).kind == .interface_
if node.receiver_type.is_ptr() && (!left_type.is_ptr()
|| node.from_embed_types.len != 0 || (left_type.has_flag(.shared_f) && node.name != 'str')) {
// The receiver is a reference, but the caller provided a value
Expand Down Expand Up @@ -1461,7 +1463,7 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
g.write('(map[]){')
g.expr(node.left)
g.write('}[0]')
} else if node.from_embed_types.len > 0 {
} else if !is_interface && node.from_embed_types.len > 0 {
n_ptr := node.left_type.nr_muls() - 1
if n_ptr > 0 {
g.write('(')
Expand All @@ -1471,23 +1473,56 @@ fn (mut g Gen) method_call(node ast.CallExpr) {
} else {
g.expr(node.left)
}
} else if is_interface && node.from_embed_types.len > 0 {
if g.out.last_n(1) == '&' {
g.go_back(1)
}
if node.receiver_type.is_ptr() && left_type.is_ptr() {
// (main__IFoo*)bar
g.write('(')
g.write(g.table.sym(node.from_embed_types.last()).cname)
g.write('*)')
g.expr(node.left)
} else if node.receiver_type.is_ptr() && !left_type.is_ptr() {
// (main__IFoo*)&bar
g.write('(')
g.write(g.table.sym(node.from_embed_types.last()).cname)
g.write('*)&')
g.expr(node.left)
} else if !node.receiver_type.is_ptr() && left_type.is_ptr() {
// *((main__IFoo*)bar)
g.write('*((')
g.write(g.table.sym(node.from_embed_types.last()).cname)
g.write('*)')
g.expr(node.left)
g.write(')')
} else {
// *((main__IFoo*)&bar)
g.write('*((')
g.write(g.table.sym(node.from_embed_types.last()).cname)
g.write('*)&')
g.expr(node.left)
g.write(')')
}
} else {
g.expr(node.left)
}
for i, embed in node.from_embed_types {
embed_sym := g.table.sym(embed)
embed_name := embed_sym.embed_name()
is_left_ptr := if i == 0 {
left_type.is_ptr()
} else {
node.from_embed_types[i - 1].is_ptr()
}
if is_left_ptr {
g.write('->')
} else {
g.write('.')
if !is_interface || node.from_embed_types.len == 0 {
for i, embed in node.from_embed_types {
embed_sym := g.table.sym(embed)
embed_name := embed_sym.embed_name()
is_left_ptr := if i == 0 {
left_type.is_ptr()
} else {
node.from_embed_types[i - 1].is_ptr()
}
if is_left_ptr {
g.write('->')
} else {
g.write('.')
}
g.write(embed_name)
}
g.write(embed_name)
}
if left_type.has_flag(.shared_f)
&& (left_type != node.receiver_type || is_array_method_first_last_repeat) {
Expand Down
28 changes: 28 additions & 0 deletions vlib/v/tests/interface_embedding_call_test.v
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,31 @@ interface ParentGreeter {
interface Greeter {
greet() string
}

// for issue 16496
interface Foo {
a_method()
}

fn (f Foo) foo_method() int {
return 0
}

interface Bar {
Foo
}

fn (b Bar) bar_method() int {
// The test calls the method of the embedded interface in the interface method
return b.foo_method()
}

struct Derived {}

fn (d &Derived) a_method() {
}

fn test_embedding_method_call_cgen() {
bar := Bar(Derived{})
assert bar.bar_method() == 0
}
Loading