Skip to content

fix(scala): dispatch self-type annotations to requires edges (#2052) - #2245

Closed
Yyunozor wants to merge 1 commit into
Graphify-Labs:v8from
Yyunozor:fix/2052-scala-self-type-requires
Closed

fix(scala): dispatch self-type annotations to requires edges (#2052)#2245
Yyunozor wants to merge 1 commit into
Graphify-Labs:v8from
Yyunozor:fix/2052-scala-self-type-requires

Conversation

@Yyunozor

Copy link
Copy Markdown
Contributor

Fixes #2052

Bug

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.

Fix

One new dispatch branch in graphify/extractors/engine.py, next to val_definition/var_definition. self_type's children carry no field names, so the type node is found positionally: binder identifier first, type (when present) second. self => binds a name with no type, so len(named) < 2 correctly yields no type node instead of misreading the binder. _scala_collect_type_refs already handles every shape the type position can take (type_identifier, compound_type for with, refinement bodies) — reused unchanged, matching the issue's proposed diff. Edges use a new requires relation, following the issue's own proposal; the label stays your call per its open question.

Also added requires to DEFAULT_AFFECTED_RELATIONS (graphify/affected.py), mirroring how indirect_call was wired in when introduced, so graphify affected follows it like inherits/mixes_in/embeds. Verified end-to-end with graphify update . + graphify affected on a toy project, not just direct extractor calls.

Known limitation

Self-types on trait bodies — this issue's own cake-pattern example — don't yet produce edges. Tracing the dispatch directly showed trait_definition isn't in _SCALA_CONFIG.class_types (only class_definition/object_definition are), so traits aren't class-like containers at all yet: parent_class_nid stays None inside a trait body today (extends_clause heritage is equally absent there, same cause). This dispatch is container-agnostic, so it will automatically cover trait self-types once trait recognition lands — already proposed separately in #1792 — no further change needed here. class/object self-types already work today (tested below).

Tests

New tests/test_scala_self_type.py (8 tests, own fixture — avoids colliding with other open Scala PRs' shared-fixture edits): single type, with-compound, structural refinement (base type only, matching how refinement bodies are unscanned elsewhere), binder-only self => (no edge), a plain class without a self-type (no spurious edge), coexistence with an unrelated extends, edge shape (no context), and an affected-traversal consumer test.

Full suite identical on branch and bare v8: same 6 pre-existing environment-dependent failures (missing openai extra, one wheel-build test) both sides, 8 net new passes. Ruff clean.

…y-Labs#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).
@safishamsi

Copy link
Copy Markdown
Collaborator

Thanks @Yyunozor. Shipped in v0.9.29 (cherry-picked to v8). Note: the requires-edge emission fires for self-types in a class/object; the trait cake-pattern case (#2052) additionally needs trait-as-class recognition, tracked in #2247. Closed-unmerged here, but it's in the release: https://github.com/Graphify-Labs/graphify/releases/tag/v0.9.29

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

Labels

None yet

Projects

None yet

2 participants