Skip to content

Scala given instances are invisible to structural extraction — scope narrower than it first appears, one genuine design question for the anonymous case #2047

Open

Description

@sub4biz

Summary

given_definition genuinely has no adapted precedent anywhere else in graphify's extractors — this issue
narrows the problem to something smaller than it first appears, and presents the one part that's still a
real, undecided design call rather than presenting a single answer as settled.

given_definition is two different things, not one gap

given intOrdering: Ordering[Int] = Ordering.Int                    // (1) named, term-level
given_definition
  identifier "intOrdering"
  generic_type "Ordering[Int]"
  = field_expression "Ordering.Int"
given stringCodec: Codec[String] with                              // (2) named, class-like
  def encode(s: String): String = s
  def decode(s: String): String = s
given_definition
  identifier "stringCodec"
  generic_type "Codec[String]"
  with
  with_template_body
    function_definition "encode"
    function_definition "decode"
given [A](using ord: Ordering[A]): Ordering[List[A]] with           // (3) anonymous, class-like
  def compare(x: List[A], y: List[A]): Int = 0
given_definition
  type_parameters [A]
  parameters (using ord: ...)
  generic_type "Ordering[List[A]]"
  with
  with_template_body
    function_definition "compare"

The discriminator is the presence of a with/with_template_body child, not anything else. Form (1) —
no with_template_body — is a pure value binding: a name, a type annotation, an = and an expression. It has
no members of its own, nothing for anything to attach to as a "container." This is not the
container-invisibility gap it first appears to be — it's the same val/implicit val/given value-binding
gap already documented in the main missing-node-types issue
(the one noting given is strictly more
invisible than implicit val, leaking zero trace vs. one stray references edge). Re-scoping form (1) there
instead of here avoids describing the same root cause twice under two issues.

Forms (2) and (3) — the ones with with_template_body — are the real "container" gap, structurally the
closest thing to a class/trait body Scala has for given, and genuinely never produce any node or
member attribution today (their function_definition children leak to file scope, same symptom as the
extension/enum findings in the companion issue).

Form (3) has no name at all — the actual open design question

Form (2) has a direct identifier child (stringCodec) — exactly the shape the existing
name_fallback_child_types=("identifier",) config already looks for, so a class-like named given could, in
principle, slot into the same kind of handling as trait/enum.

Form (3) has no identifier child anywhere — anonymous givens (given [A](using ...): Ordering[List[A]] with ...) are the idiomatic Scala 3 style precisely because the instance is looked up by type, not by
name, so omitting the name is common in real code, not a rare edge case. There is nothing resembling a name
node for this shape to fall back to. Checked whether any other language handled by this extractor has a
precedent for "this node type sometimes has no name at all, needs a synthesized one" — found none; this is a
genuinely new situation for this extraction model, not an adaptation.

Two candidate approaches for the anonymous case — neither has a working precedent

Option 1 — synthesize a name/id from the provided type. For given [A](using ...): Ordering[List[A]] with ..., take the type after : (Ordering[List[A]]) as the label source — e.g. label "given Ordering[List[A]]", id derived from a sanitized form of that text plus the defining line (to disambiguate
two anonymous givens for the same type in one file, which is legal Scala as long as they're in different
scopes). Pro: every anonymous given still gets some discoverable node, keeping its methods (compare) off
file scope. Con: the id is derived from a type expression's text, not an identifier — more fragile to
formatting differences (Ordering[List[A]] vs Ordering[ List[A] ]) than every other id in this extractor,
which are all derived from source identifiers.

Option 2 — treat anonymous given as out of scope for a node of its own; only fix named givens (form
2).
Simpler, and avoids inventing a new id-construction convention with no precedent — but leaves exactly
the idiomatic-Scala-3 case (anonymous instances) still leaking to file scope, which is arguably the more
common real-world shape for given in cats-effect-style code (typeclass instances are usually looked up by
type, so are very often left anonymous).

Not picking one — this is a real product/design decision (does an approximate, type-derived id/label pull
its weight, or is "no node for anonymous givens" an acceptable, explicitly-documented limitation) that
belongs to whoever owns the extractor's id-construction conventions, not something to decide unilaterally in
an issue proposal.

What's deliberately not included here

  • Form (1) (term-level given) — re-scoped to the main missing-node-types issue's existing val/given
    finding, not duplicated here.
  • No proposed diff for form (2)/(3) container handling itself — Option 1 vs Option 2 changes what that diff
    would even do, so writing one now would bake in an unmade decision. Once the naming question is resolved,
    the actual container-attachment mechanics would reuse the same name_resolver hook design proposed in the
    companion enum-case/extension issue (Option B there) rather than inventing a third mechanism —
    given_definition and extension_definition share the "name isn't a plain field/direct child in every
    case" problem, so a single generalized hook could serve both if the maintainer takes that route.

Additional edge cases checked, not left as assumptions

given Ordering[Int] = Ordering.Int
HAS_ERROR: False
given_definition
  generic_type "Ordering[Int]"
  = field_expression "Ordering.Int"

Fully anonymous term-level given (no identifier, no with) parses cleanly with no name node of any
kind — confirming this variant also falls into the already-documented val/given gap; that finding's scope
doesn't depend on the binding having a name, so this needed no separate handling either.

given listOrdering[A](using ord: Ordering[A]): Ordering[List[A]] with
  def compare(x: List[A], y: List[A]): Int = 0
HAS_ERROR: False
given_definition
  identifier "listOrdering"
  type_parameters [A]
  parameters (using ord: Ordering[A])
  generic_type "Ordering[List[A]]"
  with
  with_template_body
    function_definition "compare"

A named, class-like given with both type parameters and a using clause together parses cleanly, with
identifier "listOrdering" present as expected — confirming the with_template_body discriminator holds
regardless of how many other constructs (generics, using params) are layered onto the same given.

Environment

  • graphifyy 0.9.20, tree_sitter_scala 0.26.0
  • All forms and edge cases in this file were dumped directly against the installed grammar
    (tree_sitter.Parser + tree_sitter_scala.language()), checked for absence of ERROR/is_missing nodes,
    not inferred from grammar docs.

Activity

  1. sub4biz commented on Nov 15, 2026

    @sub4biz
    Author

    Retested on clean upstream v0.9.109. Both named and anonymous class-like givens now get a container and their methods attach to it. Thank you!

    The anonymous-name disambiguation discussed in this issue still appears to have a gap. These two givens are in different objects and the source compiles with Scala 3, but extract_scala produces one given Ordering node and one .compare() node: both objects' contains edges point to that same given node.

    object One:
      given Ordering[Int] with
        def compare(x: Int, y: Int): Int = 1
    object Two:
      given Ordering[Int] with
        def compare(x: Int, y: Int): Int = 2

    Reproduction (run from a clean checkout with the Scala grammar installed):

    from pathlib import Path
    from tempfile import TemporaryDirectory
    from graphify.extract import extract_scala
    
    source = """object One:
      given Ordering[Int] with
        def compare(x: Int, y: Int): Int = 1
    object Two:
      given Ordering[Int] with
        def compare(x: Int, y: Int): Int = 2
    """
    with TemporaryDirectory() as tmp:
        path = Path(tmp) / "example.scala"
        path.write_text(source, encoding="utf-8")
        graph = extract_scala(path)
        labels = {n["id"]: n["label"] for n in graph["nodes"]}
        givens = [n for n in graph["nodes"] if n["label"] == "given Ordering"]
        methods = [n for n in graph["nodes"] if n["label"] == ".compare()"]
        targets = {e["target"] for e in graph["edges"]
                   if e["relation"] == "contains"
                   and labels.get(e["source"]) in {"One", "Two"}
                   and labels.get(e["target"]) == "given Ordering"}
        print("given nodes:", len(givens))
        print("compare nodes:", len(methods))
        print("distinct given targets:", len(targets))

    Observed: 1, 1, 1. Expected: two distinct given nodes and two distinct method nodes, each attached to its own object.

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