checker: simplify for smartcast in for_stmt() (#13619)

pull/13623/head
yuyi 2022-03-01 18:31:48 +08:00 committed by GitHub
parent 3b6e122d9d
commit 996bd41ce8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 5 additions and 15 deletions

View File

@ -150,25 +150,15 @@ fn (mut c Checker) for_stmt(mut node ast.ForStmt) {
c.error('non-bool used as for condition', node.pos) c.error('non-bool used as for condition', node.pos)
} }
if mut node.cond is ast.InfixExpr { if mut node.cond is ast.InfixExpr {
infix := node.cond if node.cond.op == .key_is {
if infix.op == .key_is { if node.cond.right is ast.TypeNode && node.cond.left in [ast.Ident, ast.SelectorExpr] {
if infix.right is ast.TypeNode && infix.left in [ast.Ident, ast.SelectorExpr] { if c.table.type_kind(node.cond.left_type) in [.sum_type, .interface_] {
is_variable := if mut infix.left is ast.Ident { c.smartcast(node.cond.left, node.cond.left_type, node.cond.right_type, mut
infix.left.kind == .variable
} else {
true
}
left_type := c.expr(infix.left)
left_sym := c.table.sym(left_type)
if is_variable {
if left_sym.kind in [.sum_type, .interface_] {
c.smartcast(infix.left, infix.left_type, infix.right.typ, mut
node.scope) node.scope)
} }
} }
} }
} }
}
// TODO: update loop var type // TODO: update loop var type
// how does this work currenly? // how does this work currenly?
c.check_loop_label(node.label, node.pos) c.check_loop_label(node.label, node.pos)