Skip to content

Codebase intelligence for AI and humans: code health scores, auto-generated docs, git analytics, dead code detection, and architectural decisions via MCP.

fix(go): capture a generic call written with explicit type arguments (#2992)

Yang Baic0dfbffb3eed
diff --git a/packages/core/src/repowise/core/ingestion/queries/go.scm b/packages/core/src/repowise/core/ingestion/queries/go.scm
index dbd695b2a..966a47f28 100644
--- a/packages/core/src/repowise/core/ingestion/queries/go.scm
+++ b/packages/core/src/repowise/core/ingestion/queries/go.scm
@@ -55,6 +55,49 @@
arguments: (argument_list) @call.arguments
) @call.site
+; Generic call with explicit type arguments and one argument: F[int](x)
+; tree-sitter-go cannot tell this from a conversion to a generic type, so it is
+; a type_conversion_expression, which no call_expression pattern matches. A
+; real conversion (List[int](xs)) is captured too, and resolves to the type as
+; a plain conversion (MyInt(x), a call_expression) always has.
+(type_conversion_expression
+ type: (generic_type
+ type: (type_identifier) @call.target
+ )
+) @call.site
+
+; The same through a package: pkg.NewQ[string](x)
+(type_conversion_expression
+ type: (generic_type
+ type: (qualified_type
+ package: (package_identifier) @call.receiver
+ name: (type_identifier) @call.target
+ )
+ )
+) @call.site
+
+; One type argument with no argument or several: F[int](), F[int](a, b)
+; Here the grammar reads F[int] as an index expression. (Two or more type
+; arguments with two or more arguments parse as an ordinary call_expression
+; with a type_arguments field, which the patterns above already match.)
+(call_expression
+ function: (index_expression
+ operand: (identifier) @call.target
+ )
+ arguments: (argument_list) @call.arguments
+) @call.site
+
+; The same through a package: pkg.NewQ[string](), pkg.F[int](a, b)
+(call_expression
+ function: (index_expression
+ operand: (selector_expression
+ operand: (identifier) @call.receiver
+ field: (field_identifier) @call.target
+ )
+ )
+ arguments: (argument_list) @call.arguments
+) @call.site
+
; Method call: obj.Method(args)
(call_expression
function: (selector_expression
diff --git a/tests/unit/ingestion/test_go_extractors.py b/tests/unit/ingestion/test_go_extractors.py
index dcf8783a1..d3449bbfb 100644
--- a/tests/unit/ingestion/test_go_extractors.py
+++ b/tests/unit/ingestion/test_go_extractors.py
@@ -3,6 +3,7 @@
from __future__ import annotations
from datetime import datetime
+from pathlib import Path
import pytest
@@ -141,3 +142,111 @@ class TestGoCallThroughField:
do_calls = [c for c in result.calls if c.target_name == "Do"]
assert len(do_calls) == 1, f"expected one call site for Do, got {do_calls}"
assert do_calls[0].receiver_name == "i"
+
+
+class TestGoGenericCallWithTypeArguments:
+ """``F[int](1)`` is a ``type_conversion_expression`` to tree-sitter-go, which
+ cannot tell it from a conversion, so no ``call_expression`` pattern saw it
+ and the call had no edge (#2988)."""
+
+ def _calls(self, parser: ASTParser, body: bytes) -> list[tuple[str | None, str]]:
+ src = (
+ b"package p\n\n"
+ b'import q "example.com/q"\n\n'
+ b"func F[T any](x T) T { return x }\n\n"
+ b"func Plain(x int) int { return x }\n\n"
+ b"type List[T any] []T\n\n"
+ b"type MyInt int\n\n"
+ b"func Build(xs []int, x int) int {\n" + body + b"\treturn 0\n}\n"
+ )
+ result = parser.parse_file(_file(), src)
+ return [(c.receiver_name, c.target_name) for c in result.calls]
+
+ def test_same_package_generic_call_is_captured(self, parser: ASTParser) -> None:
+ calls = self._calls(parser, b"\ta := F[int](1)\n\tb := Plain(2)\n\t_, _ = a, b\n")
+ assert sorted(target for _receiver, target in calls) == ["F", "Plain"]
+
+ def test_qualified_generic_call_keeps_its_package(self, parser: ASTParser) -> None:
+ calls = self._calls(parser, b"\tv := q.NewQ[string](x)\n\t_ = v\n")
+ assert calls == [("q", "NewQ")]
+
+ def test_several_type_arguments(self, parser: ASTParser) -> None:
+ calls = self._calls(parser, b"\tm := q.Map[int, string](xs)\n\t_ = m\n")
+ assert calls == [("q", "Map")]
+
+ def test_no_argument_and_several_arguments(self, parser: ASTParser) -> None:
+ """One type argument with zero or several arguments is an index
+ expression to the grammar, not a conversion: the same gap, another node."""
+ calls = self._calls(
+ parser,
+ b"\ta := F[int]()\n\tb := F[int](1, 2)\n\tc := q.NewQ[string]()\n"
+ b"\td := q.Pair[int](1, 2)\n\t_, _, _, _ = a, b, c, d\n",
+ )
+ assert calls == [(None, "F"), (None, "F"), ("q", "NewQ"), ("q", "Pair")]
+
+ def test_several_type_arguments_and_several_arguments_were_already_calls(
+ self, parser: ASTParser
+ ) -> None:
+ calls = self._calls(parser, b"\ta := F[int, string](1, 2)\n\t_ = a\n")
+ assert calls == [(None, "F")]
+
+ def test_plain_conversions_add_no_call_site(self, parser: ASTParser) -> None:
+ """`int(x)` and `MyInt(x)` were call_expression sites before and are
+ unchanged: one site each, no more."""
+ calls = self._calls(
+ parser, b"\ta := MyInt(x)\n\tb := float64(x)\n\t_, _ = a, b\n"
+ )
+ assert [target for _receiver, target in calls].count("MyInt") <= 1
+ assert [target for _receiver, target in calls].count("float64") <= 1
+
+ def test_a_conversion_to_a_generic_type_is_captured_as_one_site(
+ self, parser: ASTParser
+ ) -> None:
+ """Indistinguishable from a call in the syntax, so it is one site."""
+ calls = self._calls(parser, b"\tl := List[int](xs)\n\t_ = l\n")
+ assert calls == [(None, "List")]
+
+
+class TestGoGenericCallEdges:
+ """The captured call resolves as its plain form does: same package by name,
+ another package through the import."""
+
+ _LIB = ("go", "package q\n\nfunc NewQ[T any](x T) T { return x }\n")
+
+ def _edges(self, tmp_path: Path, body: str) -> set[tuple[str, str]]:
+ from tests.unit.ingestion.test_chained_receiver_calls import _edges
+
+ main = (
+ "package p\n\n"
+ 'import q "example.com/q"\n\n'
+ "func F[T any](x T) T { return x }\n\n"
+ "type List[T any] []T\n\n"
+ "type MyInt int\n\n"
+ "func Build(xs []int) {\n" + body + "}\n"
+ )
+ edges = _edges(
+ tmp_path,
+ {"p/main.go": ("go", main), "q/q.go": self._LIB},
+ links={"p/main.go": {"example.com/q": "q/q.go"}},
+ )
+ return {(caller, callee) for caller, callee, _confidence, _origin in edges}
+
+ @pytest.mark.parametrize("call", ["F[int](1)", "F[int]()", "F[int](1, 2)"])
+ def test_a_same_package_generic_call_has_its_edge(self, tmp_path: Path, call: str) -> None:
+ assert self._edges(tmp_path, f"\t_ = {call}\n") == {("p/main.go::Build", "p/main.go::F")}
+
+ @pytest.mark.parametrize("call", ['q.NewQ[string]("a")', "q.NewQ[string]()"])
+ def test_a_qualified_generic_call_resolves_through_the_import(
+ self, tmp_path: Path, call: str
+ ) -> None:
+ assert self._edges(tmp_path, f"\t_ = {call}\n") == {("p/main.go::Build", "q/q.go::NewQ")}
+
+ def test_a_conversion_to_a_generic_type_resolves_as_a_plain_conversion_does(
+ self, tmp_path: Path
+ ) -> None:
+ """The syntax cannot tell ``List[int](xs)`` from a call. It gets the edge
+ to the type that ``MyInt(x)`` has always had, no more and no less."""
+ plain = self._edges(tmp_path / "plain", "\t_ = MyInt(len(xs))\n")
+ generic = self._edges(tmp_path / "generic", "\t_ = List[int](xs)\n")
+ assert plain == {("p/main.go::Build", "p/main.go::MyInt")}
+ assert generic == {("p/main.go::Build", "p/main.go::List")}