Skip to content

Scala context bounds ([A: Ordering], [F[_]: Async: Temporal]) never produce a references edge — fix slots into the existing per-function Scala block, no new handler needed #2046

Open

Description

@sub4biz

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] = xs
function_definition
  identifier "sort"
  type_parameters
    identifier "A"
    context_bound
      type_identifier "Ordering"
  parameters ...
def program[F[_]: Async: Temporal](x: F[Int]): F[Int] = x
function_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, unchanged

No 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 = 0
type_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 arity

This 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.max

Integration point with extension_definition — not independent

extension [A: Ordering](xs: List[A])
  def maxOf: A = xs.max

extension'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

  1. 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_scala grammar, not inferred from grammar docs.

Activity

  1. omsenjalia commented on Oct 18, 2026

    @omsenjalia
    Contributor

    This looks fixed on the v8 branch 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?

  2. sub4biz commented on Nov 15, 2026

    @sub4biz
    Author

    Retested on clean upstream v0.9.109. The function cases in this report now emit references to Ordering, Async, Temporal and the nested higher-kinded Functor. 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 to Ordering:

    extension [A: Ordering](xs: List[A])
      def maxOf: A = xs.max

    It may be useful to keep this issue open for that extension-specific case.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions