all: cleanup smartcasts in the compiler (#13639)

pull/13643/head
yuyi 2022-03-03 16:48:31 +08:00 committed by GitHub
parent 4e7db5bab0
commit 288b13b51d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 31 deletions

View File

@ -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)

View File

@ -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)
}
}

View File

@ -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

View File

@ -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
}
}

View File

@ -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)
}
}
}

View File

@ -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 {