From fec947c7e1608e867ab1ff0bb2c6dc3cd07df8f6 Mon Sep 17 00:00:00 2001 From: yuyi Date: Sat, 1 May 2021 17:50:49 +0800 Subject: [PATCH] ast: minor cleanup in ast.FnDecl (#9950) --- vlib/v/ast/ast.v | 24 ++++++++++++------------ vlib/v/checker/checker.v | 20 ++++++++------------ 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index b1c9e02c63..d2b76192ed 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -379,18 +379,18 @@ pub: attrs []Attr skip_gen bool // this function doesn't need to be generated (for example [if foo]) pub mut: - stmts []Stmt - defer_stmts []DeferStmt - return_type Type - return_type_pos token.Position // `string` in `fn (u User) name() string` position - has_return bool - comments []Comment // comments *after* the header, but *before* `{`; used for InterfaceDecl - next_comments []Comment // coments that are one line after the decl; used for InterfaceDecl - source_file &File = 0 - scope &Scope - label_names []string - pos token.Position // function declaration position - cur_generic_types []Type + stmts []Stmt + defer_stmts []DeferStmt + return_type Type + return_type_pos token.Position // `string` in `fn (u User) name() string` position + has_return bool + comments []Comment // comments *after* the header, but *before* `{`; used for InterfaceDecl + next_comments []Comment // coments that are one line after the decl; used for InterfaceDecl + source_file &File = 0 + scope &Scope + label_names []string + pos token.Position // function declaration position + cur_concrete_types []Type // current concrete types, e.g. } // break, continue diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 03ce3c33e5..ae80e92d5a 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -489,7 +489,7 @@ pub fn (mut c Checker) struct_init(mut struct_init ast.StructInit) ast.Type { // Make sure the first letter is capital, do not allow e.g. `x := string{}`, // but `x := T{}` is ok. if !c.is_builtin_mod && !c.inside_unsafe && type_sym.language == .v - && c.cur_fn.cur_generic_types.len == 0 { + && c.cur_fn.cur_concrete_types.len == 0 { pos := type_sym.name.last_index('.') or { -1 } first_letter := type_sym.name[pos + 1] if !first_letter.is_capital() { @@ -2732,7 +2732,7 @@ pub fn (mut c Checker) return_stmt(mut return_stmt ast.Return) { mut expected_types := [expected_type] if expected_type_sym.info is ast.MultiReturn { expected_types = expected_type_sym.info.types - if c.cur_fn.cur_generic_types.len > 0 { + if c.cur_fn.cur_concrete_types.len > 0 { expected_types = expected_types.map(c.unwrap_generic(it)) } } @@ -4185,7 +4185,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) { pub fn (mut c Checker) unwrap_generic(typ ast.Type) ast.Type { if typ.has_flag(.generic) { - if t_typ := c.table.resolve_generic_to_concrete(typ, c.cur_fn.generic_names, c.cur_fn.cur_generic_types, + if t_typ := c.table.resolve_generic_to_concrete(typ, c.cur_fn.generic_names, c.cur_fn.cur_concrete_types, false) { return t_typ @@ -6599,26 +6599,22 @@ fn (mut c Checker) post_process_generic_fns() { // Loop thru each generic function concrete type. // Check each specific fn instantiation. for i in 0 .. c.file.generic_fns.len { - if c.table.fn_generic_types.len == 0 { - // no concrete types, so just skip: - continue - } mut node := c.file.generic_fns[i] c.mod = node.mod - for generic_types in c.table.fn_generic_types[node.name] { - node.cur_generic_types = generic_types + for concrete_types in c.table.fn_generic_types[node.name] { + node.cur_concrete_types = concrete_types c.fn_decl(mut node) if node.name in ['vweb.run_app', 'vweb.run'] { - c.vweb_gen_types << generic_types + c.vweb_gen_types << concrete_types } } - node.cur_generic_types = [] + node.cur_concrete_types = [] } } fn (mut c Checker) fn_decl(mut node ast.FnDecl) { c.returns = false - if node.generic_names.len > 0 && node.cur_generic_types.len == 0 { + if node.generic_names.len > 0 && node.cur_concrete_types.len == 0 { // Just remember the generic function for now. // It will be processed later in c.post_process_generic_fns, // after all other normal functions are processed.