checker: improve if expr with compound conditions (#22541)

This commit is contained in:
yuyi 2024-10-16 17:48:05 +08:00 committed by GitHub
parent ffa79219f6
commit a2e478be81
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 64 additions and 2 deletions

View File

@ -1054,6 +1054,19 @@ fn (mut c Checker) autocast_in_if_conds(mut right ast.Expr, from_expr ast.Expr,
}
ast.IndexExpr {
c.autocast_in_if_conds(mut right.left, from_expr, from_type, to_type)
c.autocast_in_if_conds(mut right.index, from_expr, from_type, to_type)
}
ast.RangeExpr {
c.autocast_in_if_conds(mut right.low, from_expr, from_type, to_type)
c.autocast_in_if_conds(mut right.high, from_expr, from_type, to_type)
}
ast.StringInterLiteral {
for mut expr in right.exprs {
c.autocast_in_if_conds(mut expr, from_expr, from_type, to_type)
}
}
ast.UnsafeExpr {
c.autocast_in_if_conds(mut right.expr, from_expr, from_type, to_type)
}
else {}
}

View File

@ -1,9 +1,14 @@
type Foo = int | []int
fn test_if_expr_with_compound_conds() {
fn works() bool {
a := Foo([3])
if a is []int && a[0] == 3 {
println('works')
return true
}
assert true
return false
}
fn test_if_expr_with_compound_conds() {
assert works()
}

View File

@ -0,0 +1,15 @@
type Foo = int | []int
fn works() bool {
arr := [1, 2, 3]
a := Foo(2)
if a is int && arr[a] == 3 {
println('works')
return true
}
return false
}
fn test_if_expr_with_compound_conds() {
assert works()
}

View File

@ -0,0 +1,14 @@
type Foo = int | []int
fn works() bool {
a := Foo(2)
if a is int && '${a}' == '2' {
println('works')
return true
}
return false
}
fn test_if_expr_with_compound_conds() {
assert works()
}

View File

@ -0,0 +1,15 @@
type Foo = int | []int
fn works() bool {
arr := [1, 2, 3]
a := Foo(2)
if a is int && unsafe { arr[..a] == [1, 2] } {
println('works')
return true
}
return false
}
fn test_if_expr_with_compound_conds() {
assert works()
}