Repository navigation
fix(go): link a method value reached through a receiver field (#1566) - #4931
Conversation
…fy-Labs#1566) 61e864a records a method value passed as an argument (`routing.Wrap(s.Health)`) as an indirect raw call, but only when its operand is a plain identifier. A handler held as a field of the receiver, `m.UseMiddleware(hs.ContextHandler.Middleware)`, has a selector operand, so it was skipped and the method got no edge. The argument walk now also accepts `self.field.M` when `self` is the method's own receiver, and records it the way the direct-call branch records `self.field.M()`: the field as `member_receiver`, the receiver's type as `field_owner`. _resolve_go_member_calls then types it through the caller's table or the owner struct's fields in a sibling file (Graphify-Labs#3368) and emits the INFERRED `indirect_call`. A field whose type is declared nowhere links and parks nothing, as before.
|
Thanks for the pull request, @nothariharan. 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 1 changed function(s) (1 sampled, not proven).
Formal verification. PR-changed functions: 1/1 verified (0 proven, 1 may-equivalent, 0 distinguished) · 0 not verified.
Graphify review — findings
Extends Go method-value indirect-call detection to cover values reached through a field of the method's own receiver, e.g. m.UseMiddleware(hs.ContextHandler.Middleware). The field's declared type now types the receiver, the same way a direct hs.ContextHandler.Middleware() call already does, so Middleware gets an INFERRED indirect_call edge from the caller instead of no edge at all. Only self.field.Method is handled. Selectors rooted at any other identifier are skipped, and the raw call carries a new field_owner so the resolver can look up the field type, including one declared in a sibling file. If the field's type can't be resolved, no edge is emitted and nothing is parked as an unresolved call. New tests cover the same-file case, the sibling-file case, and the unknown-type case.
No blocking issues surfaced. 1 lower-confidence candidate did not survive cross-model review.
Analysis details — impact, health, verification
Impact & health
Graphify review
Impact — 69 functions depend on the 43 functions this change touches.
Health — grade B; 2 existing hotspot(s) in the area this change touches (pre-existing, not introduced here):
extract_go()— 19 callers, 13 callees (high)walk()— 1 callers, 13 callees (high)
Verification — 69 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: 69 function(s) in the blast radius were not formally verified this run
Test selection
Test selection
4 of 487 test file(s) selected (1%) via static blast radius.
tests/test_go_method_value_indirect_calls.py— impact, changed-testtests/test_go_type_alias.py— impacttests/test_languages.py— impacttests/test_multilang.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
No difference found (not proven): No behavior difference found in extract\_go (not a proof).
The verifier ran both versions of extract\_go on many inputs and saw identical behavior every time. Strong evidence the change is safe, but evidence, not a proof.
Guarantee: Empirical: differential testing (both versions run on many generated inputs). A divergence on an untested input remains possible, so this is 'no counterexample found', not 'proven equivalent'.
Note: An input the sampler did not try could still differ.
… fixes #4933 (python bare-call), #4931 (go method-value via field) and #4932 (go var-init type refs) were merged on GitHub but each changes AST-cached output and deferred its schema bump; one combined 33->34 bump so cached Python/Go extractions re-run. #4929 (namespace packages) is a post-cache pass and needs none. Opens the unreleased 0.9.101 changelog. Co-Authored-By: Claude Opus 4.8 <[email protected]>
What does this PR do?
Refs #1566. 61e864a (
fix(go): link a method value passed as an argument as an indirect_call) linksrouting.Wrap(s.Health)toServer.Health. It does not link a method value reached through a field of the receiver, which is how Go code usually registers a handler held as a dependency:On v8 (
f5155bad, 0.9.100) the call edges are onlyaddMiddlewares -> UseMiddleware (calls)plusreferences; there is no edge toContextHandler.Middleware. Same result whenHTTPServeris declared in a sibling file of the package. The real-world shape is Grafanapkg/api/http_server.go:732,m.UseMiddleware(hs.ContextHandler.Middleware).Root cause
In
extract_go'swalk_calls, the method-value loop over call arguments added in 61e864a skips anyselector_expressionargument whose operand is not anidentifier.hs.ContextHandler.Middlewarehas aselector_expressionoperand (hs.ContextHandler), so no raw call was recorded. The direct-call branch already handles the same shape (hs.ContextHandler.Middleware()) by recording the field asmember_receiverand the receiver's type asfield_owner, and_resolve_go_member_callsalready types that through the caller's table or, for a struct declared in a sibling file, the owner's_go_fields(#3368).The change
graphify/extractors/go.pyonly: the argument loop also acceptsself.field.Mwhenselfis the enclosing method's own receiver, and records it withmember_receiver = field,receiver_type = None,field_owner = self_type, the same values the direct-call branch uses. Resolution, guards and the emitted edge (indirect_call,context: "argument", INFERRED, 0.85) are unchanged code in_resolve_go_member_calls. A chain rooted at anything other than the method's own receiver is still skipped, as for direct calls.Scope
*contexthandler.ContextHandler, the Grafana case)._go_single_type_namedrops aqualified_type, so the field has no type on v8; that is what fix(go): resolve member calls on receivers typed from another package (#3368) #4853 adds, and this PR does not duplicate it. I applied fix(go): resolve member calls on receivers typed from another package (#3368) #4853's source diff together with this change in a scratch worktree: the cross-package repro then also getsaddMiddlewares -> ContextHandler.Middleware (indirect_call)with no further change.Type of change
Verification & Invariants
Invariant: a Go method value binds only to a method of the single type its receiver is declared as, and a value whose type is unknown links nothing and parks nothing (it may be a field, not a method). The new path goes through the same resolver and guards as a direct
s.field.M()call, so it can only add an edge where the direct-call form would get one.New tests in
tests/test_go_method_value_indirect_calls.py:test_a_method_value_through_a_receiver_field_is_an_indirect_call: same file; assertsindirect_call, INFERRED, nocallsedge toMiddleware, and theUseMiddlewarecall unchanged.test_a_method_value_through_a_field_declared_in_a_sibling_file: struct declared in another file of the package.test_a_method_value_through_a_field_of_unknown_type_links_and_parks_nothing: field typed*Missing(declared nowhere): noindirect_call, nounresolved_callson the caller.Read the CONTRIBUTING.md guide.
Reproduced the issue and identified the invariant.
Made the smallest fix necessary.
Added a regression test (if bug fix) or isolated boundary test.
Kept the PR description synchronized with the final implementation.
Documented any limitations / unsupported cases explicitly.
How was this tested?
Ad-hoc repro (
graphify.extract.extract(..., parallel=False)) for the three variants: same-package and sibling-file now emitaddMiddlewares -> Middleware (indirect_call); cross-package emits it only with #4853 applied.Graphify-specific checklist
uv run python -m tools.skillgen --bless) when changing their source fragments. (No skill source changed;--checkpasses.)