fix(go): scope the predeclared-function filter to Go bare identifiers
Review feedback on #2296: the previous revision added Go's predeclared names to _LANGUAGE_BUILTIN_GLOBALS, which ~11 languages consult through engine.py and the cross-file pass, and whose check wraps the in-file EXTRACTED branch as well as raw_calls. Two confirmed regressions: * Rust normalizes 'Widget::new(3)' to the bare token 'new', so every in-file 'Type::new()' edge disappeared. Rust keeps its own _RUST_TRAIT_METHOD_BLOCKLIST, deliberately on the cross-file branch only — this change follows that language-local pattern. * Go 'h.append(v)' is a selector_expression call to a real method and was dropped with it. On the 3x-ui corpus this cost a genuine 'systemMetrics.append(...)' -> '(*metricHistory).append' edge. The filter now lives in extractors/go.py as _GO_PREDECLARED_FUNCS and fires only when the callee node is a bare identifier, so selector calls ('h.append(v)', 'pkg.Delete(x)') and every other language are untouched. Go raw_calls now carry language="go" and the shared pass gates on it, mirroring the bash gate, as a backstop for Go raw_calls minted elsewhere. The set is the Go spec's predeclared list in full: being Go-local and bare-identifier-only makes 'len'/'max'/'min'/'print' safe to include, and a spec boundary beats a hand-picked subset. Remeasured on the same 466-file Go corpus: upstream v8 16904 edges, 334 inbound to 'append' previous rev 16571 edges, 1 inbound (332 phantom gone, but the genuine selector call too) this rev 16572 edges, 2 inbound (332 phantom gone, genuine call restored) Tests kept, plus three regressions: builtin 'append' must not bind in-file (the branch a cross-file-only gate would miss), the Go selector call must survive, and the in-file Rust 'Type::new()' edge must survive. The last two fail on the previous revision. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
This commit is contained in:
committed by
safishamsi
co-authored by
Claude Opus 5
parent
773250ad31
commit
3868434942
+8
-1
@@ -43,7 +43,7 @@ from graphify.extractors.dart import extract_dart # noqa: F401
|
||||
from graphify.extractors.dm import extract_dm, extract_dmf, extract_dmi, extract_dmm # noqa: F401
|
||||
from graphify.extractors.elixir import extract_elixir # noqa: F401
|
||||
from graphify.extractors.fortran import _cpp_preprocess, extract_fortran # noqa: F401
|
||||
from graphify.extractors.go import extract_go # noqa: F401
|
||||
from graphify.extractors.go import _GO_PREDECLARED_FUNCS, extract_go # noqa: F401
|
||||
from graphify.extractors.json_config import extract_json # noqa: F401
|
||||
from graphify.extractors.markdown import extract_markdown # noqa: F401
|
||||
from graphify.extractors.pascal_forms import extract_delphi_form, extract_lazarus_form # noqa: F401
|
||||
@@ -5322,6 +5322,13 @@ def extract(
|
||||
# in the corpus — exactly what #2141 must not do.
|
||||
if rc.get("language") == "bash":
|
||||
continue
|
||||
# A Go predeclared function is never a cross-file call: the extractor
|
||||
# already drops bare `append(s, x)` (extractors/go.py), so this is the
|
||||
# backstop for Go raw_calls minted on any other path. Language-gated
|
||||
# rather than folded into _LANGUAGE_BUILTIN_GLOBALS because `new`,
|
||||
# `close` and `delete` are ordinary method names elsewhere (#2296).
|
||||
if rc.get("language") == "go" and callee in _GO_PREDECLARED_FUNCS:
|
||||
continue
|
||||
# Exact-case match first (case is semantic). Fold only when the CALLING
|
||||
# file's language is case-insensitive, and only against the folded index of
|
||||
# case-insensitive-language definitions — so a Python `Path()` call can never
|
||||
|
||||
@@ -48,16 +48,6 @@ _LANGUAGE_BUILTIN_GLOBALS: frozenset[str] = frozenset({
|
||||
"NSObject", "NSString", "NSError", "NSLock", "NSAttributedString",
|
||||
"DispatchQueue", "DispatchGroup", "OperationQueue", "RunLoop",
|
||||
"View", "Color", "Font",
|
||||
# Go predeclared functions. The Go resolver looks a callee up by bare name,
|
||||
# so an unexported method that happens to share a builtin's name (e.g.
|
||||
# `func (h *history) append(...)`) absorbs every builtin call in the repo:
|
||||
# on a 8.9k-node Go codebase one such method collected 330 phantom
|
||||
# inbound edges, inventing a database -> service layering violation.
|
||||
# Builtin *types* are deliberately not listed: Go conversions are
|
||||
# call-shaped too, but they produced no phantom edges on that corpus, and
|
||||
# listing them would suppress genuine constructor-like calls.
|
||||
"append", "cap", "clear", "close", "complex", "copy", "delete", "imag",
|
||||
"make", "new", "panic", "println", "real", "recover",
|
||||
})
|
||||
|
||||
|
||||
|
||||
@@ -12,6 +12,35 @@ _GO_PREDECLARED_TYPES = frozenset({
|
||||
"uint", "uint8", "uint16", "uint32", "uint64", "uintptr", "any", "comparable",
|
||||
})
|
||||
|
||||
# Go predeclared functions, filtered only when the callee is a BARE identifier.
|
||||
# The Go resolver looks a callee up by name, so an unexported method that happens
|
||||
# to share a builtin's name (`func (h *history) append(...)`) absorbs every
|
||||
# builtin call in the corpus: on an 8.9k-node Go codebase one such method
|
||||
# collected 330 phantom inbound `calls` edges, inventing twelve database-layer ->
|
||||
# service-layer edges — a layering violation absent from the source.
|
||||
#
|
||||
# Deliberately language-local (mirroring _RUST_TRAIT_METHOD_BLOCKLIST) rather
|
||||
# than added to the shared _LANGUAGE_BUILTIN_GLOBALS: `new`, `close` and friends
|
||||
# are ordinary method names in the ~11 other languages that consult the shared
|
||||
# set — listing them there kills every in-file Rust `Type::new()` edge.
|
||||
#
|
||||
# Bare-identifier-only for the same reason within Go: `h.append(v)` and
|
||||
# `pkg.Delete(x)` are selector_expression callees and are genuine calls, so the
|
||||
# filter must not reach them. Builtin *types* stay out (see
|
||||
# _GO_PREDECLARED_TYPES): Go conversions are call-shaped too, but they produced
|
||||
# no phantom edges on that corpus and filtering them would suppress genuine
|
||||
# constructor-like calls.
|
||||
#
|
||||
# The set is the Go spec's predeclared function list in full. Being Go-local and
|
||||
# bare-identifier-only makes completeness safe here: `len`, `max`, `min` and
|
||||
# `print` carry the same shadowing hazard as `append`, and a principled boundary
|
||||
# (the spec list) beats a hand-picked subset.
|
||||
_GO_PREDECLARED_FUNCS = frozenset({
|
||||
"append", "cap", "clear", "close", "complex", "copy", "delete", "imag",
|
||||
"len", "make", "max", "min", "new", "panic", "print", "println", "real",
|
||||
"recover",
|
||||
})
|
||||
|
||||
def _go_collect_type_refs(node, source: bytes, generic: bool, out: list[tuple[str, str]]) -> None:
|
||||
"""Walk a Go type expression; append (name, role) tuples."""
|
||||
if node is None:
|
||||
@@ -343,8 +372,10 @@ def extract_go(path: Path) -> dict:
|
||||
func_node = node.child_by_field_name("function")
|
||||
callee_name: str | None = None
|
||||
is_member_call: bool = False
|
||||
is_bare_identifier: bool = False
|
||||
if func_node:
|
||||
if func_node.type == "identifier":
|
||||
is_bare_identifier = True
|
||||
callee_name = _read_text(func_node, source)
|
||||
elif func_node.type == "selector_expression":
|
||||
field = func_node.child_by_field_name("field")
|
||||
@@ -355,6 +386,12 @@ def extract_go(path: Path) -> dict:
|
||||
is_member_call = receiver_name not in go_imported_pkgs
|
||||
if field:
|
||||
callee_name = _read_text(field, source)
|
||||
if is_bare_identifier and callee_name in _GO_PREDECLARED_FUNCS:
|
||||
# A bare `append(s, x)` is the builtin, never the same-named
|
||||
# method a sibling file happens to declare. Skipping before both
|
||||
# branches drops the in-file phantom edge and keeps the name out
|
||||
# of raw_calls, so the cross-file pass cannot bind it either.
|
||||
callee_name = None
|
||||
if callee_name and callee_name not in _LANGUAGE_BUILTIN_GLOBALS:
|
||||
tgt_nid = label_to_nid.get(callee_name)
|
||||
if tgt_nid and tgt_nid != caller_nid:
|
||||
@@ -377,6 +414,7 @@ def extract_go(path: Path) -> dict:
|
||||
"caller_nid": caller_nid,
|
||||
"callee": callee_name,
|
||||
"is_member_call": is_member_call,
|
||||
"language": "go",
|
||||
"source_file": str_path,
|
||||
"source_location": f"L{node.start_point[0] + 1}",
|
||||
})
|
||||
|
||||
@@ -12,6 +12,11 @@ append(...)` method collected 330 phantom inbound `calls` edges from every
|
||||
`append(slice, x)` in the project, which in turn invented twelve
|
||||
database-layer -> service-layer edges (a layering violation that does not
|
||||
exist in the source).
|
||||
|
||||
The fix is Go-local (`_GO_PREDECLARED_FUNCS`) and applies only to bare-identifier
|
||||
callees. The last two tests here are the reason: putting these names in the
|
||||
shared set instead would kill in-file Rust `Type::new()` edges (`new` normalizes
|
||||
to the same token) and drop genuine Go `h.append(v)` selector calls.
|
||||
"""
|
||||
import pytest
|
||||
|
||||
@@ -22,6 +27,17 @@ def _nodes_by_file(result, suffix):
|
||||
return [n for n in result["nodes"] if str(n.get("source_file", "")).endswith(suffix)]
|
||||
|
||||
|
||||
def _label(node):
|
||||
return (node.get("label") or "").strip(".()")
|
||||
|
||||
|
||||
def _edges_between(result, source_ids, target_ids):
|
||||
return [
|
||||
e for e in result["edges"]
|
||||
if e.get("source") in source_ids and e.get("target") in target_ids
|
||||
]
|
||||
|
||||
|
||||
def _extract_go(tmp_path):
|
||||
return extract(sorted(tmp_path.glob("*.go")), cache_root=tmp_path, parallel=False)
|
||||
|
||||
@@ -114,3 +130,96 @@ def test_non_builtin_cross_file_call_still_resolves(tmp_path):
|
||||
if e.get("target") in target_ids and e.get("source") in runner_ids
|
||||
]
|
||||
assert resolved, "a genuine cross-file method call must still resolve"
|
||||
|
||||
|
||||
def test_builtin_append_does_not_bind_in_file(tmp_path):
|
||||
"""Same-file binding needs the guard too, not just the cross-file pass.
|
||||
|
||||
`walk_calls` resolves a bare callee against the file's own label index
|
||||
first, so a sibling function in the SAME file as the shadowing method binds
|
||||
without ever reaching `raw_calls`. Gating only the cross-file pass would
|
||||
leave this edge behind.
|
||||
"""
|
||||
(tmp_path / "history.go").write_text(
|
||||
"package main\n"
|
||||
"\n"
|
||||
"type metricHistory struct {\n"
|
||||
"\tsamples []int\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"func (h *metricHistory) append(v int) {\n"
|
||||
"\th.samples = append(h.samples, v)\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"func widen(xs []int) []int {\n"
|
||||
"\treturn append(xs, 0)\n"
|
||||
"}\n"
|
||||
)
|
||||
result = _extract_go(tmp_path)
|
||||
method_ids = {n["id"] for n in _nodes_by_file(result, "history.go") if _label(n) == "append"}
|
||||
widen_ids = {n["id"] for n in _nodes_by_file(result, "history.go") if _label(n) == "widen"}
|
||||
assert method_ids and widen_ids, "both symbols must still be extracted as nodes"
|
||||
|
||||
phantom = _edges_between(result, widen_ids, method_ids)
|
||||
assert phantom == [], f"builtin append() in widen() bound to the method: {phantom}"
|
||||
|
||||
|
||||
def test_go_selector_call_to_shadowing_method_survives(tmp_path):
|
||||
"""`h.append(v)` is a real method call — the filter must not reach it.
|
||||
|
||||
The callee is a `selector_expression`, not a bare identifier. Filtering by
|
||||
name alone (the shared-set approach) drops this genuine edge.
|
||||
"""
|
||||
(tmp_path / "history.go").write_text(
|
||||
"package main\n"
|
||||
"\n"
|
||||
"type metricHistory struct {\n"
|
||||
"\tsamples []int\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"func (h *metricHistory) append(v int) {\n"
|
||||
"\th.samples = append(h.samples, v)\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"func record(h *metricHistory, v int) {\n"
|
||||
"\th.append(v)\n"
|
||||
"}\n"
|
||||
)
|
||||
result = _extract_go(tmp_path)
|
||||
method_ids = {n["id"] for n in _nodes_by_file(result, "history.go") if _label(n) == "append"}
|
||||
record_ids = {n["id"] for n in _nodes_by_file(result, "history.go") if _label(n) == "record"}
|
||||
assert method_ids and record_ids, "both symbols must still be extracted as nodes"
|
||||
|
||||
resolved = _edges_between(result, record_ids, method_ids)
|
||||
assert resolved, "a genuine h.append(v) selector call must still resolve"
|
||||
|
||||
|
||||
def test_rust_in_file_type_new_edge_survives(tmp_path):
|
||||
"""Cross-language guard: `new` must stay resolvable in Rust.
|
||||
|
||||
Rust normalizes `Widget::new(3)` to the bare token `new`, and the
|
||||
builtin check wraps the in-file EXTRACTED branch as well as `raw_calls`.
|
||||
Adding Go's predeclared names to the shared `_LANGUAGE_BUILTIN_GLOBALS`
|
||||
therefore erased every in-file `Type::new()` edge in a Rust codebase —
|
||||
which is why `_GO_PREDECLARED_FUNCS` is Go-local. Rust keeps its own
|
||||
`_RUST_TRAIT_METHOD_BLOCKLIST`, deliberately applied to the cross-file
|
||||
branch only.
|
||||
"""
|
||||
(tmp_path / "lib.rs").write_text(
|
||||
"pub struct Widget { n: i32 }\n"
|
||||
"\n"
|
||||
"impl Widget {\n"
|
||||
" pub fn new(n: i32) -> Widget { Widget { n } }\n"
|
||||
"}\n"
|
||||
"\n"
|
||||
"pub fn build() -> Widget {\n"
|
||||
" Widget::new(3)\n"
|
||||
"}\n"
|
||||
)
|
||||
result = extract(sorted(tmp_path.glob("*.rs")), cache_root=tmp_path, parallel=False)
|
||||
new_ids = {n["id"] for n in _nodes_by_file(result, "lib.rs") if _label(n) == "new"}
|
||||
build_ids = {n["id"] for n in _nodes_by_file(result, "lib.rs") if _label(n) == "build"}
|
||||
assert new_ids and build_ids, "both symbols must still be extracted as nodes"
|
||||
|
||||
resolved = _edges_between(result, build_ids, new_ids)
|
||||
assert resolved, "an in-file Rust Type::new() call must still resolve"
|
||||
|
||||
Reference in New Issue
Block a user