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 4 (#18744)

This commit is contained in:
yuyi 2023-07-03 04:54:17 +08:00 committed by GitHub
parent c75382ad23
commit fd6983fcb4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 55 additions and 11 deletions

View File

@ -830,18 +830,17 @@ 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) {
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
}
}
fn (mut c Checker) autocast_in_if_conds(mut right ast.Expr, from_expr ast.Expr, from_type ast.Type, to_type ast.Type) {
if '${right}' == from_expr.str() {
right = ast.AsCast{
typ: to_type
expr: from_expr
expr_type: from_type
}
return
}
match mut right {
ast.SelectorExpr {
if right.expr.str() == from_expr.str() {
right.expr = ast.ParExpr{

View File

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