From 7d0cba5e966530e7e62139407182604b5e4dc2e6 Mon Sep 17 00:00:00 2001 From: spaceface Date: Thu, 11 Mar 2021 13:50:02 +0100 Subject: [PATCH] v.ast: remove the `stmt.position()` method (#9233) --- vlib/v/ast/ast.v | 24 +++--------------------- vlib/v/checker/checker.v | 12 +++++------- vlib/v/doc/doc.v | 2 +- vlib/v/fmt/fmt.v | 2 +- vlib/v/gen/c/cgen.v | 6 +++--- vlib/v/parser/if_match.v | 2 +- 6 files changed, 14 insertions(+), 34 deletions(-) diff --git a/vlib/v/ast/ast.v b/vlib/v/ast/ast.v index 8c74d1615a..8ba67c90db 100644 --- a/vlib/v/ast/ast.v +++ b/vlib/v/ast/ast.v @@ -1356,28 +1356,10 @@ pub: is_ptr bool // whether the type is a pointer } -pub fn (stmt Stmt) position() token.Position { - match stmt { - AssertStmt, AssignStmt, Block, BranchStmt, CompFor, ConstDecl, DeferStmt, EnumDecl, ExprStmt, - FnDecl, ForCStmt, ForInStmt, ForStmt, GotoLabel, GotoStmt, Import, Return, StructDecl, - GlobalDecl, HashStmt, InterfaceDecl, Module, SqlStmt, GoStmt { - return stmt.pos - } - TypeDecl { - match stmt { - AliasTypeDecl, FnTypeDecl, SumTypeDecl { return stmt.pos } - } - } - // Please, do NOT use else{} here. - // This match is exhaustive *on purpose*, to help force - // maintaining/implementing proper .pos fields. - } -} - pub fn (node Node) position() token.Position { match node { Stmt { - mut pos := node.position() + mut pos := node.pos if node is Import { for sym in node.syms { pos = pos.extend(sym.pos) @@ -1406,8 +1388,8 @@ pub fn (node Node) position() token.Position { File { mut pos := token.Position{} if node.stmts.len > 0 { - first_pos := node.stmts.first().position() - last_pos := node.stmts.last().position() + first_pos := node.stmts.first().pos + last_pos := node.stmts.last().pos pos = first_pos.extend_with_last_line(last_pos, last_pos.line_nr) } return pos diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index 0d30a12c05..6f293f72b0 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3160,7 +3160,7 @@ fn (mut c Checker) check_loop_label(label string, pos token.Position) { fn (mut c Checker) stmt(node ast.Stmt) { $if trace_checker ? { - stmt_pos := node.position() + stmt_pos := node.pos eprintln('checking file: ${c.file.path:-30} | stmt pos: ${stmt_pos.str():-45} | stmt') } // c.expected_type = table.void_type @@ -3602,7 +3602,7 @@ fn (mut c Checker) stmts(stmts []ast.Stmt) { for stmt in stmts { if c.scope_returns { if unreachable.line_nr == -1 { - unreachable = stmt.position() + unreachable = stmt.pos } } c.stmt(stmt) @@ -4410,9 +4410,7 @@ pub fn (mut c Checker) match_expr(mut node ast.MatchExpr) table.Type { // probably any mismatch will be caught by not producing a value instead for st in branch.stmts[0..branch.stmts.len - 1] { // must not contain C statements - st.check_c_expr() or { - c.error('`match` expression branch has $err', st.position()) - } + st.check_c_expr() or { c.error('`match` expression branch has $err', st.pos) } } } // If the last statement is an expression, return its type @@ -4791,7 +4789,7 @@ pub fn (mut c Checker) select_expr(mut node ast.SelectExpr) table.Type { } else { if !branch.is_else { - c.error('receive or send statement expected as `select` key', branch.stmt.position()) + c.error('receive or send statement expected as `select` key', branch.stmt.pos) } } } @@ -5031,7 +5029,7 @@ pub fn (mut c Checker) if_expr(mut node ast.IfExpr) table.Type { } for st in branch.stmts { // must not contain C statements - st.check_c_expr() or { c.error('`if` expression branch has $err', st.position()) } + st.check_c_expr() or { c.error('`if` expression branch has $err', st.pos) } } } // Also check for returns inside a comp.if's statements, even if its contents aren't parsed diff --git a/vlib/v/doc/doc.v b/vlib/v/doc/doc.v index fc3d55e300..29983234c5 100644 --- a/vlib/v/doc/doc.v +++ b/vlib/v/doc/doc.v @@ -130,7 +130,7 @@ pub fn (mut d Doc) stmt(stmt ast.Stmt, filename string) ?DocNode { mut node := DocNode{ name: d.stmt_name(stmt) content: d.stmt_signature(stmt) - pos: d.convert_pos(filename, stmt.position()) + pos: d.convert_pos(filename, stmt.pos) file_path: os.join_path(d.base_path, filename) is_pub: d.stmt_pub(stmt) } diff --git a/vlib/v/fmt/fmt.v b/vlib/v/fmt/fmt.v index 309979af4e..a76b103951 100644 --- a/vlib/v/fmt/fmt.v +++ b/vlib/v/fmt/fmt.v @@ -378,7 +378,7 @@ pub fn (mut f Fmt) stmt_str(node ast.Stmt) string { pub fn (mut f Fmt) stmt(node ast.Stmt) { if f.is_debug { - eprintln('stmt: ${node.position():-42} | node: ${node.type_name():-20}') + eprintln('stmt: ${node.pos:-42} | node: ${node.type_name():-20}') } match node { ast.AssignStmt { diff --git a/vlib/v/gen/c/cgen.v b/vlib/v/gen/c/cgen.v index 27ddefc382..433d53f304 100644 --- a/vlib/v/gen/c/cgen.v +++ b/vlib/v/gen/c/cgen.v @@ -956,11 +956,11 @@ fn (mut g Gen) stmts_with_tmp_var(stmts []ast.Stmt, tmp_var string) { // stmt := stmts[stmts.len-1] if stmt !is ast.FnDecl && g.inside_ternary == 0 { // g.writeln('// autofree scope') - // g.writeln('// autofree_scope_vars($stmt.position().pos) | ${typeof(stmt)}') + // g.writeln('// autofree_scope_vars($stmt.pos.pos) | ${typeof(stmt)}') // go back 1 position is important so we dont get the // internal scope of for loops and possibly other nodes - // g.autofree_scope_vars(stmt.position().pos - 1) - mut stmt_pos := stmt.position() + // g.autofree_scope_vars(stmt.pos.pos - 1) + mut stmt_pos := stmt.pos if stmt_pos.pos == 0 { // Do not autofree if the position is 0, since the correct scope won't be found. // Report a bug, since position shouldn't be 0 for most nodes. diff --git a/vlib/v/parser/if_match.v b/vlib/v/parser/if_match.v index 89c3dd12f0..9a5b579129 100644 --- a/vlib/v/parser/if_match.v +++ b/vlib/v/parser/if_match.v @@ -384,7 +384,7 @@ fn (mut p Parser) select_expr() ast.SelectExpr { } } else { - p.error_with_pos('select: transmission statement expected', stmt.position()) + p.error_with_pos('select: transmission statement expected', stmt.pos) return ast.SelectExpr{} } }