Skip to content

fix(csharp): new X() never binds to a method that shares X's name - #4428

Closed
nixvideojuego-ui wants to merge 1 commit into
Graphify-Labs:v8from
nixvideojuego-ui:fix/csharp-new-never-binds-a-method
Closed

nixvideojuego-ui wants to merge 1 commit into
Graphify-Labs:v8from
nixvideojuego-ui:fix/csharp-new-never-binds-a-method

Conversation

@nixvideojuego-ui

Copy link
Copy Markdown
Contributor

What does this PR do?

Fixes #4419.

A C# new X(...) was resolved against every definition named X, methods included:

  • Same file: static Bounds Bounds(..) => new Bounds(..) (Unity's Bounds, outside the corpus) became an EXTRACTED self-loop.
  • Across files: the bare-name pass bound every new List<T>() to an unrelated List() method, minting an INFERRED hub.

Changes:

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Tests or CI
  • Refactor
  • Security fix

Verification & Invariants

Invariants:

  • new X() constructs a type and never binds to a method.

  • A qualified construction is resolved by its qualifier, never by path proximity over the bare name.

  • Read the CONTRIBUTING.md guide.

  • Reproduced the issue and identified the invariant.

  • Made the smallest fix necessary.

  • Added a regression test: 5 tests in tests/test_csharp_object_creation.py. All 5 fail on v8 (e8eb76e) and pass here.

  • Kept the PR description synchronized with the final implementation.

  • Documented any limitations / unsupported cases explicitly.

Limitations:

  • A qualified construction with exactly one bare-name candidate binds as before, even when that candidate is not nested in the named outer type.
  • The outer-type match uses the qualifier's last segment only, so it does not check the outer type's own namespace.

On a 1,512-file Unity C# codebase, this branch alone changes the calls edges as follows:

  • 156 removed and 72 added;
  • self-loops go from 516 to 454.

Removed examples:

  • Unity's new WaitForSeconds() bound to UniTask's WaitForSeconds() method (41 false callers);
  • new Rect() bound to a Rect() UI helper (23);
  • new Stack<T>().

Added examples:

  • new SkipUntil<T>(..) inside the SkipUntil() factory now reaching the class;
  • new QuickSlotsBar.Slot reaching the right Slot.

The qualified constructions that were already right (new FarmWaves.Entry(..) and similar) are kept.

How was this tested?

uv run pytest tests/test_csharp_object_creation.py              # 21 passed
uv run pytest tests -k "csharp or cs_ or dotnet or razor or unity"   # 442 passed, 13 skipped
uv run pytest tests                                             # see note below
uv run ruff check graphify/extract.py graphify/extractors/engine.py tests/test_csharp_object_creation.py   # clean
uv run pyright graphify/extractors/engine.py graphify/extract.py      # no new diagnostics

Full suite: 7610 passed, 15 failed, 247 skipped. All 15 failures also fail on an untouched v8 checkout on the same machine (Windows 11, Python 3.14), so they come from the environment: test_skillgen ×11, test_hooks ×1, the two *_memoization tests and one test_extract checkout-path test.

Graphify-specific checklist

  • I updated generated skill artifacts (uv run python -m tools.skillgen --bless) when changing their source fragments. (N/A: no fragments touched)
  • I confirmed that AST/structural extraction remains deterministic (no ambient state dependencies like ENV variables).
  • I reviewed changes for security implications (no unsafe interpolation into shell/Python).
  • I confirmed that no API keys or local-only graph data are included.
  • (If applicable) I disclosed AI authorship in my commit messages.

🤖 Generated with Claude Code

A C# object creation was resolved against every definition named X, methods
included. Inside a helper like `static Bounds Bounds(..) => new Bounds(..)`
(Unity's Bounds, outside the corpus) it became an EXTRACTED self-loop; across
files the bare-name pass bound every `new List<T>()` in the corpus to an
unrelated `List()` method, minting an INFERRED hub with one false caller per
construction site.

The same-file path now defers a construction whose bare-name target is a
method, and the cross-file pass drops method candidates for `csharp_new`
calls, next to the enum-member/property exclusion (Graphify-Labs#3815, Graphify-Labs#4245). A real
type with the same name still wins.

Dropping those candidates removes ties that used to hide a separate guess: a
qualified construction (`new QuickSlotsBar.Slot()`) with several bare-name
`Slot` classes went to the path-proximity tie-break, which ignores the
qualifier and picks the Slot next to the caller. A qualified construction now
takes the one candidate nested in a type named by the qualifier's last
segment, or gets no edge; namespace qualifiers stay with
`_resolve_csharp_qualified_calls`.

Fixes Graphify-Labs#4419.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@github-actions

Copy link
Copy Markdown

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.

@graphify-labs graphify-labs Bot left a comment •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

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: extract (vacuous: never exercised), \_extract\_generic (unsupported).


Graphify review — findings

Stops C# new X() from resolving to a method that happens to share X's name. In-file resolution now defers instead of self-looping inside static Bounds Bounds(..) => new Bounds(..), and the cross-file pass drops method candidates, so new List<T>() no longer collapses onto some List() method as an INFERRED hub. A qualified construction like new Outer.Slot() now binds only to the single Slot nested in a type named Outer instead of taking a path-proximity guess, and gets no edge when no such outer type exists.

No blocking issues surfaced. 3 lower-confidence candidates did not survive cross-model review.

Analysis details — impact, health, verification

Impact & health

Graphify review

Impact — 3340 functions depend on the 665 functions this change touches.

Health — grade A; 10 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):

  • extract() — 945 callers, 57 callees (high)
  • _rebuild_code() — 177 callers, 59 callees (high)
  • _extract_generic() — 20 callers, 40 callees (high)
  • extract_js() — 91 callers, 7 callees (high)
  • main() — 110 callers, 3 callees (high)
  • extract_objc() — 25 callers, 13 callees (high)
  • extract_xaml() — 19 callers, 17 callees (high)
  • dispatch_command() — 2 callers, 132 callees (high)
  • …and 2 more

Verification — 3340 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: 3103 function(s) in the blast radius were not formally verified this run

Test selection

Test selection

189 of 412 test file(s) selected (46%) via static blast radius.

  • tests/test_affected_overrides.py — impact
  • tests/test_astro_extraction.py — impact
  • tests/test_astro_import_ids.py — impact
  • tests/test_blade_extractor.py — impact
  • tests/test_build.py — impact
  • tests/test_builtin_global_type_refs.py — impact
  • tests/test_c_include_resolution.py — impact
  • tests/test_cache.py — impact
  • tests/test_case_sensitive_resolution.py — impact
  • tests/test_cjs_module_extension.py — impact
  • tests/test_cobol_extractor.py — impact
  • tests/test_cpp_method_declarations.py — impact
  • tests/test_cpp_nested_and_cli.py — impact
  • tests/test_cpp_objc_cross_file_calls.py — impact
  • tests/test_cpp_template_type_params.py — impact
  • tests/test_cross_extension_reexport_self_cycle.py — impact
  • tests/test_cross_language_call_resolution.py — impact
  • tests/test_cross_repo_external_call_guards.py — impact
  • tests/test_cross_repo_member_calls.py — impact
  • tests/test_csharp_call_site_generic_args.py — impact
  • tests/test_csharp_enum_members.py — impact
  • tests/test_csharp_field_generic_args.py — impact
  • tests/test_csharp_generic_callsites.py — impact
  • tests/test_csharp_interface_dispatch.py — impact
  • tests/test_csharp_member_calls.py — impact
  • tests/test_csharp_member_chain_calls.py — impact
  • tests/test_csharp_member_nodes.py — impact
  • tests/test_csharp_object_creation.py — impact, changed-test
  • tests/test_csharp_partial_classes.py — impact
  • tests/test_csharp_tuple_type_refs.py — impact
  • tests/test_csharp_type_resolution.py — impact
  • tests/test_csharp_var_call_return_type.py — impact
  • tests/test_csharp_verbatim_interpolation.py — impact
  • tests/test_definition_file_portability.py — impact
  • tests/test_detect.py — impact
  • tests/test_dotnet.py — impact
  • tests/test_duplicate_annotation_edges.py — impact
  • tests/test_ejs_extractor.py — impact
  • tests/test_elixir_defdelegate.py — impact
  • tests/test_elixir_import_resolution.py — impact
  • tests/test_elixir_keyword_def_calls.py — impact
  • tests/test_elixir_qualified_calls.py — impact
  • tests/test_elixir_unqualified_call_scope.py — impact
  • tests/test_erlang_extractor.py — impact
  • tests/test_extract.py — impact
  • tests/test_extract_cache_location.py — impact
  • tests/test_extract_path_memo.py — impact
  • tests/test_extract_php_closures.py — impact
  • tests/test_file_label_disambiguation.py — impact
  • tests/test_file_node_id_spec.py — impact
  • … and 139 more

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 extract.

The verifier did not have enough to check extract, 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 90 sampled inputs raised on both versions — the function never executed, so 'no divergence' would be vacuous (mostly TypeError — 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

@nixvideojuego-ui

Copy link
Copy Markdown
Contributor Author

Ran the full suite on Python 3.10 and 3.13 as suggested (Windows 11, uv sync --frozen, no optional extras), next to an untouched v8 checkout (e8eb76e) on the same machine:

Python this branch untouched v8
3.10.21 7612 passed, 17 failed, 243 skipped 7607 passed, 17 failed, 243 skipped
3.13.15 7611 passed, 17 failed, 244 skipped 7606 passed, 17 failed, 244 skipped

The extra passes are this PR's 5 new tests. The 17 failures are the same tests on the branch and on untouched v8, on both versions, so they come from the Windows environment:

  • test_skillgen ×11 and test_skillgen_input_path_injection ×3;
  • the two *_memoization::test_absolute_path_lookup_skips_getcwd;
  • test_extract::test_c_include_out_of_root_target_id_is_deterministic_across_checkout_paths.

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.

safishamsi added a commit that referenced this pull request Oct 13, 2026
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]>
@safishamsi

Copy link
Copy Markdown
Member

Shipped in v0.9.90, just released to PyPI as graphifyy 0.9.90. Landed via an authorship-preserving cherry-pick (your commit keeps your name and counts on your contributor graph), which is why GitHub marks this closed rather than merged. Thanks @nixvideojuego-ui!

@safishamsi safishamsi closed this Oct 13, 2026
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]: C#: new X() binds to a method named X, and new Outer.X() is guessed by path proximity

2 participants