Skip to content

Commit ee29272

Browse files
Mpasha17Grok Bot
authored andcommitted
feat(scala): extract class-level context bounds
`class Foo[A: Ordering]` (and the same on case classes, traits and enums) now emits a `references` edge with the `type_bound` context from the type to each bound, reusing the method-level path from #2046. Closes #4080 Co-Authored-By: Grok Bot <[email protected]> (cherry picked from commit d1257ef)
1 parent 438a050 commit ee29272

2 files changed

Lines changed: 107 additions & 0 deletions

File tree

‎graphify/extractors/engine.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5056,6 +5056,22 @@ def scala_base_name(type_node) -> str | None:
50565056
if target_nid != class_nid:
50575057
add_edge(class_nid, target_nid, "references",
50585058
cp_line, context=ctx)
5059+
# Class-level context bounds (`class Foo[A: Ordering]`, also on
5060+
# traits and enums) are the same typeclass dependency as the
5061+
# method-level case (#2046), so they get the same `type_bound`
5062+
# context, attributed to the class (#4080).
5063+
for c in node.children:
5064+
if c.type != "type_parameters":
5065+
continue
5066+
bound_refs: list[tuple[str, str]] = []
5067+
_scala_collect_context_bounds(c, source, bound_refs)
5068+
for ref_name, role in bound_refs:
5069+
ctx = ("generic_arg" if role == "generic_arg"
5070+
else "type_bound")
5071+
target_nid = ensure_named_node(ref_name, line)
5072+
if target_nid != class_nid:
5073+
add_edge(class_nid, target_nid, "references",
5074+
line, context=ctx)
50595075

50605076
# C#: a primary constructor (`class Foo(IBar bar)`, C# 12+) declares
50615077
# its dependencies on the type declaration itself rather than in a

‎tests/test_scala_context_bounds.py‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,3 +137,94 @@ def test_bound_never_points_at_the_function_itself(tmp_path):
137137
r, nid = _build(tmp_path)
138138
for label in ("sort()", "program()", ".member()"):
139139
assert nid[label] not in _bound_targets(r, label, nid)
140+
141+
142+
# Class-level context bounds (#4080). `class_definition`, `trait_definition`
143+
# and `enum_definition` carry the same `type_parameters` node as a method, so
144+
# the bound is the same typeclass dependency, attributed to the type itself.
145+
CLASS_SRC = '''\
146+
trait Ordering
147+
trait Show
148+
trait Eq
149+
trait Async
150+
trait Comparable
151+
152+
class Sorted[A: Ordering](xs: List[A])
153+
154+
case class Pretty[T: Show: Eq](t: T)
155+
156+
trait Effectful[F[_]: Async]
157+
158+
enum Ranked[A: Ordering] { case Low }
159+
160+
class Plain[T](t: T)
161+
162+
class Upper[T <: Comparable[T]](t: T)
163+
164+
class Holder {
165+
def member[K: Ordering](k: K): K = k
166+
}
167+
'''
168+
169+
170+
def _build_classes(tmp_path):
171+
(tmp_path / "ClassContextBounds.scala").write_text(CLASS_SRC)
172+
r = extract_scala(tmp_path / "ClassContextBounds.scala")
173+
nid = {n["label"]: n["id"] for n in r["nodes"]}
174+
return r, nid
175+
176+
177+
def test_class_context_bound_emits_reference(tmp_path):
178+
r, nid = _build_classes(tmp_path)
179+
assert nid["Ordering"] in _bound_targets(r, "Sorted", nid)
180+
181+
182+
def test_case_class_several_context_bounds_each_emit_a_reference(tmp_path):
183+
r, nid = _build_classes(tmp_path)
184+
targets = _bound_targets(r, "Pretty", nid)
185+
assert nid["Show"] in targets
186+
assert nid["Eq"] in targets
187+
188+
189+
def test_trait_higher_kinded_context_bound_emits_reference(tmp_path):
190+
r, nid = _build_classes(tmp_path)
191+
assert nid["Async"] in _bound_targets(r, "Effectful", nid)
192+
193+
194+
def test_enum_context_bound_emits_reference(tmp_path):
195+
r, nid = _build_classes(tmp_path)
196+
assert nid["Ordering"] in _bound_targets(r, "Ranked", nid)
197+
198+
199+
def test_class_without_context_bound_emits_no_bound_edge(tmp_path):
200+
r, nid = _build_classes(tmp_path)
201+
assert not _bound_targets(r, "Plain", nid)
202+
assert not _bound_targets(r, "Upper", nid)
203+
204+
205+
def test_class_context_bound_keeps_constructor_param_as_field(tmp_path):
206+
# The bound must not displace the class_parameters walk: `xs: List[A]`
207+
# still references List under the `field` context.
208+
r, nid = _build_classes(tmp_path)
209+
refs = {(e["target"], e.get("context")) for e in r["edges"]
210+
if e["relation"] == "references" and e["source"] == nid["Sorted"]}
211+
assert (nid["List"], "field") in refs
212+
assert (nid["List"], "type_bound") not in refs
213+
214+
215+
def test_member_bound_is_not_hoisted_to_the_enclosing_class(tmp_path):
216+
r, nid = _build_classes(tmp_path)
217+
assert nid["Ordering"] in _bound_targets(r, ".member()", nid)
218+
assert not _bound_targets(r, "Holder", nid)
219+
220+
221+
def test_class_bound_edge_is_tagged_extracted_with_a_location(tmp_path):
222+
r, nid = _build_classes(tmp_path)
223+
edges = [e for e in r["edges"]
224+
if e["relation"] == "references"
225+
and e["source"] == nid["Sorted"]
226+
and e.get("context") == "type_bound"]
227+
assert edges
228+
for e in edges:
229+
assert e.get("confidence") == "EXTRACTED", e
230+
assert e.get("source_location") == "L7", e

0 commit comments

Comments
 (0)