Repository navigation
feat(csharp): type foreach loop variables for receiver resolution - #4423
nixvideojuego-ui wants to merge 1 commit into
Conversation
A C# `foreach` loop variable had no binding in the method-scoped receiver table, so a call on it (or a chain starting at it) never resolved across files: `foreach (var a in agents.Values) a.look.Animate();` produced no edge. The loop variable is now bound inside its loop: to its explicit type (`foreach (Agent a in xs)`), or for `var` to what a class field or property yields when iterated: the element of a `T[]` or of a sequence type, and a dictionary's value or key type for `f.Values` / `f.Keys`. Iterating a dictionary itself (KeyValuePair), nested collections, type parameters, and a loop over a name that a local or parameter shadows stay untyped. Closes Graphify-Labs#4420. Co-Authored-By: Claude Opus 5.5 <[email protected]>
|
Thanks for the pull request, @nixvideojuego-ui. A maintainer will review it soon. Want to talk it through while it is in review? Come join us on our Discord server. For longer-form discussion there is also GitHub Discussions. A couple of things that speed up review: make sure the test suite passes on Python 3.10 and 3.13, and that the change keeps extraction deterministic. |
There was a problem hiding this comment.
Graphify reviewed this change.
No issues found by static checks — no coupling regressions or blocking issues in the code graph. That is not the same as safe to merge: see what was not checked.
Not checked: tests were not run; formal verification proved 0 of 2 changed function(s) (2 not verified).
Formal verification. PR-changed functions: 0/2 verified (0 proven, 0 may-equivalent, 0 distinguished) · 2 not verified (1 vacuous, 1 unsupported).
Not verified on this run: \_csharp\_method\_receiver\_types (vacuous: never exercised), \_extract\_generic (unsupported).
Graphify review — findings
Binds C# foreach loop variables so member calls on them resolve across files: an explicitly typed variable takes its declared type, and a var takes the element type of the class field or property it iterates (f, this.f, f.Values, f.Keys). _csharp_loop_element_types records those element types for arrays, single-argument sequence types, and dictionary keys/values. Iterating a dictionary directly (which yields KeyValuePair), nested collections, type parameters, and fields shadowed by a local or parameter leave the variable untyped, so no edge is emitted rather than a guessed one.
No blocking issues surfaced. 2 lower-confidence candidates did not survive cross-model review.
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 917 functions depend on the 338 functions this change touches.
Health — this change adds coupling hotspots:
- worse:
walk()— 1 callers, 73 callees - worse:
_csharp_method_receiver_types()— 1 callers, 7 callees
Verification — 917 functions in the blast radius were not formally verified this run (proofs are advisory here).
Gate & verification
graphify gate
PASS — objectively clean (no health regressions, tests not run — proofs not run this pass (advisory)). Grounded, not self-assessed.
Advisory (not blocking):
- verification_scope: 806 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
37 of 413 test file(s) selected (9%) via static blast radius.
tests/test_astro_extraction.py— impacttests/test_build.py— impacttests/test_cjs_module_extension.py— impacttests/test_cpp_nested_and_cli.py— impacttests/test_csharp_foreach_loop_variable.py— impact, changed-testtests/test_csharp_partial_classes.py— impacttests/test_dotnet.py— impacttests/test_ejs_extractor.py— impacttests/test_extract.py— impacttests/test_extract_php_closures.py— impacttests/test_import_extension_resolution.py— impacttests/test_indirect_call_block_scoped_shadow.py— impacttests/test_indirect_dispatch.py— impacttests/test_indirect_dispatch_assign_return.py— impacttests/test_indirect_dispatch_getattr.py— impacttests/test_java_member_calls.py— impacttests/test_js_exported_scalar_bindings.py— impacttests/test_languages.py— impacttests/test_multilang.py— impacttests/test_php_unparsed_casts.py— impacttests/test_python_inherited_method_calls.py— impacttests/test_python_property_reads.py— impacttests/test_python_underscore_resolution.py— impacttests/test_rationale.py— impacttests/test_ruby_resolution.py— impacttests/test_scala_context_bounds.py— impacttests/test_scala_given_instances.py— impacttests/test_scala_self_type.py— impacttests/test_scala_top_level_binding.py— impacttests/test_scala_type_definition.py— impacttests/test_svelte_extraction.py— impacttests/test_swift_computed_properties.py— impacttests/test_swift_protocol_requirements.py— impacttests/test_trailing_newline_not_a_syntax_error.py— impacttests/test_ts_new_expression_calls.py— impacttests/test_typescript_module_extensions.py— impacttests/test_vue_extraction.py— impact
Selection is safe under the controlled-regression assumption; always-run tests + a periodic full run are the backstops. Advisory — it never changes the check verdict.
Formal verification
Could not verify: Could not verify \_csharp\_method\_receiver\_types.
The verifier did not have enough to check \_csharp\_method\_receiver\_types, so it is saying so rather than guessing. No false assurance is the whole point.
Guarantee: No guarantee either way, this is an honest abstention, not a pass.
Note: Reason: no capturable inputs from the test suite; property tier: not verifiable: all 233 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly AttributeError — names the real obstacle, not a sampling gap)
Could not verify: Could not verify \_extract\_generic.
The verifier did not have enough to check \_extract\_generic, so it is saying so rather than guessing. No false assurance is the whole point.
Guarantee: No guarantee either way, this is an honest abstention, not a pass.
Note: Reason: no capturable inputs from the test suite; property tier: parameter `config` is annotated `LanguageConfig` — outside the synthesizable primitive/collection set
· 2 more finding(s) on lines outside this diff (see the check run).
|
Ran the full suite on Python 3.10 and 3.13 as suggested (Windows 11,
The extra passes are this PR's 7 new tests. The 17 failures are the same tests on the branch and on untouched
On determinism: the change only reads the parsed tree and the per-class field tables already built during extraction. It does not depend on the environment, filesystem order or anything nondeterministic. |
C# nameless / `new X()` / `foreach` receiver resolution (#4422, #4428, #4423) and `_`/case-distinct node ids (#4416) change AST extraction output, so warm caches must re-extract. Co-Authored-By: Claude Opus 4.8 <[email protected]>
|
Shipped in v0.9.90, just released to PyPI as |
What does this PR do?
Closes #4420.
A C#
foreachloop variable had no binding in_csharp_method_receiver_types, so a call on it, or a chain starting at it, never resolved across files. For example,foreach (var a in agents.Values) a.look.Animate();produced no edge.Changes:
foreach_statementrange):foreach (Agent a in xs));var, to what the iterated class field or property yields.name[]element entries (C#: calls through a member chain, through element access, or inside a constructor produce no edge (follow-up to #3797) #4246), undername{},name.Values{}andname.Keys{}:T[]or a sequence type (List,IList,IReadOnlyList,IEnumerable,ICollection,IReadOnlyCollection,HashSet,ISet,SortedSet,Queue,Stack,LinkedList);f.Values/f.Keys.varloops are typed after the walk, once every local and parameter is bound. A loop over a name that a local or parameter shadows at that point stays untyped.Type of change
Verification & Invariants
Invariant: a loop variable is typed only from evidence written in source, never guessed. The evidence is its declared type or the declared type of the field or property it iterates. Everything else stays untyped, so the existing no-binding / no-edge behavior is unchanged there.
tests/test_csharp_foreach_loop_variable.py. The 4 positive tests fail onv8(e8eb76e) and pass here. The 3 boundary tests pass on both:Limitations: only class fields and properties are typed as the iterated source. A loop over a local, a parameter, a method result or a longer chain (
s.modules) stays untyped, as today.On a 1,512-file Unity C# codebase, this branch alone adds 58
callsedges and removes none. Examples:foreach (var e in _spawned) e.Refresh()→MailEntry.Refresh();entry.Deselect()→InventoryEntry.Deselect().How was this tested?
All 15 full-suite failures also fail on an untouched
v8checkout on the same machine (Windows 11, Python 3.14), so they come from the environment:test_skillgen×11,test_hooks×1, the two*_memoizationtests and onetest_extractcheckout-path test.This PR is independent of #4418 / #4419 and passes on its own. All three also pass together (452 C# tests).
Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no fragments touched)🤖 Generated with Claude Code