From 8119a297f7236fc7331ae13fe41ae0c8cbeb1762 Mon Sep 17 00:00:00 2001 From: yuyi Date: Mon, 14 Feb 2022 19:43:36 +0800 Subject: [PATCH] ast: fix error for 'struct embed is interface' (#13465) --- vlib/v/ast/table.v | 2 +- vlib/v/tests/struct_embed_is_interface_test.v | 53 +++++++++++++++++++ 2 files changed, 54 insertions(+), 1 deletion(-) create mode 100644 vlib/v/tests/struct_embed_is_interface_test.v diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index aca8bf58d6..367baacfc4 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -1384,7 +1384,7 @@ pub fn (t Table) does_type_implement_interface(typ Type, inter_typ Type) bool { } // verify methods for imethod in inter_sym.info.methods { - if method := sym.find_method(imethod.name) { + if method := t.find_method_with_embeds(sym, imethod.name) { msg := t.is_same_method(imethod, method) if msg.len > 0 { return false diff --git a/vlib/v/tests/struct_embed_is_interface_test.v b/vlib/v/tests/struct_embed_is_interface_test.v new file mode 100644 index 0000000000..9625e2028c --- /dev/null +++ b/vlib/v/tests/struct_embed_is_interface_test.v @@ -0,0 +1,53 @@ +pub struct Base { +mut: + x int + y int +} + +pub fn (mut b Base) init() {} + +pub fn (b Base) get_pos() (int, int) { + return b.x, b.y +} + +pub struct Label { + Base +} + +pub interface Widget { +mut: + init() +} + +pub interface Layoutable { + get_pos() (int, int) +} + +pub struct Layout { + Base +mut: + widgets []Widget +} + +pub fn (mut l Layout) layout() { + for wd in l.widgets { + if wd is Layoutable { + lw := wd as Layoutable + dump(lw.get_pos()) + x, y := lw.get_pos() + assert x == 10 + assert y == 20 + } else { + println('wd is NOT Layoutable') + } + } +} + +fn test_struct_embed_is_interface() { + mut l := Layout{} + l.widgets << Label{ + x: 10 + y: 20 + } + l.layout() +}