Skip to content

fix(unroll): emit extern call statements as statements - #498

Open
tonycoder-hub wants to merge 1 commit into
qBraid:mainfrom
tonycoder-hub:gb/fix-extern-call-semicolon
Open

tonycoder-hub wants to merge 1 commit into
qBraid:mainfrom
tonycoder-hub:gb/fix-extern-call-semicolon

Conversation

@tonycoder-hub

Copy link
Copy Markdown
Contributor

unroll() turns an extern call used as a statement into broken output. Two problems, same path:

  • _visit_expression_statement in src/pyqasm/visitor.py returned the statements from evaluate_expression unchanged. For an extern call, that list holds the bare FunctionCall expression that _visit_function_call appends (visitor.py:2796-2803), so openqasm3.printer renders it as an expression, with no ; and no line break (my_extern(3)x q[0];).
  • _visit_function_call sets _in_extern_function, and only the declaration/assignment paths clear it (_handle_extern_function_cleanup, visitor.py:624). After my_extern(3); the flag stayed set. The next declaration was then emitted as written (for example int i = 0; in an unrolled for). In int[32] y = g(q[0]);, where g calls an extern, the inlined body of g was dropped and int[32] y = g(q[0]); was emitted against a subroutine that no longer exists.

Fix (visitor.py:3641): wrap expression nodes in ExpressionStatement (carrying the statement's span), and restore the enclosing _in_extern_function value after an expression statement. Restoring rather than clearing keeps declarations such as int[32] y = my_extern(3) + g(); intact when g contains a call statement.

Tests in tests/qasm3/subroutines/test_subroutines.py:

  • test_extern_function_call_statement_round_trips and test_extern_function_call_statement_in_subroutine check the unrolled output and that openqasm3.parse(dumps(m)) succeeds. Both fail on main (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

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-eye

argus-eye Bot commented Nov 30, 2026

Copy link
Copy Markdown

Argus review

Auto-review is off for this repo. Tick the box below to run a review on this PR.

  • Trigger Argus review

Estimated cost

  • Files changed: 3
  • Diff lines (±): 129
  • Historical avg: ~337.1k tokens · ~$1.17 · across last 18 review(s)

Tip: you can also comment @argus-eye review at any time.

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

This branch has not been deployed

No deployments
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.

[BUG] dumps() emits extern calls without a semicolon, producing unparsable QASM

2 participants