Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

checker: perfect if expr with compound conditions #22541

Merged
merged 1 commit into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions vlib/v/checker/infix.v
Original file line number Diff line number Diff line change
Expand Up @@ -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 {}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
15 changes: 15 additions & 0 deletions vlib/v/tests/conditions/ifs/if_expr_with_compound_conds_2_test.v
Original file line number Diff line number Diff line change
@@ -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()
}
14 changes: 14 additions & 0 deletions vlib/v/tests/conditions/ifs/if_expr_with_compound_conds_3_test.v
Original file line number Diff line number Diff line change
@@ -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()
}
15 changes: 15 additions & 0 deletions vlib/v/tests/conditions/ifs/if_expr_with_compound_conds_4_test.v
Original file line number Diff line number Diff line change
@@ -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()
}
Loading