Skip to content

Commit 6deb27f

Browse files
Yyunozorsafishamsi
authored andcommitted
fix(scala): dispatch self-type annotations to requires edges (#2052)
self_type (`self: Logging with Database =>`, `this: T =>`) was never dispatched on anywhere in the Scala extractor, so a trait/class's structural precondition on its enclosing type produced zero edges, in any context. The type node sits at a fixed position among self_type's unnamed-field children (binder identifier first, type second when present), and _scala_collect_type_refs already handles every shape that position can take (type_identifier, compound_type for `with`, refinement bodies) -- reused unchanged, one new dispatch branch. Also add the new `requires` relation to DEFAULT_AFFECTED_RELATIONS, mirroring how `indirect_call` was wired into blast-radius traversal when it was introduced, so `graphify affected` follows it like the existing inherits/mixes_in/embeds structural relations. Covers: single type, `with`-compound, structural refinement (base type only, matching how refinement bodies are already unscanned elsewhere), the binder-only `self =>` shape (no requires edge), coexistence with an unrelated `extends`, and a plain class without a self-type (no spurious edge).
1 parent 51b7b9f commit 6deb27f

3 files changed

Lines changed: 167 additions & 0 deletions

File tree

‎graphify/affected.py‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"uses",
2323
"mixes_in",
2424
"embeds",
25+
"requires",
2526
)
2627

2728

‎graphify/extractors/engine.py‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3135,6 +3135,31 @@ def _emit_java_parent_type(type_node, rel: str, at_line: int) -> None:
31353135
line, context=ctx)
31363136
# fall through so any call expressions in the initializer get walked
31373137

3138+
# Scala: `self: Logging with Database =>` (or `this: T =>`) declares a
3139+
# structural precondition on the enclosing type, not a mixin/reference.
3140+
# self_type carries no field names, so the type node is found
3141+
# positionally: the binder identifier is named[0], the type (when
3142+
# present) is named[1]. `self =>` binds a name with no type at all, so
3143+
# len(named) < 2 correctly yields no type node rather than misreading
3144+
# the binder as a type. _scala_collect_type_refs already handles every
3145+
# shape a self-type's type position can take (type_identifier,
3146+
# compound_type for `with`, refinement bodies via compound_type) --
3147+
# reused unchanged.
3148+
if (config.ts_module == "tree_sitter_scala"
3149+
and t == "self_type"
3150+
and parent_class_nid):
3151+
named = [c for c in node.children if c.is_named]
3152+
type_node = named[1] if len(named) >= 2 else None
3153+
if type_node is not None:
3154+
line = node.start_point[0] + 1
3155+
refs: list[tuple[str, str]] = []
3156+
_scala_collect_type_refs(type_node, source, False, refs)
3157+
for ref_name, role in refs:
3158+
target_nid = ensure_named_node(ref_name, line)
3159+
if target_nid != parent_class_nid:
3160+
add_edge(parent_class_nid, target_nid, "requires", line)
3161+
return
3162+
31383163
if (config.ts_module == "tree_sitter_cpp"
31393164
and t == "field_declaration"
31403165
and parent_class_nid):

‎tests/test_scala_self_type.py‎

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
"""Scala self-type annotations (`self: Logging with Database =>`, `this: T =>`).
2+
3+
A trait/class's self-type declares a structural precondition on the enclosing
4+
type ("this can only be mixed into something also providing X") -- the
5+
cake-pattern idiom. `self_type` was never dispatched on anywhere in the
6+
extractor, so it produced no edges at all, in any context (#2052). These
7+
tests pin the new dispatch branch: every real type shape a self-type can
8+
carry, the negative case where no type is present, that an ordinary class
9+
without a self-type never gains a spurious edge, that the new `requires`
10+
relation coexists cleanly with an unrelated `extends` on the same class, and
11+
that `affected` (blast radius) now traverses it.
12+
"""
13+
from pathlib import Path
14+
15+
import networkx as nx
16+
17+
from graphify.affected import affected_nodes
18+
from graphify.extract import extract_scala
19+
20+
SRC = '''\
21+
trait Loggable
22+
trait Database
23+
trait HasConfig
24+
trait BaseTrait
25+
26+
class SimpleSelfType {
27+
self: HasConfig =>
28+
29+
def configure(): Unit = ()
30+
}
31+
32+
class CompoundSelfType {
33+
self: Loggable with Database =>
34+
35+
def run(): Unit = ()
36+
}
37+
38+
class RefinedSelfType {
39+
self: Loggable { def extra: Int } =>
40+
41+
def refined(): Unit = ()
42+
}
43+
44+
class BinderOnlySelfType {
45+
self =>
46+
47+
def noop(): Unit = ()
48+
}
49+
50+
class NoSelfType {
51+
def plain(): Unit = ()
52+
}
53+
54+
class SelfTypeWithExtends extends BaseTrait {
55+
self: Loggable =>
56+
57+
def combined(): Unit = ()
58+
}
59+
'''
60+
61+
62+
def _build(tmp_path):
63+
(tmp_path / "SelfTypes.scala").write_text(SRC)
64+
r = extract_scala(tmp_path / "SelfTypes.scala")
65+
nid = {n["label"]: n["id"] for n in r["nodes"]}
66+
return r, nid
67+
68+
69+
def _rels(r, relation):
70+
return {(e["source"], e["target"]) for e in r["edges"] if e["relation"] == relation}
71+
72+
73+
def test_self_type_simple_type_emits_requires(tmp_path):
74+
r, nid = _build(tmp_path)
75+
requires = _rels(r, "requires")
76+
assert (nid["SimpleSelfType"], nid["HasConfig"]) in requires
77+
78+
79+
def test_self_type_compound_with_emits_requires_for_each_member(tmp_path):
80+
r, nid = _build(tmp_path)
81+
requires = _rels(r, "requires")
82+
assert (nid["CompoundSelfType"], nid["Loggable"]) in requires
83+
assert (nid["CompoundSelfType"], nid["Database"]) in requires
84+
85+
86+
def test_self_type_structural_refinement_emits_requires_for_base_only(tmp_path):
87+
r, nid = _build(tmp_path)
88+
requires = _rels(r, "requires")
89+
assert (nid["RefinedSelfType"], nid["Loggable"]) in requires
90+
# The refinement body's own member signature (`def extra: Int`) is not
91+
# walked -- consistent with every other refinement-type context in this
92+
# extractor (a plain `val x: Loggable { def extra: Int }` behaves the
93+
# same way), not a new limitation introduced here.
94+
assert "extra" not in nid
95+
96+
97+
def test_self_type_binder_only_emits_no_requires_edge(tmp_path):
98+
r, nid = _build(tmp_path)
99+
requires = _rels(r, "requires")
100+
binder_only = nid["BinderOnlySelfType"]
101+
assert not any(src == binder_only for src, _tgt in requires)
102+
103+
104+
def test_class_without_self_type_emits_no_requires_edge(tmp_path):
105+
r, nid = _build(tmp_path)
106+
requires = _rels(r, "requires")
107+
no_self_type = nid["NoSelfType"]
108+
assert not any(src == no_self_type for src, _tgt in requires)
109+
110+
111+
def test_self_type_coexists_with_unrelated_extends(tmp_path):
112+
r, nid = _build(tmp_path)
113+
inherits = _rels(r, "inherits")
114+
requires = _rels(r, "requires")
115+
combined = nid["SelfTypeWithExtends"]
116+
# extends_clause and self_type are independent code paths -- both fire
117+
# on the same class with no conflict.
118+
assert (combined, nid["BaseTrait"]) in inherits
119+
assert (combined, nid["Loggable"]) in requires
120+
121+
122+
def test_requires_edges_carry_no_context(tmp_path):
123+
r, _nid = _build(tmp_path)
124+
for e in r["edges"]:
125+
if e["relation"] == "requires":
126+
assert "context" not in e
127+
128+
129+
def test_affected_includes_self_type_dependents(tmp_path):
130+
r, nid = _build(tmp_path)
131+
g = nx.DiGraph()
132+
for n in r["nodes"]:
133+
g.add_node(n["id"], **n)
134+
for e in r["edges"]:
135+
g.add_edge(e["source"], e["target"], **e)
136+
137+
# HasConfig's blast radius now includes SimpleSelfType through `requires`
138+
# (DEFAULT_AFFECTED_RELATIONS), mirroring how `inherits`/`mixes_in` are
139+
# already traversed.
140+
affected = {h.node_id for h in affected_nodes(g, nid["HasConfig"])}
141+
assert nid["SimpleSelfType"] in affected

0 commit comments

Comments
 (0)