From 5ae3637d276bfefcd6f6d624cc223bc5696485a3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20D=C3=A4schle?= Date: Fri, 1 Jan 2021 14:36:07 +0100 Subject: [PATCH] checker: revert embedded methods fix (#7780) --- examples/vweb/vweb_assets/vweb_assets.v | 4 +-- vlib/v/checker/checker.v | 43 ++++++++++++------------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/examples/vweb/vweb_assets/vweb_assets.v b/examples/vweb/vweb_assets/vweb_assets.v index e5faf57717..64177fe452 100644 --- a/examples/vweb/vweb_assets/vweb_assets.v +++ b/examples/vweb/vweb_assets/vweb_assets.v @@ -41,9 +41,9 @@ pub fn (mut app App) index() { } fn (mut app App) text() vweb.Result { - return app.text('Hello, world from vweb!') + return app.Context.text('Hello, world from vweb!') } fn (mut app App) time() vweb.Result { - return app.text(time.now().format()) + return app.Context.text(time.now().format()) } diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 2380753b6b..126f574f2a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -1289,34 +1289,31 @@ pub fn (mut c Checker) call_method(mut call_expr ast.CallExpr) table.Type { method = m has_method = true } else { + // can this logic be moved to table.type_find_method() so it can be used from anywhere + if left_type_sym.info is table.Struct { + mut found_methods := []table.Fn{} + mut embed_of_found_methods := []table.Type{} + for embed in left_type_sym.info.embeds { + embed_sym := c.table.get_type_symbol(embed) + if m := c.table.type_find_method(embed_sym, method_name) { + found_methods << m + embed_of_found_methods << embed + } + } + if found_methods.len == 1 { + method = found_methods[0] + has_method = true + is_method_from_embed = true + call_expr.from_embed_type = embed_of_found_methods[0] + } else if found_methods.len > 1 { + c.error('ambiguous method `$method_name`', call_expr.pos) + } + } if left_type_sym.kind == .aggregate { // the error message contains the problematic type unknown_method_msg = err } } - // check even if method is found, we could be overriding a method (TODO: should this be allowed?) - // can this logic be moved to table.type_find_method() so it can be used from anywhere - // TODO: should overriden methods use embed type name to access it's methods? - // eg go does: `fn (mut app App) text() vweb.Result { return app.Context.text() }` - if left_type_sym.info is table.Struct { - mut found_methods := []table.Fn{} - mut embed_of_found_methods := []table.Type{} - for embed in left_type_sym.info.embeds { - embed_sym := c.table.get_type_symbol(embed) - if m := c.table.type_find_method(embed_sym, method_name) { - found_methods << m - embed_of_found_methods << embed - } - } - if found_methods.len == 1 { - method = found_methods[0] - has_method = true - is_method_from_embed = true - call_expr.from_embed_type = embed_of_found_methods[0] - } else if found_methods.len > 1 { - c.error('ambiguous method `$method_name`', call_expr.pos) - } - } if has_method { if !method.is_pub && !c.is_builtin_mod && !c.pref.is_test && left_type_sym.mod != c.mod && left_type_sym.mod != '' { // method.mod != c.mod {