From dc0b0c83c6329d247901b0f6c911e90cc953c17d Mon Sep 17 00:00:00 2001 From: yuyi Date: Tue, 15 Feb 2022 01:22:44 +0800 Subject: [PATCH] ast: fix error for interface embedding call (#13466) --- vlib/v/ast/table.v | 20 +++++++++++++++ vlib/v/tests/interface_embedding_call_test.v | 26 ++++++++++++++++++++ 2 files changed, 46 insertions(+) create mode 100644 vlib/v/tests/interface_embedding_call_test.v diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 367baacfc4..1767d16a0b 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -426,6 +426,26 @@ pub fn (t &Table) find_method_from_embeds(sym &TypeSymbol, method_name string) ? } else if found_methods.len > 1 { return error('ambiguous method `$method_name`') } + } else if sym.info is Interface { + mut found_methods := []Fn{} + mut embed_of_found_methods := []Type{} + for embed in sym.info.ifaces { + embed_sym := t.sym(embed) + if m := t.find_method(embed_sym, method_name) { + found_methods << m + embed_of_found_methods << embed + } else { + method, types := t.find_method_from_embeds(embed_sym, method_name) or { continue } + found_methods << method + embed_of_found_methods << embed + embed_of_found_methods << types + } + } + if found_methods.len == 1 { + return found_methods[0], embed_of_found_methods + } else if found_methods.len > 1 { + return error('ambiguous method `$method_name`') + } } else if sym.info is Aggregate { for typ in sym.info.types { agg_sym := t.sym(typ) diff --git a/vlib/v/tests/interface_embedding_call_test.v b/vlib/v/tests/interface_embedding_call_test.v new file mode 100644 index 0000000000..e552d710cd --- /dev/null +++ b/vlib/v/tests/interface_embedding_call_test.v @@ -0,0 +1,26 @@ +module main + +fn test_interface_embedding_call() { + g1 := G1{} + do_the_greet(g1) +} + +struct G1 {} + +fn (g G1) greet() string { + return 'hello from G1' +} + +fn do_the_greet(g ParentGreeter) { + greet := g.greet() + println('Someone says: $greet') + assert greet == 'hello from G1' +} + +interface ParentGreeter { + Greeter +} + +interface Greeter { + greet() string +}