Repository navigation
fix(unroll): emit extern call statements as statements - #498
Open
tonycoder-hub wants to merge 1 commit into
Open
tonycoder-hub wants to merge 1 commit into
tonycoder-hub wants to merge 1 commit into
Conversation
An extern call used as a statement reached the unrolled AST as a bare FunctionCall expression, so dumps() printed it without a semicolon and the output did not reparse. The expression-statement path also left _in_extern_function set, so the next declaration was emitted as written and a subroutine body inlined into a declaration was dropped. Wrap the call in ExpressionStatement, and restore the enclosing _in_extern_function value after evaluating an expression statement, so a declaration that itself contains an extern call is still kept. Fixes qBraid#438 Signed-off-by: Tony Coder <407243179@qq.com>
Argus reviewAuto-review is off for this repo. Tick the box below to run a review on this PR.
Estimated cost
Tip: you can also comment |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
unroll()turns an extern call used as a statement into broken output. Two problems, same path:_visit_expression_statementinsrc/pyqasm/visitor.pyreturned the statements fromevaluate_expressionunchanged. For an extern call, that list holds the bareFunctionCallexpression that_visit_function_callappends (visitor.py:2796-2803), soopenqasm3.printerrenders it as an expression, with no;and no line break (my_extern(3)x q[0];)._visit_function_callsets_in_extern_function, and only the declaration/assignment paths clear it (_handle_extern_function_cleanup,visitor.py:624). Aftermy_extern(3);the flag stayed set. The next declaration was then emitted as written (for exampleint i = 0;in an unrolledfor). Inint[32] y = g(q[0]);, wheregcalls an extern, the inlined body ofgwas dropped andint[32] y = g(q[0]);was emitted against a subroutine that no longer exists.Fix (
visitor.py:3641): wrap expression nodes inExpressionStatement(carrying the statement's span), and restore the enclosing_in_extern_functionvalue after an expression statement. Restoring rather than clearing keeps declarations such asint[32] y = my_extern(3) + g();intact whengcontains a call statement.Tests in
tests/qasm3/subroutines/test_subroutines.py:test_extern_function_call_statement_round_tripsandtest_extern_function_call_statement_in_subroutinecheck the unrolled output and thatopenqasm3.parse(dumps(m))succeeds. Both fail onmain(95747c7) and pass with this change.test_statement_call_keeps_enclosing_extern_declaration(2 cases) checks that a call statement inside a subroutine does not drop an enclosing extern declaration.Fixes #438