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

Fix bug in python if/else chains #1183

Merged
merged 1 commit into from Mar 23, 2024
Merged

Conversation

johnynek
Copy link
Owner

close #1181

This bug has been lurking in python code generation for some time, but only with generative testing did we push hard enough to find it.

expr match {
case If(c1, t1, e1) =>
val (ifs, e2) = combine(e1)
(((c1, t1)) :: ifs, e2)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

previously this code was in PythonGen, but it really only depends on Matchless.Expr. The order was being reversed by appending to the tail: ifs :+ ((c1, t1)) which for some branches changes semantics (where Matchless has leveraged prior information to do less work).

expr match {
case If(c1, t1, e1) =>
val (ifs, e2) = combine(e1)
(ifs :+ ((c1, t1)), e2)
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note the order is wrong here: we are pushing c1, t1 on to the end of ifs but that came from expanding e1 so it should come before e1 since it was before originally.

@@ -151,4 +156,21 @@ class MatchlessTest extends AnyFunSuite {
assert(matchlessRes == matchRes)
}
}

test("If.flatten can be unflattened") {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test didn't always find the bug (since randomly finding a case is somewhat rare, but after 3 runs, one found it, so I think it would be flakey enough that we would discover the issue).

@@ -71,6 +67,10 @@ def cmp_BinNat(a: BinNat, b: BinNat) -> Comparison:
case GT | EQ: GT
case LT: LT
case Zero: GT
case Zero:
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is more efficient since Zero is rare, but triggered the bug before this fix.

@codecov-commenter
Copy link

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.68%. Comparing base (04a0e87) to head (0c0d723).
Report is 15 commits behind head on main.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1183      +/-   ##
==========================================
+ Coverage   91.32%   91.68%   +0.36%     
==========================================
  Files          96       96              
  Lines       11846    12011     +165     
  Branches     2675     2819     +144     
==========================================
+ Hits        10818    11012     +194     
+ Misses       1028      999      -29     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@johnynek johnynek merged commit 0bb465d into main Mar 23, 2024
9 checks passed
@johnynek johnynek deleted the oscar/20240323_fix_python_if_chain branch March 23, 2024 19:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

python code generation has a bug
2 participants