Skip to content

refactor: narrow ASTNode types to check_call and _synthesize_binary#1726

Open
acl-cqc wants to merge 3 commits into
mainfrom
acl/check_synth_types
Open

refactor: narrow ASTNode types to check_call and _synthesize_binary#1726
acl-cqc wants to merge 3 commits into
mainfrom
acl/check_synth_types

Conversation

@acl-cqc
Copy link
Copy Markdown
Contributor

@acl-cqc acl-cqc commented May 11, 2026

For check_call, a couple of cases here requires building a "fake" ast.Call node to represent the call, using with_loc to identify the same location.

For synthesize_call the problem is much worse: there are many more such non-ast.Call nodes (e.g. BinOp and Compare), and moreover there is

call_node, ret_ty = func.synthesize_call(arg_exprs, state.node, ctx)

where the AST location state.node can be a FunctionDef or a variety of other things. synthesize_call then calls into CustomCallChecker so I couldn't update that either....which is to say: I didn't really get to where I wanted, but it seemed worthwhile to keep even just this.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 11, 2026

🐰 Bencher Report

Branchacl/check_synth_types
TestbedLinux
Click to view all benchmark results
Benchmarkhugr_bytesBenchmark Result
bytes x 1e3
(Result Δ%)
Upper Boundary
bytes x 1e3
(Limit %)
hugr_nodesBenchmark Result
nodes
(Result Δ%)
Upper Boundary
nodes
(Limit %)
tests/benchmarks/test_big_array.py::test_big_array_compile📈 view plot
🚷 view threshold
158.77 x 1e3
(0.00%)Baseline: 158.77 x 1e3
160.36 x 1e3
(99.01%)
📈 view plot
🚷 view threshold
6,641.00
(0.00%)Baseline: 6,641.00
6,707.41
(99.01%)
tests/benchmarks/test_ctrl_flow.py::test_many_ctrl_flow_compile📈 view plot
🚷 view threshold
27.53 x 1e3
(0.00%)Baseline: 27.53 x 1e3
27.81 x 1e3
(99.01%)
📈 view plot
🚷 view threshold
1,074.00
(0.00%)Baseline: 1,074.00
1,084.74
(99.01%)
tests/benchmarks/test_queue_push_pop.py::test_queue_push_benchmark_compile📈 view plot
🚷 view threshold
10.91 x 1e3
(0.00%)Baseline: 10.91 x 1e3
11.02 x 1e3
(99.01%)
📈 view plot
🚷 view threshold
308.00
(0.00%)Baseline: 308.00
311.08
(99.01%)
tests/benchmarks/test_queue_push_pop.py::test_queue_push_pop_benchmark_compile📈 view plot
🚷 view threshold
14.84 x 1e3
(0.00%)Baseline: 14.84 x 1e3
14.99 x 1e3
(99.01%)
📈 view plot
🚷 view threshold
435.00
(0.00%)Baseline: 435.00
439.35
(99.01%)
🐰 View full continuous benchmarking report in Bencher

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 11, 2026

Codecov Report

❌ Patch coverage is 77.77778% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 93.50%. Comparing base (e4324d5) to head (758c98f).

Files with missing lines Patch % Lines
...s/src/guppylang_internals/std/_internal/checker.py 33.33% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1726      +/-   ##
==========================================
- Coverage   93.51%   93.50%   -0.01%     
==========================================
  Files         133      133              
  Lines       12767    12772       +5     
==========================================
+ Hits        11939    11943       +4     
- Misses        828      829       +1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 11, 2026

Merging this PR will not alter performance

✅ 9 untouched benchmarks


Comparing acl/check_synth_types (758c98f) with main (e4324d5)

Open in CodSpeed

@acl-cqc acl-cqc marked this pull request as ready for review May 11, 2026 20:17
@acl-cqc acl-cqc requested a review from a team as a code owner May 11, 2026 20:17
@acl-cqc acl-cqc requested a review from qartik May 11, 2026 20:17
@qartik qartik requested review from a team, Copilot and nicolaassolini-qntm and removed request for a team May 11, 2026 20:31
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR refactors internal type annotations to make CallableDef.check_call accept only ast.Call nodes (instead of a broader AstNode), and tightens the accepted node types for _synthesize_binary in the expression checker. To support the narrower check_call contract in a couple of internal non-call contexts, it introduces an ast_util.fake_call() helper and uses it to manufacture “call-like” nodes for error reporting/spans.

Changes:

  • Narrow CallableDef.check_call(..., node=...) from AstNode to ast.Call and update all implementers accordingly.
  • Narrow ExprSynthesizer._synthesize_binary(..., node=...) to ast.BinOp | ast.Compare.
  • Add fake_call() helper and use it in try_coerce_to() and to_sized_iter() to provide an ast.Call node to check_call.

Reviewed changes

Copilot reviewed 10 out of 10 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
guppylang-internals/src/guppylang_internals/std/_internal/checker.py Use a “fake” ast.Call node when calling check_call for __new__ in to_sized_iter.
guppylang-internals/src/guppylang_internals/definition/value.py Narrow abstract CallableDef.check_call node parameter to ast.Call.
guppylang-internals/src/guppylang_internals/definition/traced.py Update check_call override signature to node: ast.Call.
guppylang-internals/src/guppylang_internals/definition/pytket_circuits.py Update check_call override signature to node: ast.Call.
guppylang-internals/src/guppylang_internals/definition/overloaded.py Update check_call override signature to node: ast.Call.
guppylang-internals/src/guppylang_internals/definition/function.py Update check_call override signature to node: ast.Call.
guppylang-internals/src/guppylang_internals/definition/declaration.py Update check_call override signature to node: ast.Call.
guppylang-internals/src/guppylang_internals/definition/custom.py Update check_call override signature to node: ast.Call.
guppylang-internals/src/guppylang_internals/checker/expr_checker.py Narrow _synthesize_binary node type; create a “fake” call node for numeric coercions before check_call.
guppylang-internals/src/guppylang_internals/ast_util.py Add fake_call() helper for manufacturing ast.Call nodes.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.


def fake_call(name: str, args: list[ast.expr]) -> ast.Call:
"""Creates a fake call node with the given name and arguments."""
return ast.Call(func=ast.Name(id=name, ctx=ast.Load()), args=args, keywords=[])
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

All the time we call fake_call, we wrap the result with with_loc, thus this comment is not right. However, since we always use with_loc, maybe can be useful to include the with_loc call in fake_call directly

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I was wrong in the first comment. The issue underlined by copilot is right, however, I'm not sure if exceptions regarding node.func (like check_num_args) are possible on a fake_call function. @acl-cqc any thoughts?

@nicolaassolini-qntm
Copy link
Copy Markdown
Contributor

@acl-cqc, I think the PR is fine, but I'm missing the reason why these changes are needed in the first place

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.

5 participants