feat(kernel): R7b Kotlin walker — kotlin module, vendored-grammar-C build, kotlin default-routed (#1382)
Sixth R7b port — the T1½ batch finale. Checklist-first recipe
(docs/design/kotlin-kernel-port-checklist.md, 1,121 lines, dist-extractor
ground truth); parity passed FIRST RUN on all three repos.
THE NOVEL MECHANISM — vendored-grammar-C (the §4 tracker's prescription,
first use): the crates.io tree-sitter-kotlin 0.3.8 pins `tree-sitter >= 0.21,
< 0.23` (the kernel links 0.25) and tree-sitter-kotlin-ng is a DIFFERENT
grammar (8 fields vs 0, renamed kinds — extractor-breaking), so no crate dep
is possible. The fwcd 0.3.8 tag's sha-matched parser.c + scanner.c are
vendored into codegraph-kernel/grammars/kotlin and compiled by build.rs (cc),
exposed via tree-sitter-language::LanguageFn. The wasm re-vendor is
behavior-NEUTRAL (0 CST/error disagreements across 1,984 gate-repo files;
old-vs-new full-init dumps byte-identical ×3) — a reproducibility re-vendor,
ABI stays 14.
Walker firsts: extension-function receivers (getReceiverType →
`WidgetK::extend` QN OVERRIDE with no package prefix, the qualified-receiver
`com::qext` first-segment bug, and the owner-contains fallback that excludes
`interface` kinds and is source-order dependent) and extractModifiers
(expect/actual platform modifiers → the node DECORATORS wire field on every
created node — the KMP synthesizer's feed, incl. `actual typealias`).
Preserved bug-for-bug: the FIELD_COUNT-0 dead cluster (no signatures, ZERO
type-annotation refs), hook-consumed property initializers emitting nothing
(incl. `by lazy {}`), the bodiless-vs-bodied class header asymmetry, enum-
entry bodies being invisible, KDoc never a docstring AND chain-breaking,
comment-gluing into import/package extents, `@Anno(args)` emitting nothing
while `@Marker` decorates, zero instantiates refs, the paren-then-lambda
`trailing()` garbage callee, text-includes visibility/suspend false
positives, and the packaged-file value-ref target drop. The fun-interface
misparse-recovery hook is DEFER-SHIELDED (every such file has_error) and
deliberately not ported. The swift-sweep lesson pre-applied: the shared
`assignment` shadow-prune case is implemented alongside the
property_declaration case.
Gates: sweeps 0-diff okio 299/322, okhttp 531/580, kotlinx.coroutines
1031/1082 (deferrals exactly the predicted 23/49/51 — both-arm grammar
reality incl. PHANTOM hasError files with complete CSTs; the kernel trusts
the flag); full-init dumps byte-identical ×3 (46.5k/108.9k/92.3k lines); KMP
expect/actual synthesis IDENTICAL across arms (412 edges on
kotlinx.coroutines — the tracker's KMP validation); kernel-kotlin-parity
suite (torture reflowed off the phantom shapes + .kts script + CRLF variants
+ fun-interface and phantom defer pins) + kotlin grammar-parity row (the
C-build ↔ wasm table identity proof); full suite 2,633 green ×2 under
CODEGRAPH_KERNEL_EXPECT=1. DEFAULT_ROUTED += kotlin (15 langs).
Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
09e301bbfa
commit
45a53eb5b5
@@ -0,0 +1,4 @@
|
||||
val greeting = "hi"
|
||||
println(greeting)
|
||||
fun scripted() { work() }
|
||||
scripted()
|
||||
@@ -0,0 +1,267 @@
|
||||
/** KDoc for the file's package. */
|
||||
package com.example.torture
|
||||
|
||||
import com.example.other.OtherClass
|
||||
import com.example.util.helper
|
||||
import com.example.wild.*
|
||||
import com.example.alias.LongName as Short
|
||||
|
||||
// line comment run 1
|
||||
// line comment run 2
|
||||
fun topLevel(x: Int, s: String): WidgetK {
|
||||
val local = x + 1
|
||||
return WidgetK(local)
|
||||
}
|
||||
|
||||
/** KDoc on extension fn. */
|
||||
fun WidgetK.extend(n: Int): Int {
|
||||
render()
|
||||
return n
|
||||
}
|
||||
|
||||
fun <T> List<T>.genericExt(): T = first()
|
||||
|
||||
fun com.example.Qualified.qext() {}
|
||||
|
||||
suspend fun suspender(): Unit { helper() }
|
||||
|
||||
private internal fun visFn() {}
|
||||
|
||||
fun inferred() = helper()
|
||||
|
||||
fun nullableRet(): WidgetK? = null
|
||||
|
||||
fun lambdaRet(): (Int) -> Unit = { }
|
||||
|
||||
expect fun platformThing(): Int
|
||||
|
||||
actual fun actualThing(): Int = 1
|
||||
|
||||
tailrec fun tailer(n: Int): Int = if (n <= 0) 0 else tailer(n - 1)
|
||||
|
||||
infix fun Int.pow(e: Int): Int = this
|
||||
|
||||
operator fun WidgetK.plus(o: WidgetK): WidgetK = this
|
||||
|
||||
val topVal: Int = 3
|
||||
var topVar = "s"
|
||||
const val TOP_CONST = 99
|
||||
val topDelegated by lazy { WidgetK(1) }
|
||||
val (destA, destB) = makePair()
|
||||
val withGetter: Int
|
||||
get() = 42
|
||||
|
||||
class WidgetK(val size: Int, private var name: String = defaultName()) {
|
||||
val area: Int = size * size
|
||||
var label: String? = null
|
||||
val computed: Int
|
||||
get() = size * 2
|
||||
|
||||
init {
|
||||
val initLocal = 5
|
||||
register(initLocal)
|
||||
}
|
||||
|
||||
constructor(s: String) : this(s.length) {
|
||||
log(s)
|
||||
}
|
||||
|
||||
fun render(): Unit {
|
||||
draw(size)
|
||||
}
|
||||
|
||||
fun chainInner(): WidgetK = this
|
||||
|
||||
companion object {
|
||||
val SHARED = WidgetK(0)
|
||||
const val COMPANION_CONST = 7
|
||||
fun create(): WidgetK = WidgetK(1)
|
||||
}
|
||||
|
||||
companion object Named { }
|
||||
}
|
||||
|
||||
data class DataK(val a: Int, val b: String)
|
||||
|
||||
abstract class AbstractK {
|
||||
abstract fun impl(): Int
|
||||
}
|
||||
|
||||
open class OpenBase(n: Int) {
|
||||
open fun over() {}
|
||||
}
|
||||
|
||||
class SubK(n: Int) : OpenBase(n), Drawable, Comparable<SubK> {
|
||||
override fun over() {}
|
||||
override fun compareTo(other: SubK): Int = 0
|
||||
override fun draw() {}
|
||||
}
|
||||
|
||||
class QualifiedSuper : com.example.deep.RemoteBase() { }
|
||||
|
||||
class DelegatedImpl(d: Drawable) : Drawable by d
|
||||
|
||||
interface Drawable {
|
||||
fun draw()
|
||||
fun outline(): Int = 1
|
||||
val prop: Int get() = 2
|
||||
}
|
||||
|
||||
sealed class SealedOp {
|
||||
object Add : SealedOp()
|
||||
data class Mul(val f: Int) : SealedOp()
|
||||
}
|
||||
|
||||
sealed interface SealedIface
|
||||
|
||||
enum class Color {
|
||||
RED, GREEN, BLUE
|
||||
}
|
||||
|
||||
enum class Http(val code: Int) {
|
||||
OK(200) {
|
||||
override fun label(): String = "ok"
|
||||
},
|
||||
ERR(500) {
|
||||
override fun label(): String = "err"
|
||||
};
|
||||
|
||||
abstract fun label(): String
|
||||
fun common(): Int = code
|
||||
companion object {
|
||||
fun of(c: Int): Http = OK
|
||||
}
|
||||
}
|
||||
|
||||
object Registry {
|
||||
val instances = mutableListOf<WidgetK>()
|
||||
var count = 0
|
||||
const val REG_CONST = 1
|
||||
fun register(w: WidgetK) { instances.add(w) }
|
||||
}
|
||||
|
||||
annotation class MyMarker(val why: String = "")
|
||||
|
||||
@MyMarker
|
||||
class Annotated {
|
||||
@JvmStatic
|
||||
fun jvmStatic() {}
|
||||
|
||||
@Deprecated("gone", ReplaceWith("new"))
|
||||
fun old() {}
|
||||
|
||||
@field:JvmField
|
||||
val fielded: Int = 1
|
||||
|
||||
@get:MyMarker
|
||||
val got: Int = 2
|
||||
}
|
||||
|
||||
typealias Handler = (Int) -> Unit
|
||||
typealias WidgetList = List<WidgetK>
|
||||
|
||||
expect class PlatformFile {
|
||||
fun path(): String
|
||||
}
|
||||
|
||||
actual class ActualFile {
|
||||
actual fun path(): String = "/"
|
||||
}
|
||||
|
||||
actual typealias PlatformClock = java.time.Clock
|
||||
|
||||
fun caller() {
|
||||
val w = WidgetK(1)
|
||||
w.render()
|
||||
this.toString()
|
||||
super.hashCode()
|
||||
Registry.register(w)
|
||||
Registry.count
|
||||
Color.RED
|
||||
com.example.Fq.CONST_READ
|
||||
WidgetK.create().render()
|
||||
Foo.getInstance().bar()
|
||||
lowerFactory().chain()
|
||||
w.chainInner().render()
|
||||
"literal".uppercase()
|
||||
5.toString()
|
||||
listOf(1, 2).size
|
||||
w.label?.length
|
||||
w.label!!.length
|
||||
helper()
|
||||
Short.static()
|
||||
val fn: Handler = { i -> println(i) }
|
||||
fn(3)
|
||||
(fn)(4)
|
||||
run { helper() }
|
||||
listOf(1).forEach { it + 1 }
|
||||
w.let { it.render() }
|
||||
generic<Int>(1)
|
||||
register(::topLevel)
|
||||
register(OtherClass::handle)
|
||||
register(w::render)
|
||||
register(this::caller)
|
||||
obtain(String::class)
|
||||
val m = ::caller
|
||||
val bound = w::render
|
||||
val s = "interp $topVal and ${w.render()} end"
|
||||
val multi = """raw $topVal"""
|
||||
when (w.size) {
|
||||
1 -> helper()
|
||||
else -> draw(0)
|
||||
}
|
||||
if (topVal > 1) { helper() }
|
||||
for (i in 1..3) { draw(i) }
|
||||
fun localFn(): Int = 5
|
||||
localFn()
|
||||
class LocalClass {
|
||||
fun lm() {}
|
||||
}
|
||||
object LocalObj {
|
||||
fun om() {}
|
||||
}
|
||||
val anon = object : Drawable {
|
||||
override fun draw() { helper() }
|
||||
}
|
||||
anon.draw()
|
||||
label@ for (i in 1..2) { break@label }
|
||||
val backtick = `weird name`()
|
||||
}
|
||||
|
||||
fun `weird name`(): Int = 1
|
||||
|
||||
fun trailing(block: (Int) -> Int): Int = block(1)
|
||||
|
||||
fun useTrailing() {
|
||||
trailing { it * 2 }
|
||||
trailing() { it * 3 }
|
||||
}
|
||||
|
||||
fun defaults(a: Int = compute(), b: String = "x") {}
|
||||
|
||||
fun varargFn(vararg xs: Int) {}
|
||||
|
||||
fun destructuringBody(p: Pair<Int, Int>) {
|
||||
val (x, y) = p
|
||||
draw(x + y)
|
||||
}
|
||||
|
||||
fun assignRefs() {
|
||||
Registry.count = 5
|
||||
Registry.count += 1
|
||||
}
|
||||
|
||||
fun nullish(x: WidgetK?) {
|
||||
x?.render()
|
||||
val l = x ?: WidgetK(0)
|
||||
}
|
||||
|
||||
fun stringsEdge() {
|
||||
val a = "quote \" and dollar ${'$'} done"
|
||||
}
|
||||
|
||||
fun labeledLambda() {
|
||||
listOf(1).forEach loop@{ if (it == 0) return@loop }
|
||||
}
|
||||
|
||||
fun whereClause(): Int where Int : Comparable<Int> = 1
|
||||
@@ -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'];
|
||||
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go', 'c', 'cpp', 'rust', 'csharp', 'ruby', 'php', 'swift', 'kotlin'];
|
||||
|
||||
describe.skipIf(!kernelBuilt)('kernel↔wasm grammar parity', () => {
|
||||
beforeAll(async () => {
|
||||
|
||||
@@ -0,0 +1,140 @@
|
||||
/**
|
||||
* Kernel↔wasm Kotlin extraction parity (R7b of the kernel migration).
|
||||
*
|
||||
* Asserts the native walker (codegraph-kernel/src/kotlin.rs — grammar
|
||||
* compiled from the vendored fwcd 0.3.8 C sources, the arc's first
|
||||
* vendored-grammar-C language) produces the SAME ExtractionResult as the
|
||||
* wasm TreeSitterExtractor over the checked-in torture fixture (torture.kt:
|
||||
* the property hook's scope classification, extension-function receiver QNs
|
||||
* (`WidgetK::extend`, the qualified `com::qext` bug) + the owner-contains
|
||||
* fallback, expect/actual → node DECORATORS (the KMP synthesizer feed),
|
||||
* the bodiless-vs-bodied class header asymmetry, comment-glued
|
||||
* import/package extents, KDoc dropped-and-chain-breaking docstrings,
|
||||
* `@Marker` decorates vs `@Anno(args)` nothing, zero type-annotation refs,
|
||||
* zero instantiates, the #750 capitalized-chain re-encode, paren-then-
|
||||
* lambda garbage callees, `${X}`-reads-vs-`$X`-non-reads value refs and the
|
||||
* packaged-file target drop) plus a `.kts` script fixture (file-attributed
|
||||
* top-level calls), with in-memory CRLF variants (#1329), and two defer
|
||||
* fixtures — a `fun interface` file and a PHANTOM error (a one-line class
|
||||
* body sets hasError with a complete, ERROR-node-free CST; the kernel
|
||||
* trusts the flag).
|
||||
*
|
||||
* The full-repo sweep lives in scripts/kernel-parity.mjs (okio/okhttp/
|
||||
* kotlinx.coroutines — expected deferrals 23/49/51, grammar-inherent).
|
||||
* Skips when no kernel binary is staged; CODEGRAPH_KERNEL_EXPECT=1 turns
|
||||
* that into a failure (kernel-scaffold.test.ts).
|
||||
*/
|
||||
|
||||
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 Kotlin extraction parity', () => {
|
||||
beforeAll(async () => {
|
||||
await initGrammars();
|
||||
await loadGrammarsForLanguages(['kotlin']);
|
||||
});
|
||||
|
||||
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 = 3): void {
|
||||
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
||||
delete process.env.CODEGRAPH_KERNEL;
|
||||
const viaKernel = tryKernelExtract(filePath, source, 'kotlin');
|
||||
expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
|
||||
|
||||
process.env.CODEGRAPH_KERNEL = '0';
|
||||
const viaWasm = extractFromSource(filePath, source, 'kotlin');
|
||||
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);
|
||||
}
|
||||
|
||||
const FIXTURES: Array<{ file: string; minNodes: number }> = [
|
||||
{ file: 'torture.kt', minNodes: 40 },
|
||||
{ file: 'TortureScript.kts', minNodes: 2 },
|
||||
];
|
||||
|
||||
for (const { file, minNodes } of FIXTURES) {
|
||||
it(`${file}: hook properties, receivers, decorators, calls, value refs`, () => {
|
||||
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('fun-interface files defer to the wasm extractor (grammar-inherent error)', () => {
|
||||
const src = 'package p\n\nfun interface Transformer {\n fun transform(x: Int): Int\n}\n\nfun after() { work() }\n';
|
||||
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
||||
delete process.env.CODEGRAPH_KERNEL;
|
||||
expect(tryKernelExtract('src/FunIface.kt', src, 'kotlin')).toBeNull();
|
||||
process.env.CODEGRAPH_KERNEL = '0';
|
||||
const viaWasm = extractFromSource('src/FunIface.kt', src, 'kotlin');
|
||||
delete process.env.CODEGRAPH_KERNEL;
|
||||
// The wasm arm's misparse-recovery hook still mints the interface node.
|
||||
expect(viaWasm.nodes.some((n) => n.kind === 'interface' && n.name === 'Transformer')).toBe(true);
|
||||
});
|
||||
|
||||
it('PHANTOM errors defer too — hasError with a complete, ERROR-node-free CST', () => {
|
||||
const src = 'abstract class A { abstract fun i(): Int }\n';
|
||||
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
||||
delete process.env.CODEGRAPH_KERNEL;
|
||||
expect(tryKernelExtract('src/Phantom.kt', src, 'kotlin')).toBeNull();
|
||||
process.env.CODEGRAPH_KERNEL = '0';
|
||||
const viaWasm = extractFromSource('src/Phantom.kt', src, 'kotlin');
|
||||
delete process.env.CODEGRAPH_KERNEL;
|
||||
expect(viaWasm.nodes.some((n) => n.kind === 'class' && n.name === 'A')).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -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'] as const) {
|
||||
for (const lang of ['typescript', 'tsx', 'javascript', 'jsx', 'java', 'python', 'go', 'ruby', 'php', 'swift', 'kotlin'] as const) {
|
||||
expect(kernelRoutes(lang), lang).toBe(true);
|
||||
}
|
||||
expect(kernelRoutes('kotlin')).toBe(false);
|
||||
expect(tryKernelExtract('src/a.kt', 'fun f() {}\n', 'kotlin')).toBeNull();
|
||||
expect(kernelRoutes('scala')).toBe(false);
|
||||
expect(tryKernelExtract('src/a.scala', 'object A { def f(): Int = 1 }\n', 'scala')).toBeNull();
|
||||
// CODEGRAPH_KERNEL_LANGS REPLACES the default set when present.
|
||||
process.env.CODEGRAPH_KERNEL_LANGS = 'tsx';
|
||||
expect(kernelRoutes('typescript')).toBe(false);
|
||||
|
||||
Reference in New Issue
Block a user