Skip to content

Commit 447d41d

Browse files
Faisal-Fayazclaude
authored andcommitted
fix(extract): keep Scala file-scope val/var type references (#2054)
Scala 3 allows `val`/`var` to be written directly at file scope with no enclosing object/class/trait. The `val_definition`/`var_definition` type-reference dispatch was gated on `and parent_class_nid`, so a file-scope binding produced no node and no edge of any kind, while the byte-identical definition nested one level inside any container resolved correctly. The missing container was the only difference: both positions parse to the same node type carrying the same `type` field. This is the Scala instance of a generic defect in the walker's default recursion path that had already caused and been fixed once for Python (#1050) -- a position that legitimately has no container must fall back to the file node rather than be skipped. The fix reuses the `owner_nid = parent_class_nid or file_nid` idiom already present in the property branch above, and moves the self-reference guard onto the same anchor so a file-scope binding cannot mint an edge from the file node to itself. Behaviour is unchanged for class-nested bindings; a regression test pins that case explicitly. Verified: - uv run pytest tests/test_scala_top_level_binding.py -q (8 passed) - 6 of the 8 new tests fail on the pre-fix tree, 2 pass either way (the in-class control, and the self-edge guard which is trivially empty before the fix and meaningful after it) - uv run pytest tests/ -q -> 28 failed, 6144 passed, 101 skipped; the 28 failures are byte-identical to the clean-tree baseline (missing tree-sitter grammars for erlang/R/solidity/vbnet, plus ollama retry tests) and none touch Scala - uv run ruff check . -> clean - uv run pyright -> 635 errors, identical to the clean-tree baseline (pre-existing debt; this change adds none) Co-Authored-By: Claude <[email protected]> (cherry picked from commit 2d61ef7)
1 parent b523898 commit 447d41d

2 files changed

Lines changed: 154 additions & 4 deletions

File tree

‎graphify/extractors/engine.py‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5329,8 +5329,19 @@ def scala_base_name(type_node) -> str | None:
53295329
return
53305330

53315331
if (config.ts_module == "tree_sitter_scala"
5332-
and t in ("val_definition", "var_definition")
5333-
and parent_class_nid):
5332+
and t in ("val_definition", "var_definition")):
5333+
# Scala 3 drops the boilerplate wrapper: `val`/`var` can be written
5334+
# directly at file scope with no enclosing object/class/trait. Gating
5335+
# on `parent_class_nid` therefore dropped every type reference a
5336+
# file-scope binding makes -- the parsed shape is identical to the
5337+
# in-class case (both carry a `type` field), so the missing container
5338+
# was the only difference, and the binding vanished from the graph
5339+
# entirely rather than degrading (#2054). This is the Scala instance
5340+
# of the same generic defect already fixed for Python (#1050): a
5341+
# position that legitimately has no container must fall back to the
5342+
# file node rather than be skipped. Reuses the owner idiom from the
5343+
# property branch above.
5344+
owner_nid = parent_class_nid or file_nid
53345345
type_node = node.child_by_field_name("type")
53355346
if type_node is not None:
53365347
line = node.start_point[0] + 1
@@ -5339,8 +5350,8 @@ def scala_base_name(type_node) -> str | None:
53395350
for ref_name, role in refs:
53405351
ctx = "generic_arg" if role == "generic_arg" else "field"
53415352
target_nid = ensure_named_node(ref_name, line)
5342-
if target_nid != parent_class_nid:
5343-
add_edge(parent_class_nid, target_nid, "references",
5353+
if target_nid != owner_nid:
5354+
add_edge(owner_nid, target_nid, "references",
53445355
line, context=ctx)
53455356
# fall through so any call expressions in the initializer get walked
53465357

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
"""Scala top-level (file-scope) `val`/`var` bindings lose every type reference.
2+
3+
Scala 3's headline feature is dropping the boilerplate wrapper: `val`, `var`,
4+
`given` and `def` can be written directly at file scope with no enclosing
5+
`object`/`class`/`trait`. The `val_definition`/`var_definition` type-reference
6+
dispatch was gated on `and parent_class_nid`, so a file-scope binding silently
7+
produced no node and no edge of any kind -- the exact same definition nested one
8+
level inside any container resolved correctly. The gate was the only difference:
9+
both positions parse to an identical `val_definition` with a `type` field.
10+
11+
The same generic mechanism in the walker's default recursion path had already
12+
caused and been fixed once for a Python-specific case (#1050), so this is the
13+
Scala instance of a known defect rather than a new one.
14+
15+
These tests pin the anchor fallback: a file-scope binding attributes its type
16+
reference to the file node, a class-nested binding is unchanged, and the
17+
self-reference guard follows the anchor so a file-scope binding whose type
18+
resolves to the file's own stem does not mint an edge to itself.
19+
"""
20+
from pathlib import Path
21+
22+
from graphify.extract import extract_scala
23+
24+
SRC = '''\
25+
class Inner {
26+
val nested: String = "x"
27+
var nestedCounter: Int = 0
28+
}
29+
30+
val greeting: String = "World"
31+
var counter: Int = 0
32+
33+
trait Config
34+
val config: Config = null
35+
36+
val names: List[String] = Nil
37+
'''
38+
39+
40+
def _build(tmp_path):
41+
(tmp_path / "TopLevel.scala").write_text(SRC)
42+
r = extract_scala(tmp_path / "TopLevel.scala")
43+
nid = {n["label"]: n["id"] for n in r["nodes"]}
44+
return r, nid
45+
46+
47+
def _rels(r, relation):
48+
return {(e["source"], e["target"]) for e in r["edges"] if e["relation"] == "references"}
49+
50+
51+
def test_class_nested_binding_keeps_its_type_reference(tmp_path):
52+
# Control: the pre-existing in-class behaviour must not regress. The edge is
53+
# owned by the class, not the file.
54+
r, nid = _build(tmp_path)
55+
refs = _rels(r, "references")
56+
assert (nid["Inner"], nid["String"]) in refs
57+
58+
59+
def test_top_level_val_emits_type_reference_owned_by_file(tmp_path):
60+
r, nid = _build(tmp_path)
61+
refs = _rels(r, "references")
62+
file_nid = nid["TopLevel.scala"]
63+
assert (file_nid, nid["String"]) in refs
64+
65+
66+
def test_top_level_var_emits_type_reference_owned_by_file(tmp_path):
67+
r, nid = _build(tmp_path)
68+
refs = _rels(r, "references")
69+
file_nid = nid["TopLevel.scala"]
70+
assert (file_nid, nid["Int"]) in refs
71+
72+
73+
def test_top_level_binding_type_resolves_to_another_declaration(tmp_path):
74+
# The type is a real declaration elsewhere in the corpus, so the reference
75+
# must land on that node rather than on a bare stub.
76+
r, nid = _build(tmp_path)
77+
refs = _rels(r, "references")
78+
file_nid = nid["TopLevel.scala"]
79+
assert (file_nid, nid["Config"]) in refs
80+
81+
82+
def test_top_level_binding_emits_no_self_edge(tmp_path):
83+
# The self-reference guard compared against parent_class_nid, which is None at
84+
# file scope. Anchoring on the file node must keep the guard working there,
85+
# otherwise a binding typed with the file's own stem mints file -> file.
86+
r, nid = _build(tmp_path)
87+
file_nid = nid["TopLevel.scala"]
88+
refs = _rels(r, "references")
89+
assert not any(src == file_nid and tgt == file_nid for src, tgt in refs)
90+
91+
92+
def test_top_level_type_reference_is_tagged_extracted(tmp_path):
93+
# A type annotation written in the source is an extracted fact, not an
94+
# inference: the confidence tag must say so, or a consumer cannot tell a
95+
# real annotation from a guess (#2054).
96+
r, nid = _build(tmp_path)
97+
file_nid = nid["TopLevel.scala"]
98+
edges = [e for e in r["edges"]
99+
if e["relation"] == "references" and e["source"] == file_nid]
100+
assert edges, "expected the file node to own the top-level type references"
101+
for e in edges:
102+
assert e.get("confidence") == "EXTRACTED", e
103+
104+
105+
def test_top_level_type_reference_carries_source_location(tmp_path):
106+
# The edge must point at the line the annotation is written on, so a query
107+
# can cite it without a second file read. Locations serialise as "L<line>".
108+
r, nid = _build(tmp_path)
109+
file_nid = nid["TopLevel.scala"]
110+
edges = [e for e in r["edges"]
111+
if e["relation"] == "references" and e["source"] == file_nid]
112+
assert edges
113+
locs = set()
114+
for e in edges:
115+
loc = e.get("source_location")
116+
assert isinstance(loc, str) and loc.startswith("L"), e
117+
locs.add(int(loc[1:]))
118+
# `val greeting` L6, `var counter` L7, `val config` L10, `val names` L12 --
119+
# all strictly inside the file, never the default line 1.
120+
assert locs == {6, 7, 10, 12}, locs
121+
122+
123+
def test_top_level_generic_annotation_keeps_its_own_context(tmp_path):
124+
# `List[String]` at file scope must resolve BOTH the base type and the type
125+
# argument, and the argument keeps its own `generic_arg` context rather than
126+
# being flattened into the plain `field` shape -- so a consumer can tell a
127+
# type argument from a plain annotation. The type parameter `String` is
128+
# shadowed by the real `String` node, which is exactly the resolution the
129+
# in-class path already performs.
130+
r, nid = _build(tmp_path)
131+
refs = _rels(r, "references")
132+
file_nid = nid["TopLevel.scala"]
133+
assert (file_nid, nid["List"]) in refs
134+
assert (file_nid, nid["String"]) in refs
135+
ctxs = {(tgt, e.get("context")) for e in r["edges"]
136+
if e["relation"] == "references" and e["source"] == file_nid
137+
for tgt in (e["target"],)}
138+
assert (nid["List"], "field") in ctxs
139+
assert (nid["String"], "generic_arg") in ctxs

0 commit comments

Comments
 (0)