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 condtions (#18699)

This commit is contained in:
yuyi 2023-06-28 18:37:20 +08:00 committed by GitHub
parent 735654296c
commit 2b2aca6eb7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 46 additions and 0 deletions

View File

@ -824,6 +824,20 @@ fn (mut c Checker) autocast_in_if_conds(mut right ast.Expr, from_expr ast.Expr,
expr_type: from_type
}
}
} else {
c.autocast_in_if_conds(mut right.expr, from_expr, from_type, to_type)
}
}
ast.ParExpr {
c.autocast_in_if_conds(mut right.expr, from_expr, from_type, to_type)
}
ast.PrefixExpr {
c.autocast_in_if_conds(mut right.right, from_expr, from_type, to_type)
}
ast.CallExpr {
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)
}
}
ast.InfixExpr {

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(name string) string {
return 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.info.name) == 'foo' {
println('ok')
assert true
} else {
assert false
}
}