From 288b13b51d89fcc5489c38d504ddf88295d8e973 Mon Sep 17 00:00:00 2001 From: yuyi Date: Thu, 3 Mar 2022 16:48:31 +0800 Subject: [PATCH] all: cleanup smartcasts in the compiler (#13639) --- vlib/v/checker/checker.v | 9 +++------ vlib/v/checker/for.v | 7 +++---- vlib/v/gen/c/sql.v | 6 ++---- vlib/v/parser/assign.v | 9 ++++----- vlib/v/parser/sql.v | 15 +++++++-------- vlib/v/transformer/transformer.v | 8 ++++---- 6 files changed, 23 insertions(+), 31 deletions(-) diff --git a/vlib/v/checker/checker.v b/vlib/v/checker/checker.v index c72fa5d8a3..cf17c1eeba 100644 --- a/vlib/v/checker/checker.v +++ b/vlib/v/checker/checker.v @@ -3544,12 +3544,9 @@ pub fn (mut c Checker) prefix_expr(mut node ast.PrefixExpr) ast.Type { for mut expr is ast.ParExpr { expr = expr.expr } - match expr { - ast.BoolLiteral, ast.CallExpr, ast.CharLiteral, ast.FloatLiteral, ast.IntegerLiteral, - ast.InfixExpr, ast.StringLiteral, ast.StringInterLiteral { - c.error('cannot take the address of $expr', node.pos) - } - else {} + if expr in [ast.BoolLiteral, ast.CallExpr, ast.CharLiteral, ast.FloatLiteral, + ast.IntegerLiteral, ast.InfixExpr, ast.StringLiteral, ast.StringInterLiteral] { + c.error('cannot take the address of $expr', node.pos) } if mut node.right is ast.IndexExpr { typ_sym := c.table.sym(node.right.left_type) diff --git a/vlib/v/checker/for.v b/vlib/v/checker/for.v index d4e7993a36..f9cc7a7fe7 100644 --- a/vlib/v/checker/for.v +++ b/vlib/v/checker/for.v @@ -103,10 +103,9 @@ fn (mut c Checker) for_in_stmt(mut node ast.ForInStmt) { value_type = value_type.ref() match node.cond { ast.Ident { - if node.cond.obj is ast.Var { - obj := node.cond.obj as ast.Var - if !obj.is_mut { - c.error('`$obj.name` is immutable, it cannot be changed', + if mut node.cond.obj is ast.Var { + if !node.cond.obj.is_mut { + c.error('`$node.cond.obj.name` is immutable, it cannot be changed', node.cond.pos) } } diff --git a/vlib/v/gen/c/sql.v b/vlib/v/gen/c/sql.v index cb67bb02ae..9b82d343a4 100644 --- a/vlib/v/gen/c/sql.v +++ b/vlib/v/gen/c/sql.v @@ -684,10 +684,8 @@ fn (mut g Gen) sql_select(node ast.SqlExpr, expr string, left string) { name := sel s := g.table.find_type_idx('orm.Primitive') if s != 0 { - if ident.info is ast.IdentVar { - mut info := ident.info as ast.IdentVar - info.typ = s - ident.info = info + if mut ident.info is ast.IdentVar { + ident.info.typ = s } } ident.name = name diff --git a/vlib/v/parser/assign.v b/vlib/v/parser/assign.v index 9d10b4763c..2daf97a25b 100644 --- a/vlib/v/parser/assign.v +++ b/vlib/v/parser/assign.v @@ -159,10 +159,9 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme return p.error_with_pos('redefinition of `$lx.name`', lx.pos) } mut share := ast.ShareType(0) - if lx.info is ast.IdentVar { - iv := lx.info as ast.IdentVar - share = iv.share - if iv.is_static { + if mut lx.info is ast.IdentVar { + share = lx.info.share + if lx.info.is_static { if !p.pref.translated && !p.is_translated && !p.pref.is_fmt && !p.inside_unsafe_fn { return p.error_with_pos('static variables are supported only in translated mode or in [unsafe] fn', @@ -170,7 +169,7 @@ fn (mut p Parser) partial_assign_stmt(left []ast.Expr, left_comments []ast.Comme } is_static = true } - if iv.is_volatile { + if lx.info.is_volatile { is_volatile = true } } diff --git a/vlib/v/parser/sql.v b/vlib/v/parser/sql.v index 307c4076d6..da9f2f0b4a 100644 --- a/vlib/v/parser/sql.v +++ b/vlib/v/parser/sql.v @@ -31,17 +31,16 @@ fn (mut p Parser) sql_expr() ast.Expr { p.next() where_expr = p.expr(0) // `id == x` means that a single object is returned - if !is_count && where_expr is ast.InfixExpr { - e := where_expr as ast.InfixExpr - if e.op == .eq && e.left is ast.Ident { - if e.left.name == 'id' { + if !is_count && mut where_expr is ast.InfixExpr { + if where_expr.op == .eq && mut where_expr.left is ast.Ident { + if where_expr.left.name == 'id' { query_one = true } } - if e.right is ast.Ident { - if !p.scope.known_var(e.right.name) { - p.check_undefined_variables([e.left], e.right) or { - return p.error_with_pos(err.msg(), e.right.pos) + if mut where_expr.right is ast.Ident { + if !p.scope.known_var(where_expr.right.name) { + p.check_undefined_variables([where_expr.left], where_expr.right) or { + return p.error_with_pos(err.msg(), where_expr.right.pos) } } } diff --git a/vlib/v/transformer/transformer.v b/vlib/v/transformer/transformer.v index d1761b0c6c..c7032c62f0 100644 --- a/vlib/v/transformer/transformer.v +++ b/vlib/v/transformer/transformer.v @@ -935,8 +935,8 @@ pub fn (mut t Transformer) if_expr(mut node ast.IfExpr) ast.Expr { stmt = t.stmt(mut stmt) if i == branch.stmts.len - 1 { - if stmt is ast.ExprStmt { - expr := (stmt as ast.ExprStmt).expr + if mut stmt is ast.ExprStmt { + expr := stmt.expr match expr { ast.IfExpr { @@ -976,8 +976,8 @@ pub fn (mut t Transformer) match_expr(mut node ast.MatchExpr) ast.Expr { stmt = t.stmt(mut stmt) if i == branch.stmts.len - 1 { - if stmt is ast.ExprStmt { - expr := (stmt as ast.ExprStmt).expr + if mut stmt is ast.ExprStmt { + expr := stmt.expr match expr { ast.IfExpr {