Skip to content

Commit 836fb8f

Browse files
rajatnagda45safishamsi
authored andcommitted
fix(extract): drop Scala generic type parameters from references
A `class Box[T]` / `def map[R](...)` binds `T`/`R` as local names, not corpus symbols, but `_scala_collect_type_refs` appended the bare identifiers used in a signature as type references and ran them through the sourceless-stub path. Every generic class/method in the corpus then collapsed its `T` onto one bare `t` node — a god node that pollutes god-node rankings, since single-letter type parameters are not in the analyzer's builtin-noise filter. C#, Java, TypeScript, Kotlin, and Go already exclude theirs. Mirrors that pattern: a new `_scala_type_parameters_in_scope` helper walks up to every enclosing declaration's `type_parameters` list (the use site is `type_arguments`, a distinct node) and collects parameter names from direct identifiers and variance-annotated params, skipping `context_bound`/ `upper_bound`/`lower_bound` nodes (those name real types). `_scala_collect_type_refs` auto-computes and threads that skip set. Context bounds (#2046), genuine user types, and the generic container head still reference. (cherry picked from commit 585e909)
1 parent 6d64681 commit 836fb8f

2 files changed

Lines changed: 98 additions & 7 deletions

File tree

‎graphify/extractors/engine.py‎

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1442,15 +1442,55 @@ def _scala_given_synthetic_name(node, source: bytes) -> str | None:
14421442
return "given"
14431443

14441444

1445-
def _scala_collect_type_refs(node, source: bytes, generic: bool, out: list[tuple[str, str]]) -> None:
1445+
def _scala_type_parameters_in_scope(node, source: bytes) -> frozenset[str]:
1446+
"""Return Scala type-parameter names visible from ``node``.
1447+
1448+
A `class Box[T]` / `def map[R](...)` binds `T`/`R` as local names, not
1449+
corpus symbols. Walking up to every enclosing declaration's `type_parameters`
1450+
list (the use site is `type_arguments`, a distinct node, so an instantiation
1451+
argument is never collected) collects them for the scope skip set. A
1452+
variance-annotated parameter (`[+A]`/`[-B]`) wraps its name one level
1453+
deeper. A bound (`[A: Ordering]`, `[T <: Base]`) names a real type, not a
1454+
parameter, so its `context_bound`/`upper_bound`/`lower_bound` node is skipped.
1455+
"""
1456+
names: set[str] = set()
1457+
scope = node
1458+
while scope is not None:
1459+
for child in scope.children:
1460+
if child.type != "type_parameters":
1461+
continue
1462+
for param in child.children:
1463+
if param.type in ("identifier", "type_identifier"):
1464+
name = _read_text(param, source)
1465+
if name:
1466+
names.add(name)
1467+
elif param.type in ("covariant_type_parameter", "contravariant_type_parameter"):
1468+
for sub in param.children:
1469+
if sub.type in ("identifier", "type_identifier"):
1470+
name = _read_text(sub, source)
1471+
if name:
1472+
names.add(name)
1473+
break
1474+
scope = scope.parent
1475+
return frozenset(names)
1476+
1477+
1478+
def _scala_collect_type_refs(node, source: bytes, generic: bool, out: list[tuple[str, str]],
1479+
skip: frozenset[str] | None = None) -> None:
14461480
"""Walk a Scala type expression; append (name, role) tuples.
1447-
Handles type_identifier, generic_type (List[T]), and common type wrappers."""
1481+
Handles type_identifier, generic_type (List[T]), and common type wrappers.
1482+
1483+
Generic type parameters in scope (``skip``) are local bindings, not
1484+
references; emitting one mints a sourceless stub (the bare `t` god node a
1485+
`[T]` class or method would otherwise share corpus-wide)."""
14481486
if node is None:
14491487
return
1488+
if skip is None:
1489+
skip = _scala_type_parameters_in_scope(node, source)
14501490
t = node.type
14511491
if t == "type_identifier":
14521492
text = _read_text(node, source)
1453-
if text:
1493+
if text and text not in skip:
14541494
out.append((text, "generic_arg" if generic else "type"))
14551495
return
14561496
if t == "generic_type":
@@ -1462,19 +1502,19 @@ def _scala_collect_type_refs(node, source: bytes, generic: bool, out: list[tuple
14621502
break
14631503
if base is not None and base.type == "type_identifier":
14641504
text = _read_text(base, source)
1465-
if text:
1505+
if text and text not in skip:
14661506
out.append((text, "generic_arg" if generic else "type"))
14671507
for c in node.children:
14681508
if c.type == "type_arguments":
14691509
for arg in c.children:
14701510
if arg.is_named:
1471-
_scala_collect_type_refs(arg, source, True, out)
1511+
_scala_collect_type_refs(arg, source, True, out, skip)
14721512
return
14731513
if t in ("compound_type", "infix_type", "function_type", "tuple_type",
14741514
"annotated_type", "projected_type"):
14751515
for c in node.children:
14761516
if c.is_named:
1477-
_scala_collect_type_refs(c, source, generic, out)
1517+
_scala_collect_type_refs(c, source, generic, out, skip)
14781518
# A `match type` (`type Elem[X] = X match { case String => Char }`) names a
14791519
# real type in every case clause -- both the type it discriminates on and
14801520
# the type it yields. Without these two the walker returned nothing for a
@@ -1483,7 +1523,7 @@ def _scala_collect_type_refs(node, source: bytes, generic: bool, out: list[tuple
14831523
if t in ("match_type", "type_case_clause"):
14841524
for c in node.children:
14851525
if c.is_named:
1486-
_scala_collect_type_refs(c, source, generic, out)
1526+
_scala_collect_type_refs(c, source, generic, out, skip)
14871527

14881528

14891529
def _scala_collect_context_bounds(
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
"""Scala generic type parameters must not mint sourceless god nodes.
2+
3+
A `class Box[T]` / `def map[R](...)` binds `T`/`R` as local names, not corpus
4+
symbols. The Scala type-ref pass appended the bare `T`/`R` identifiers used in
5+
a signature as type references and ran them through the sourceless-stub path,
6+
so every generic class/method in the corpus collapsed its `T` onto one bare `t`
7+
node — a god node (single-letter type params are not in the analyzer's
8+
builtin-noise filter). C#, Java, TypeScript, Kotlin, and Go already exclude
9+
their type parameters; Scala now matches.
10+
"""
11+
from pathlib import Path
12+
13+
from graphify.extract import extract
14+
15+
16+
def _sourceless_labels(r):
17+
return sorted({n.get("label") for n in r["nodes"] if not n.get("source_file")})
18+
19+
20+
def test_generic_class_and_method_type_params_mint_no_stub(tmp_path):
21+
(tmp_path / "S.scala").write_text(
22+
"class Box[T](v: T) {\n"
23+
" def get(): T = v\n"
24+
" def map[R](f: T => R): R = ???\n"
25+
"}\n"
26+
)
27+
r = extract([tmp_path / "S.scala"], cache_root=tmp_path, parallel=False)
28+
assert not any(e.get("target") in ("t", "r") for e in r["edges"])
29+
assert "T" not in _sourceless_labels(r)
30+
assert "R" not in _sourceless_labels(r)
31+
32+
33+
def test_bounds_param_dropped_but_context_bound_and_real_types_kept(tmp_path):
34+
# `A` is a parameter (dropped); the `Ordering` context bound is a real
35+
# dependency (kept, #2046), and a genuine user type in the signature still
36+
# references. (An upper bound `<: User` is deliberately not emitted, #2046.)
37+
(tmp_path / "S.scala").write_text(
38+
"class User(name: String)\n"
39+
"class Processor[A: Ordering] {\n"
40+
" def run(u: User): User = u\n"
41+
"}\n"
42+
)
43+
r = extract([tmp_path / "S.scala"], cache_root=tmp_path, parallel=False)
44+
refs = {
45+
rn["label"]
46+
for e in r["edges"] if e.get("relation") == "references"
47+
for rn in r["nodes"] if rn["id"] == e["target"]
48+
}
49+
assert "Ordering" in refs
50+
assert "User" in refs
51+
assert "A" not in refs

0 commit comments

Comments
 (0)