fix(go): link a method value reached through a receiver field (#1566)

61e864a7 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 (#3368) and emits the
INFERRED `indirect_call`. A field whose type is declared nowhere links and
parks nothing, as before.
This commit is contained in:
nothariharan committed 2026-11-01 18:22:15 +05:30
1 parent f5155baddd
commit 6bb5913dac
2 files changed
+79 -6

No files matched your search

+22 -6
View File
@@ -876,17 +876,33 @@ def extract_go(path: Path) -> dict:
# `r.Get("/p", s.Handle)`) is how Go registers a handler or callback. It is
# not a call here, so it never reached the branch above and the method had
# no caller. Record it for the member-call resolver as an indirect call
# through the receiver's declared type (#1566).
# through the receiver's declared type (#1566). `s.field.Handle` is typed
# through the field, as a direct `s.field.M()` is, only when `s` is this
# method's own receiver.
args = node.child_by_field_name("arguments")
for arg in args.children if args is not None else ():
if arg.type != "selector_expression":
continue
operand = arg.child_by_field_name("operand")
field = arg.child_by_field_name("field")
if operand is None or field is None or operand.type != "identifier":
if operand is None or field is None:
continue
name = _read_text(operand, source)
if name in go_imported_pkgs:
arg_field_owner: str | None = None
if operand.type == "identifier":
name = _read_text(operand, source)
if name in go_imported_pkgs:
continue
arg_receiver_type = (self_type if self_name and name == self_name
else scope_types.get(name))
elif operand.type == "selector_expression":
inner = operand.child_by_field_name("operand")
if not (self_name and inner is not None and inner.type == "identifier"
and _read_text(inner, source) == self_name):
continue
name = _read_text(operand.child_by_field_name("field"), source)
arg_receiver_type = None
arg_field_owner = self_type
else:
continue
raw_calls.append({
"caller_nid": caller_nid,
@@ -897,8 +913,8 @@ def extract_go(path: Path) -> dict:
"language": "go",
"receiver": None,
"member_receiver": name,
"receiver_type": self_type if self_name and name == self_name
else scope_types.get(name),
"receiver_type": arg_receiver_type,
"field_owner": arg_field_owner,
"import_path": None,
"source_file": str_path,
"source_location": f"L{arg.start_point[0] + 1}",
@@ -95,3 +95,60 @@ def test_a_package_function_value_is_not_read_as_a_method_value(tmp_path):
"func Register(s *Server) any {\n\treturn keep(routing.Wrap)\n}\n\n"
"func keep(v any) any { return v }\n"))
assert not [k for k in edges if k[1] == "indirect_call"], edges
# A method value reached through a field of the method's own receiver:
# `m.UseMiddleware(hs.ContextHandler.Middleware)`. The field types the receiver, as it
# does for a direct `hs.ContextHandler.Middleware()`.
HANDLER = (
"package api\n\ntype ContextHandler struct{}\n\n"
"func (h *ContextHandler) Middleware(next int) int { return next }\n"
)
HTTP_SERVER = (
"package api\n\ntype Macaron struct{}\n\n"
"func (m *Macaron) UseMiddleware(f func(int) int) {}\n\n"
"type HTTPServer struct {\n\tContextHandler *ContextHandler\n}\n"
)
ADD_MIDDLEWARES = (
"func (hs *HTTPServer) addMiddlewares(m *Macaron) {\n"
"\tm.UseMiddleware(hs.ContextHandler.Middleware)\n}\n"
)
def test_a_method_value_through_a_receiver_field_is_an_indirect_call(tmp_path):
edges = _edges(tmp_path, {
"go.mod": "module example.com/m\n\ngo 1.22\n",
"api/handler.go": HANDLER,
"api/http_server.go": HTTP_SERVER + "\n" + ADD_MIDDLEWARES,
})
edge = edges.get((".addMiddlewares()", "indirect_call", ".Middleware()"))
assert edge is not None, edges
assert edge["confidence"] == "INFERRED"
assert (".addMiddlewares()", "calls", ".Middleware()") not in edges, edges
assert (".addMiddlewares()", "calls", ".UseMiddleware()") in edges, edges
def test_a_method_value_through_a_field_declared_in_a_sibling_file(tmp_path):
edges = _edges(tmp_path, {
"go.mod": "module example.com/m\n\ngo 1.22\n",
"api/handler.go": HANDLER,
"api/http_server.go": HTTP_SERVER,
"api/middlewares.go": "package api\n\n" + ADD_MIDDLEWARES,
})
assert (".addMiddlewares()", "indirect_call", ".Middleware()") in edges, edges
def test_a_method_value_through_a_field_of_unknown_type_links_and_parks_nothing(tmp_path):
files = {
"go.mod": "module example.com/m\n\ngo 1.22\n",
"api/handler.go": HANDLER,
"api/http_server.go": HTTP_SERVER.replace("*ContextHandler", "*Missing")
+ "\n" + ADD_MIDDLEWARES,
}
edges = _edges(tmp_path, files)
assert not [k for k in edges if k[1] == "indirect_call"], edges
result = extract([tmp_path / n for n in files if n.endswith(".go")],
cache_root=tmp_path / "graphify-out", parallel=False)
caller = next(n for n in result["nodes"] if n["label"] == ".addMiddlewares()")
assert not (caller.get("metadata") or {}).get("unresolved_calls"), caller