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 (#18715)
This commit is contained in:
parent
1f8e13a497
commit
36577eed09
@ -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 {
|
||||
@ -831,8 +830,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{
|
||||
@ -863,7 +872,9 @@ fn (mut c Checker) autocast_in_if_conds(mut right ast.Expr, from_expr ast.Expr,
|
||||
}
|
||||
}
|
||||
}
|
||||
if right.left !is ast.Ident {
|
||||
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)
|
||||
}
|
||||
|
32
vlib/v/tests/autocast_in_if_conds_4_test.v
Normal file
32
vlib/v/tests/autocast_in_if_conds_4_test.v
Normal 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
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user