1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

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

This commit is contained in:
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

View File

@ -150,21 +150,11 @@ fn (mut c Checker) for_stmt(mut node ast.ForStmt) {
c.error('non-bool used as for condition', node.pos)
}
if mut node.cond is ast.InfixExpr {
infix := node.cond
if infix.op == .key_is {
if infix.right is ast.TypeNode && infix.left in [ast.Ident, ast.SelectorExpr] {
is_variable := if mut infix.left is ast.Ident {
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)
}
if node.cond.op == .key_is {
if node.cond.right is ast.TypeNode && node.cond.left in [ast.Ident, ast.SelectorExpr] {
if c.table.type_kind(node.cond.left_type) in [.sum_type, .interface_] {
c.smartcast(node.cond.left, node.cond.left_type, node.cond.right_type, mut
node.scope)
}
}
}