feat(kernel): R7b Scala walker — scala module, vendored-grammar-C master@0aca5d0a6f, scala default-routed (#1385)

R7b batch 4 #3 (docs/design/scala-kernel-port-checklist.md is the
authoritative quirk list). The third vendored-grammar-C language and the
biggest grammar in the tree (35MB parser.c): the vendored wasm is
tree-sitter/tree-sitter-scala master@0aca5d0a6f — a post-v0.26.0 generation
sync that is not a release (the 0.26.0 crate is 30 states BEHIND, so a
crate pin would be a silent downgrade). NO wasm change: production has
parsed with this exact revision since #91 — the kernel-grammar-parity row
(ABI 15, 26,650 states, 32 fields, id-by-id tables) is the whole alignment
proof.

Preserved bug-for-bug (all probe-pinned): the leak-through asymmetries —
extension methods mint NO nodes (first def's body calls leak to the
enclosing scope, later defs invisible, and the braced form resolves its
body field to the `{` TOKEN via first-match-wins field lookup → whole
extension invisible); anonymous `new T { … }` template_body members leak to
the enclosing scope (findAnonymousClassBody misses template_body); the
bodied-vs-bodiless class asymmetry (bodiless headers walk class_parameters
→ default-value calls emit FROM the class; bodied ones never see them) —
plus first-segment import names (`import com.example.C` → `com`), the
val/var hook keyed on the enclosing-definition NODE TYPE (object vals →
constants/value-ref targets, class/trait/enum/given vals → fields) with
consumed initializers, every def routed through extractMethod with the
top-level function fallback, nested defs in bodies minting NOTHING (the
inverse of kotlin) while body-local classes extract fully, curried
signatures keeping only the FIRST parameter list (type params win the
`parameters` field), enum cases positioned at the CASE node with invisible
params/extends tails, extends with-chains via scalaBaseTypeName,
`@deprecated(args)` decorates, the #750 capitalized-chain re-encode
(`WidgetS.create().render`), literal-receiver silence, static-member reads
AND writes, infix invisibility, `derives` silence, scaladoc retention with
the CRLF `\r` pin, full value-reference machinery (shadow prune, last-wins
same-name targets, `$X`/`${X}` interpolation reads), and SCALA_SPEC
fn-refs (bare ids + postfix eta unwrap + varinit, var-init non-capture).

Gates: parity sweeps first-run 0-diff on os-lib/cats/scala3-compiler-src/
scala3-library-src — 1,935 clean files byte-parity, deferrals 0/15/57/116
matching the survey's predictions exactly (scala-3's PHANTOM hasError
files — flag-true, zero ERROR nodes, capture-checking `^` — defer on the
FLAG); full-init dumps byte-identical ×3 (os-lib, cats, scala3 whole-repo
950,889 dump lines); kernel-scala-parity suite (9 fixtures + 9 in-memory
CRLF variants incl. Scala-3 indentation through the external scanner +
phantom/real-error defer pins + first-segment/namespace/value-ref pins);
full suite 2,669 green ×3 with CODEGRAPH_KERNEL_EXPECT=1
(kernel-scaffold's stays-wasm example moved scala → pascal).
DEFAULT_ROUTED += scala (19 langs).

Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
Colby MchenryandClaude Fable 5 authored and GitHub committed 2026-07-20 19:02:52 -05:00
1 parent e32135171e
commit bdd687b49f
27 files changed
+1203380 -9

No files matched your search

@@ -0,0 +1,28 @@
/** Scaladoc kept?
* second line with star
*/
def scaladocOnly(): Int = 1
// line one
// line two
def lineRun(): Int = 2
/** block doc */
// trailing line
def mixedBlockThenLine(): Int = 3
// leading line
/** block after line */
def mixedLineThenBlock(): Int = 4
// detached
def blankLineBetween(): Int = 5
/** class doc */
class DocClass {
/** member doc */
def documented(): Int = 6
/** val doc */
val docVal = 7
}
@@ -0,0 +1,17 @@
extension (s: String)
def doubleUp: String = concat(s, s)
def tripleUp: String = concat2(s, concat3(s, s))
extension (t: Int) {
def bracedA: Int = callA(t)
def bracedB: Int = callB(t)
}
extension [A](xs: List[A]) def secondOpt: Option[A] = xs.drop(1).headOption
extension (t: Int) {
def bracedA: Int = callA(t)
def bracedB: Int = callB(t)
}
extension (t: Int) {
def bracedA: Int = callA(t)
}
@@ -0,0 +1,27 @@
object Handlers {
def handler(): Unit = ()
def other(): Unit = ()
def wire(): Unit = {
register(handler)
registerEta(other _)
val stored = handler
obj.cb = other
this.cb = cb
register(missing)
}
val topStored = handler
var cb: () => Unit = other
}
object H2 {
def alpha(): Unit = ()
def beta(): Unit = ()
def gamma(): Unit = ()
def delta(): Unit = ()
def wire(): Unit = {
val stored = alpha
obj.cb = beta
val eta = gamma _
sink.handler = sink.handler
}
val listStored = List(delta)
}
@@ -0,0 +1,27 @@
object Registry {
given regOrd: Ordering[Int] = new Ordering[Int] {
def compare(a: Int, b: Int): Int = cmpHelper(a, b)
val innerVal = 5
}
def member(): Int = 1
}
given topOrd: Ordering[Long] = new Ordering[Long] {
def compare(a: Long, b: Long): Int = cmpTop(a, b)
val topInnerVal = 6
}
class BodyStmt(size: Int) {
require(size > 0)
logInit(this)
val fnField = (x: Int) => runLam(x)
}
object Boot extends App {
bootUp()
def local(): Unit = ()
}
object NamedArgs {
def cbTarget(): Unit = ()
def sink(): Unit = {
wire(cb = cbTarget)
forward(cb = cb)
}
}
@@ -0,0 +1,25 @@
package com.ex
object Indented:
val member = compute()
def m(): Int =
val local = helperI()
local + 1
class IndentedC extends BaseI:
def n(): Int = member
enum ColorI:
case Red, Green
def label(): String = "x"
trait TraitI:
def tm(): Int
given ordI: Ordering[Int] = summonOrd()
extension (s: String)
def extI: Int = s.length
def topI(): Int =
if cond then one() else two()
@@ -0,0 +1,29 @@
package a.b {
class Nested1 { def nm(): Int = 1 }
}
trait SelfTyped { self: Nested1 =>
def stm(): Int = 2
}
class Outer2 {
type Member = List[Int]
object InnerObj { val IC = 1 }
class InnerCls { def icm(): Int = 2 }
}
private[b] class QualPriv
class `Weird Name` { def `strange def`(): Int = 3 }
val multiA, multiB = 5
class CurryCall {
def run(): Unit = {
curried(1)(2)
Foo(1).bar()
Foo(1)(2)
new Widget(make())
}
}
class A extends Base(1)(2) { def m(): Int = 1 }
import single
import a.b
object Uni {
val note = "ééé €€ 😀 end"
def afteré(): Int = { helperé() }
}
@@ -0,0 +1,4 @@
val scriptVal = 10
def scriptFn(): Int = scriptVal + 1
println(scriptFn())
runTop(scriptVal)
@@ -0,0 +1,19 @@
object Config {
val RETRY_MAX = 3
val TIMEOUT_MS = 500
val SHARED_TABLE = Map("a" -> 1)
val count = 9
}
val TOP_LIMIT = 99
class Reader {
def readTop(): Int = TOP_LIMIT + 1
def readBoth(): Int = {
val RETRY_MAX = 9
RETRY_MAX + Config.TIMEOUT_MS
}
def readInterp(): String = s"limit $TIMEOUT_MS and ${SHARED_TABLE}"
def readCount(): Int = count
}
object Config2 {
val TIMEOUT_MS = 1
}
@@ -0,0 +1,177 @@
package com.example.torture
import com.example.other.OtherClass
import com.example.multi.{Alpha, Beta}
import com.example.wild._
import com.example.star.*
import com.example.alias.{LongName => Short}
/** Scaladoc for topLevel — kept? */
def topLevel(a: Int): WidgetS = new WidgetS(a)
// line one
// line two
def lineDoc(): Int = 1
def curried(a: Int)(b: String)(implicit ord: Ordering[Int]): Int = a
def genericDef[A: Numeric, B <: BoundT](x: A): B = ???
def inferredRet(a: Int) = a + 1
def unitRet(): Unit = ()
def genericLeak[T](t: T): T = t
def qualRet(w: WidgetS): com.example.other.Remote = ???
private def privTop(): Int = 1
@main def entry(): Unit = topLevel(1)
@deprecated("gone", "1.0") def old(): Int = 0
val topVal: Int = 3
var topVar = 4
lazy val topLazy = compute()
val (tupA, tupB) = (1, 2)
val Some(extracted) = Option(9)
val SHARED_TABLE: Map[String, Int] = Map.empty
val topInit = WidgetS.create()
class WidgetS(val size: Int, label: String = defaultLabel()) extends BaseW(size) with Drawable with Ordered[WidgetS] {
val cachedName: String = "w"
var mutable = 0
private val secret = 3
@volatile var annotatedField: Int = 0
def this() = this(0, "d")
def render(): RenderResult = {
val local = helperR(size)
Registry.register(this)
new RenderResult(local)
}
def +(other: WidgetS): WidgetS = new WidgetS(size + other.size)
def compare(that: WidgetS): Int = size - that.size
@inline def fast(): Int = 1
protected def hook(): Unit = ()
}
object WidgetS {
val DEFAULT_SIZE = 10
def create(): WidgetS = new WidgetS(DEFAULT_SIZE, "c")
def readDefault(): Int = DEFAULT_SIZE + 1
}
case class DataS(x: Int, y: String = mkY())
case object SingletonS extends MarkerT
abstract class AbsS {
def abstractM(): Int
def concrete(): Int = abstractM() + 1
}
trait Drawable {
def draw(): Unit
def area(): Double = 0.0
val traitVal: Int = 7
}
sealed trait Shape
object Circle extends Shape
class Square extends Shape
trait MarkerT
class DelegatedImpl(d: Drawable) extends Drawable {
def draw(): Unit = d.draw()
}
enum Http {
case Ok, NotFound
case Custom(code: Int)
def describe(): String = s"code"
}
enum Planet(mass: Double) {
case Earth extends Planet(5.9)
case Mars extends Planet(0.6)
}
given intOrd: Ordering[WidgetS] = new Ordering[WidgetS] {
def compare(a: WidgetS, b: WidgetS): Int = a.size - b.size
}
given String = "anon"
extension (s: String)
def doubleUp: String = concat(s, s)
def tripleUp: String = concat(s, concat(s, s))
extension [A](xs: List[A]) def secondOpt: Option[A] = xs.drop(1).headOption
implicit class RichIntS(val i: Int) {
def twice: Int = i * 2
}
type WidgetList = List[WidgetS]
opaque type Meters = Double
object CallSites {
def run(w: WidgetS): Unit = {
helper(1)
w.render()
WidgetS(1)
WidgetS.create().render()
lowerFactory().chain()
a.b.method3()
this.mine()
super.hashCode()
"lit".toUpperCase()
5.toString()
genericCall[Int](1)
val n2 = new Ordering[Int] { def compare(p: Int, q: Int): Int = p - q }
val s1 = s"plain $topVal and ${w.render()} end"
val e1 = registerCb(handler _)
val e2 = handler _
registerCb(handler)
val r1 = list map transform
val r2 = 1 :: rest
w match {
case ws: WidgetS => use(ws)
case _ => ()
}
for { x <- xs; y <- ys if y > 0 } yield combineXY(x, y)
}
def mine(): Int = 1
def handler(): Unit = ()
def localHost(): Int = {
def innerFn(k: Int): Int = k * 2
val lam = (q: Int) => q + 1
class LocalClass { def lm(): Int = 3 }
object LocalObj { def om(): Int = 4 }
innerFn(lam(1)) + helperCall()
}
}
object StaticReads {
def reads(): Unit = {
val c1 = Registry.count
val c2 = Http.Ok
val c3 = com.example.Fq.CONST_READ
Registry.count = 5
process(Registry.count)
Registry.register(null)
}
}
package object utilpkg {
def pkgHelper(): Int = 1
val pkgShared = 2
}
+1 -1
View File
@@ -36,7 +36,7 @@ const kernelBuilt = fs.existsSync(KERNEL_PATH);
// Every kernel-capable language. `jsx` shares the javascript grammar on BOTH
// paths (langs.rs mirrors WASM_GRAMMAR_FILES), so the distinct grammars are:
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go', 'c', 'cpp', 'rust', 'csharp', 'ruby', 'php', 'swift', 'kotlin', 'r', 'lua', 'luau'];
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go', 'c', 'cpp', 'rust', 'csharp', 'ruby', 'php', 'swift', 'kotlin', 'r', 'lua', 'luau', 'scala'];
describe.skipIf(!kernelBuilt)('kernel↔wasm grammar parity', () => {
beforeAll(async () => {
+3 -3
View File
@@ -73,11 +73,11 @@ describe.skipIf(!kernelBuilt)('kernel scaffold', () => {
});
it('TS/JS family + Java + Python + Go route to the kernel by default; others stay wasm', () => {
for (const lang of ['typescript', 'tsx', 'javascript', 'jsx', 'java', 'python', 'go', 'ruby', 'php', 'swift', 'kotlin'] as const) {
for (const lang of ['typescript', 'tsx', 'javascript', 'jsx', 'java', 'python', 'go', 'ruby', 'php', 'swift', 'kotlin', 'scala'] as const) {
expect(kernelRoutes(lang), lang).toBe(true);
}
expect(kernelRoutes('scala')).toBe(false);
expect(tryKernelExtract('src/a.scala', 'object A { def f(): Int = 1 }\n', 'scala')).toBeNull();
expect(kernelRoutes('pascal')).toBe(false);
expect(tryKernelExtract('src/a.pas', 'program A;\nbegin\nend.\n', 'pascal')).toBeNull();
// CODEGRAPH_KERNEL_LANGS REPLACES the default set when present.
process.env.CODEGRAPH_KERNEL_LANGS = 'tsx';
expect(kernelRoutes('typescript')).toBe(false);
+164
View File
@@ -0,0 +1,164 @@
/**
* Kernel↔wasm Scala extraction parity (R7b batch 4 of the kernel migration).
*
* Asserts the native walker (codegraph-kernel/src/scala.rs) produces the SAME
* ExtractionResult as the wasm TreeSitterExtractor — nodes, edges, and
* unresolved refs compared as canonicalized multisets — over the checked-in
* fixtures (torture.scala: first-segment imports, defs-as-methods with the
* top-level function fallback, curried/type-params-first signatures, the
* val/var hook with object-vs-class kinds and initializer invisibility,
* companion pairs sharing a QN namespace, the bodiless-header asymmetry,
* enum cases at case-node positions with invisible tails, extends
* with-chains, `@deprecated(args)` decorates, the #750 capitalized-chain
* re-encode, literal-receiver silence, static reads incl. the write-LHS
* emission, nested-def invisibility with body-local classes extracting
* fully; TortureDocs: scaladoc retention + the CRLF `\r` pin; TortureVref:
* value-ref targets, shadow prune, interpolation reads, the last-wins
* mis-target; TortureFnref: all five capture channels + var-init
* non-capture + eta expansion; TortureGiven/TortureExt: the anon-body and
* extension leak asymmetries — the port's likeliest regression sites;
* TortureIndent: Scala-3 indentation syntax through the external scanner;
* TortureMisc: package objects/braced packages/self-types/super-ctor args/
* unicode columns; TortureScript.sc: top-level statements from the FILE)
* and their CRLF variants (derived in-memory — #1329), plus phantom and
* real-error defer pins.
*
* The full-repo sweeps live in scripts/kernel-parity.mjs (os-lib/cats +
* scala3 compiler/src + library/src with --max-deferral 0.3); this suite
* keeps the invariant alive in `npm test`. Skips when no kernel binary is
* staged; CODEGRAPH_KERNEL_EXPECT=1 turns that into a failure.
*/
import { describe, it, expect, beforeAll, beforeEach, afterEach } from 'vitest';
import * as fs from 'fs';
import * as path from 'path';
import { extractFromSource } from '../src/extraction';
import { initGrammars, loadGrammarsForLanguages } from '../src/extraction/grammars';
import { tryKernelExtract, resetKernelForTests } from '../src/extraction/kernel';
import type { ExtractionResult } from '../src/types';
const KERNEL_PATH = path.join(
__dirname,
'..',
'codegraph-kernel',
'prebuilds',
`${process.platform}-${process.arch}`,
'codegraph-kernel.node'
);
const kernelBuilt = fs.existsSync(KERNEL_PATH);
const FIXTURE_DIR = path.join(__dirname, 'fixtures', 'kernel-parity');
function canon(result: ExtractionResult): { nodes: string[]; edges: string[]; refs: string[] } {
return {
nodes: result.nodes
.map(({ updatedAt: _u, ...n }) => JSON.stringify(n, Object.keys(n).sort()))
.sort(),
edges: result.edges.map((e) => JSON.stringify(e, Object.keys(e).sort())).sort(),
refs: result.unresolvedReferences
.map((r) => JSON.stringify(r, Object.keys(r).sort()))
.sort(),
};
}
const ENV_KEYS = ['CODEGRAPH_KERNEL', 'CODEGRAPH_KERNEL_LANGS'] as const;
let savedEnv: Record<string, string | undefined>;
describe.skipIf(!kernelBuilt)('kernel Scala extraction parity', () => {
beforeAll(async () => {
await initGrammars();
await loadGrammarsForLanguages(['scala']);
});
beforeEach(() => {
savedEnv = Object.fromEntries(ENV_KEYS.map((k) => [k, process.env[k]]));
resetKernelForTests();
});
afterEach(() => {
for (const k of ENV_KEYS) {
if (savedEnv[k] === undefined) delete process.env[k];
else process.env[k] = savedEnv[k];
}
resetKernelForTests();
});
function assertParity(filePath: string, source: string, minNodes = 2): ExtractionResult {
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
delete process.env.CODEGRAPH_KERNEL;
const viaKernel = tryKernelExtract(filePath, source, 'scala');
expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
process.env.CODEGRAPH_KERNEL = '0';
const viaWasm = extractFromSource(filePath, source, 'scala');
delete process.env.CODEGRAPH_KERNEL;
const k = canon(viaKernel!);
const w = canon(viaWasm);
expect(k.nodes, `${filePath}: nodes`).toEqual(w.nodes);
expect(k.edges, `${filePath}: edges`).toEqual(w.edges);
expect(k.refs, `${filePath}: refs`).toEqual(w.refs);
expect(viaWasm.nodes.length).toBeGreaterThanOrEqual(minNodes);
return viaKernel!;
}
const FIXTURES = [
['torture.scala', 40],
['TortureDocs.scala', 3],
['TortureVref.scala', 4],
['TortureFnref.scala', 4],
['TortureGiven.scala', 3],
['TortureExt.scala', 1],
['TortureIndent.scala', 3],
['TortureMisc.scala', 4],
['TortureScript.sc', 1],
] as const;
for (const [file, minNodes] of FIXTURES) {
it(`${file}: parity`, () => {
const src = fs.readFileSync(path.join(FIXTURE_DIR, file), 'utf8');
assertParity(`fixtures/${file}`, src, minNodes);
});
it(`${file}: CRLF parity`, () => {
const src = fs.readFileSync(path.join(FIXTURE_DIR, file), 'utf8');
const crlf = src.replace(/(?<!\r)\n/g, '\r\n');
assertParity(`fixtures/${file} (crlf)`, crlf, minNodes);
});
}
it('torture pins: import first-segment names, companion pairs, value-ref edges', () => {
const src = fs.readFileSync(path.join(FIXTURE_DIR, 'torture.scala'), 'utf8');
const result = assertParity('fixtures/torture.scala', src, 40);
// Imports are named the FIRST path segment.
const imports = result.nodes.filter((n) => n.kind === 'import');
expect(imports.length).toBeGreaterThan(0);
expect(imports.every((n) => !n.name.includes('.'))).toBe(true);
// No namespace node, ever (package headers ignored).
expect(result.nodes.some((n) => n.kind === 'namespace')).toBe(false);
// Value-ref edges exist and are metadata-tagged.
expect(result.edges.some((e) => e.kind === 'references' && e.metadata?.valueRef === true)).toBe(
true
);
});
it('scala-3 PHANTOM hasError defers (flag-true, zero ERROR nodes)', () => {
// Capture-checking postfix `^` — a complete, correct CST whose hasError
// flag is still true. The kernel must defer on the FLAG.
const phantom = 'def f(x: List[Int]^): Int = 1\n';
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
delete process.env.CODEGRAPH_KERNEL;
expect(tryKernelExtract('src/phantom.scala', phantom, 'scala')).toBeNull();
process.env.CODEGRAPH_KERNEL = '0';
const viaWasm = extractFromSource('src/phantom.scala', phantom, 'scala');
delete process.env.CODEGRAPH_KERNEL;
expect(viaWasm.nodes.some((n) => n.kind === 'file')).toBe(true);
});
it('real parse errors defer (given-with syntax)', () => {
const broken = 'trait C\ngiven x: C with { def y = 1 }\n';
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
delete process.env.CODEGRAPH_KERNEL;
expect(tryKernelExtract('src/gw.scala', broken, 'scala')).toBeNull();
});
});