Repository navigation
Description
Summary
Context bounds are the standard sugar for typeclass-parameter wiring in idiomatic Scala (arguably more common
in modern code than the desugared (implicit ...) form). grep -n "context_bound" engine.py returns zero
matches anywhere in the installed graphifyy 0.9.20 — the node type is never referenced by the extraction
code, so a type parameter's constraint (Ordering, Async, Temporal) is invisible regardless of where else
in the file it appears.
Verified shape
def sort[A: Ordering](xs: List[A]): List[A] = xsfunction_definition
identifier "sort"
type_parameters
identifier "A"
context_bound
type_identifier "Ordering"
parameters ...
def program[F[_]: Async: Temporal](x: F[Int]): F[Int] = xfunction_definition
identifier "program"
type_parameters
identifier "F"
type_parameters <- nested, for F's own [_] arity
wildcard "_"
context_bound
type_identifier "Async"
context_bound
type_identifier "Temporal"
parameters ...
context_bound is a direct child of type_parameters, one per bound, sitting alongside the type
parameter's own identifier — not nested inside parameters (that's a separate, already-diagnosed
curried-parameter-list gap in graphify#2042; this is
a distinct root cause, confirmed by the differing AST shape, not a restatement of it).
Where this slots in — existing code, no new handler needed
engine.py already has a per-function, per-ts_module block that walks a Scala function's parameters for
type references and emits references edges (engine.py:3435-3465, using the existing
_scala_collect_type_refs helper). Context bounds just need the same treatment, added to the same block,
right before it falls through to return_node handling:
if config.ts_module == "tree_sitter_scala":
params_node = None
for c in node.children:
if c.type == "parameters":
params_node = c
break
if params_node is not None:
for p in params_node.children:
... # existing parameter-type handling, unchanged
# New: context bounds (`[A: Ordering]`, `[F[_]: Async: Temporal]`)
type_params_node = None
for c in node.children:
if c.type == "type_parameters":
type_params_node = c
break
if type_params_node is not None:
for tp in type_params_node.children:
if tp.type != "context_bound":
continue
refs: list[tuple[str, str]] = []
for sub in tp.children:
if sub.is_named:
_scala_collect_type_refs(sub, source, False, refs)
for ref_name, role in refs:
ctx = "generic_arg" if role == "generic_arg" else "context_bound"
target_nid = ensure_named_node(ref_name, line)
if target_nid != func_nid:
add_edge(func_nid, target_nid, "references", line, context=ctx)
return_node = node.child_by_field_name("return_type")
... # existing, unchangedNo new dispatch site, no LanguageConfig change — reuses _scala_collect_type_refs, ensure_named_node, and
add_edge exactly as the existing parameter/return-type handling already does. The only new thing at this
point is the context label on the edge ("context_bound" proposed above, distinguishing it from
"parameter_type"/"return_type"/"generic_arg" — naming is the maintainer's call). The flat scan shown
above is not the final proposal, though — see the next section, which replaces it with a small recursive
helper once a gap in this flat version is confirmed.
A flat single-level scan misses a real, valid construct
Form (1) — single bound, unnested:
def program[F[_]: Functor]: F[Int] = ???type_parameters
identifier "F"
type_parameters (F's own arity)
wildcard "_"
context_bound
type_identifier "Functor"
Form (2) — bound on F's own higher-kinded parameter:
def program[F[_[_]: Functor]]: Int = 0type_parameters
identifier "F"
type_parameters (F's own arity)
wildcard "_"
type_parameters (nested one level deeper)
wildcard "_"
context_bound <- ONE LEVEL DEEPER than form (1)
type_identifier "Functor"
This shape (used for e.g. Free[S[_]: Functor, A]-style tagless-final code in the cats-effect/cats-free
ecosystem) is confirmed real, not a rare invented edge case — a flat single-level scan (for tp in type_params_node.children: if tp.type != "context_bound": continue) genuinely misses it, since
context_bound sits one level deeper than form (1)'s.
Fix: recurse into any nested type_parameters children instead of scanning only the immediate level:
def _scala_collect_context_bounds(type_params_node, source, refs_out):
for tp in type_params_node.children:
if tp.type == "context_bound":
for sub in tp.children:
if sub.is_named:
_scala_collect_type_refs(sub, source, False, refs_out)
elif tp.type == "type_parameters":
_scala_collect_context_bounds(tp, source, refs_out) # recurse into nested arityThis replaces the inline for tp in type_params_node.children: if tp.type != "context_bound": continue loop
in the previous section's code block — call _scala_collect_context_bounds(type_params_node, source, refs)
in its place, then emit references edges from refs exactly as shown there.
Verified against 4 shapes, not just reasoned through — ran this exact recursive logic against real parsed
trees for all four:
def sort[A: Ordering](xs: List[A]): List[A] = xs // -> ['Ordering']
def program[F[_]: Async: Temporal](x: F[Int]): F[Int] = x // -> ['Async', 'Temporal'] (flat multi-bound, unaffected by the recursion change)
def program2[F[_[_]: Functor]]: Int = 0 // -> ['Functor'] (higher-kinded nested — the case a flat scan would miss; confirms the recursive fix closes the gap)
extension [A: Ordering](xs: List[A]) // -> ['Ordering'] (confirms the extension cross-dependency below is real)
def maxOf: A = xs.maxIntegration point with extension_definition — not independent
extension [A: Ordering](xs: List[A])
def maxOf: A = xs.maxextension's own type parameters use the identical context_bound shape as a function's — running the same
_scala_collect_context_bounds logic above against this snippet resolves ['Ordering'], same as sort[A: Ordering] does. Since this fix is scoped to the function-handling block (a function_types-only code path)
and extension_definition is handled by a separate branch (see
graphify#2044, since extension_definition isn't a
function_types member), this fix does not automatically cover extension's own type parameters — the
extension handler needs to call the same _scala_collect_context_bounds helper independently, or
extension's bounded type parameters stay invisible even after this diff lands.
Open questions before landing
- Should
context_bound's edge context be a new label ("context_bound", proposed above) or reuse
"parameter_type"for consistency with how a desugared(implicit ev: Ordering[A])parameter would be
labeled? Reusing the existing label makes the two desugarings of the same concept indistinguishable in the
graph; a distinct label preserves that a context bound was used syntactically. No strong opinion.
Environment
- graphifyy 0.9.20, tree_sitter_scala 0.26.0
- All parse-tree dumps and the proposed recursive logic in this file were run directly against the installed
tree_sitter_scalagrammar, not inferred from grammar docs.
Activity
fix(extract): resolve Scala context bounds to references edges (#2046)
feat(scala): extract class-level context bounds
fix(extract): drop Scala generic type parameters from references
omsenjalia commentedon Oct 18, 2026
This looks fixed on the
v8branch by commits 836fb8ff (fix(extract): drop Scala generic type parameters from references), ee292729 (feat(scala): extract class-level context bounds), 562dfd6d (fix(extract): resolve Scala context bounds to references edges (#2046)). Could a maintainer confirm and close?sub4biz commentedon Nov 15, 2026
Retested on clean upstream v0.9.109. The function cases in this report now emit references to
Ordering,Async,Temporaland the nested higher-kindedFunctor. Thanks for the fixes!The extension-method example in the report still has a gap: on
extract_scala, the following defines.maxOf(), but emits no reference toOrdering:It may be useful to keep this issue open for that extension-specific case.