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

checker: fix autocast in complex if conditions 3 (#18710)

This commit is contained in:
yuyi 2023-06-30 02:31:40 +08:00 committed by GitHub
parent c4ba47a131
commit e74723c1e7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 3 deletions

View File

@ -24,7 +24,6 @@ fn (mut c Checker) infix_expr(mut node ast.InfixExpr) ast.Type {
to_type := c.expr(left_node.right.right)
c.autocast_in_if_conds(mut node.right, left_node.right.left, from_type,
to_type)
break
}
}
if left_node.op == .key_is {
@ -823,8 +822,18 @@ fn (mut c Checker) invalid_operator_error(op token.Kind, left_type ast.Type, rig
}
// `if node is ast.Ident && node.is_mut { ... }` -> `if node is ast.Ident && (node as ast.Ident).is_mut { ... }`
fn (mut c Checker) autocast_in_if_conds(mut right ast.Expr, from_expr ast.Expr, from_type ast.Type, to_type ast.Type) {
fn (mut c Checker) autocast_in_if_conds(mut right_ ast.Expr, from_expr ast.Expr, from_type ast.Type, to_type ast.Type) {
mut right := right_
match mut right {
ast.Ident {
if right.name == from_expr.str() {
right_ = ast.AsCast{
typ: to_type
expr: from_expr
expr_type: from_type
}
}
}
ast.SelectorExpr {
if right.expr.str() == from_expr.str() {
right.expr = ast.ParExpr{
@ -855,7 +864,6 @@ fn (mut c Checker) autocast_in_if_conds(mut right ast.Expr, from_expr ast.Expr,
}
}
}
c.autocast_in_if_conds(mut right.left, from_expr, from_type, to_type)
for mut arg in right.args {
c.autocast_in_if_conds(mut arg.expr, from_expr, from_type, to_type)
}

View File

@ -0,0 +1,32 @@
type MySumType = S1 | S2
struct Info {
name string
}
struct S1 {
is_info bool
info Info
}
struct S2 {
field2 string
}
fn get_name(s1 S1) string {
return s1.info.name
}
fn test_autocast_in_if_conds() {
s := MySumType(S1{
is_info: false
info: Info{'foo'}
})
if s is S1 && !s.is_info && get_name(s) == 'foo' {
println('ok')
assert true
} else {
assert false
}
}