From ba07e486919e9a634d7afd5e6fcbb074db414de2 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sun, 18 Dec 2022 16:29:44 +0800 Subject: [PATCH] ast, checker: fix checking generic struct type mismatch (#16701) --- vlib/v/ast/table.v | 7 ++++++- vlib/v/checker/fn.v | 5 +---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/vlib/v/ast/table.v b/vlib/v/ast/table.v index 2ef862e65c..097ceacb3a 100644 --- a/vlib/v/ast/table.v +++ b/vlib/v/ast/table.v @@ -1799,7 +1799,12 @@ pub fn (mut t Table) generic_type_names(generic_type Type) []string { } Struct, Interface, SumType { if sym.info.is_generic { - names << sym.info.generic_types.map(t.sym(it).name) + if sym.generic_types.len > 0 { + // Foo[U] (declaration: Foo[T]) + names << sym.generic_types.map(t.sym(it).name) + } else { + names << sym.info.generic_types.map(t.sym(it).name) + } } } else {} diff --git a/vlib/v/checker/fn.v b/vlib/v/checker/fn.v index 24d6917fee..c86af5079f 100644 --- a/vlib/v/checker/fn.v +++ b/vlib/v/checker/fn.v @@ -131,10 +131,7 @@ fn (mut c Checker) fn_decl(mut node ast.FnDecl) { c.error('fixed array cannot be returned by function', node.return_type_pos) } // Ensure each generic type of the parameter was declared in the function's definition - // TODO: fix inconsistent return_type type case - if node.return_type.has_flag(.generic) && (return_sym.kind == .any - || (return_sym.kind == .array - && c.table.sym((return_sym.info as ast.Array).elem_type).kind == .any)) { + if node.return_type.has_flag(.generic) { generic_names := c.table.generic_type_names(node.return_type) for name in generic_names { if name !in node.generic_names {