feat(kernel): R7b Rust walker — rustlang module, tree-sitter-rust 0.24.2 bump, rust default-routed (#1371)
First R7b language port. Grammar: tree-sitter-rust pinned =0.24.2 + wasm vendored from tag 77a3747 (parser.c/scanner.c sha-matched against the crates.io tarball), replacing the 2023 ABI-14 tree-sitter-wasms build — the bump alone is precision-positive on the wasm path (receiver-qualified instance-method resolutions replace ambiguous bare-name matches; node sections byte-identical on ripgrep/tokio). Walker mirrors the TS reference bug-for-bug per docs/design/rust-lang-kernel-port-checklist.md (survey artifact): dead-code isAsync, impl-pushes-no-scope, the impl-Trait-for-Generic<T> trait-receiver quirk, phantom const identifiers, use-binding triple emission, wildcard-use-emits-nothing, scoped-supertrait drop, chained-call re-encode gated on scoped_identifier, Rocket route macros body-only. Gates: parity sweeps 0 diffs — ripgrep 101/101, tokio 790/790, rust-analyzer 1217/1488 (271 deferrals are token-macro-table sources that error on BOTH arms — grammar-inherent); full-init dump-diffs byte-identical on all three (3,857 / 13,440 / 39,030 nodes); kernel-rustlang-parity suite (torture + CRLF + defer) in npm test; full suite green x2 with CODEGRAPH_KERNEL_EXPECT=1. Co-authored-by: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Fable 5
parent
ce0ae30e09
commit
f1ca991943
@@ -0,0 +1,211 @@
|
||||
//! Torture fixture for the rust kernel walker (R7b) — every quirk in
|
||||
//! docs/design/rust-lang-kernel-port-checklist.md, parse-clean.
|
||||
|
||||
use std::fmt;
|
||||
use crate::mod_a::Item;
|
||||
use crate::mod_b::{A, B as C, sub::D};
|
||||
use a::{b::{c, d}};
|
||||
use foo_single;
|
||||
use std::collections::*;
|
||||
|
||||
/// Widget docs.
|
||||
pub struct Widget {
|
||||
pub n: u32,
|
||||
name: String,
|
||||
field: Deep,
|
||||
}
|
||||
|
||||
pub struct Unit;
|
||||
|
||||
pub struct Pair(u32, u32);
|
||||
|
||||
/// Doc broken by the attribute below — must yield NO docstring.
|
||||
#[derive(Debug)]
|
||||
pub struct Doc {
|
||||
x: u32,
|
||||
}
|
||||
|
||||
pub struct Deep {
|
||||
z: u32,
|
||||
}
|
||||
|
||||
pub enum Shape {
|
||||
Circle(f32),
|
||||
Rect { w: f32, h: f32 },
|
||||
Empty,
|
||||
}
|
||||
|
||||
pub type Alias = Vec<Widget>;
|
||||
|
||||
/// Render trait docs.
|
||||
pub trait Render: Base + fmt::Debug {
|
||||
fn render(&self);
|
||||
fn hint(&self) -> Size {
|
||||
Size::default()
|
||||
}
|
||||
const CAP: usize = init_cap();
|
||||
type Output;
|
||||
}
|
||||
|
||||
pub trait Base {}
|
||||
|
||||
pub trait Super2: Producer<u32> {}
|
||||
|
||||
pub trait Owned: for<'de> Deserialize<'de> {}
|
||||
|
||||
impl Render for Widget {
|
||||
fn render(&self) {
|
||||
draw(self);
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for Widget {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
write!(f, "w{}", self.n)
|
||||
}
|
||||
}
|
||||
|
||||
impl Widget {
|
||||
const SCALE: usize = 3;
|
||||
|
||||
fn area(&self) -> u32 {
|
||||
self.n * mul()
|
||||
}
|
||||
|
||||
fn clone_self(&self) -> Self {
|
||||
Self::assoc();
|
||||
Widget {
|
||||
n: self.n,
|
||||
name: String::new(),
|
||||
field: Deep { z: 0 },
|
||||
}
|
||||
}
|
||||
|
||||
fn borrow_widget(&self) -> &Widget {
|
||||
self
|
||||
}
|
||||
|
||||
fn outer(&self) -> u32 {
|
||||
fn inner_helper(v: u32) -> u32 {
|
||||
v
|
||||
}
|
||||
inner_helper(self.n)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Container<T> {
|
||||
item: T,
|
||||
}
|
||||
|
||||
impl<T> Container<T> {
|
||||
fn unwrap(self) -> T {
|
||||
self.item
|
||||
}
|
||||
}
|
||||
|
||||
impl Render for Container<u32> {
|
||||
fn render(&self) {}
|
||||
}
|
||||
|
||||
impl Later {
|
||||
fn touch(&self) {}
|
||||
}
|
||||
|
||||
pub struct Later {
|
||||
z: u32,
|
||||
}
|
||||
|
||||
/* Block-doc for an async fn — isAsync must stay FALSE (dead-code hook). */
|
||||
pub async fn fetch_data(url: &str) -> Result<Response, Error> {
|
||||
let body = get(url).await;
|
||||
client.request().await.send();
|
||||
body
|
||||
}
|
||||
|
||||
pub fn nested_ret() -> Result<Vec<Widget>, Error> {
|
||||
make_result()
|
||||
}
|
||||
|
||||
pub fn vec_ret(w: &Widget) -> Vec<Widget> {
|
||||
build_list(w)
|
||||
}
|
||||
|
||||
pub(crate) fn crate_fn() {}
|
||||
|
||||
fn caller() {
|
||||
let w = Widget {
|
||||
n: 1,
|
||||
name: make_name(),
|
||||
field: Deep { z: 1 },
|
||||
};
|
||||
let v = m::Widget { n: 2 };
|
||||
let r = Foo::new().bar();
|
||||
let x = w.method_a().chain_b();
|
||||
let y = w.field.deep_call();
|
||||
let s = "lit".len();
|
||||
let f = 5.0_f64.floor();
|
||||
helper();
|
||||
m::helper2();
|
||||
let t = helper::<u32>(3);
|
||||
(helper)(1);
|
||||
takes(Widget {
|
||||
n: 3,
|
||||
name: n2(),
|
||||
field: Deep { z: 2 },
|
||||
});
|
||||
}
|
||||
|
||||
fn handler() {}
|
||||
fn handler2() {}
|
||||
fn cb_a() {}
|
||||
fn cb_b() {}
|
||||
fn invoke_all(fns: [fn(); 2]) {}
|
||||
|
||||
fn register(f: fn()) {
|
||||
f();
|
||||
}
|
||||
|
||||
pub struct Holder {
|
||||
cb: fn(),
|
||||
}
|
||||
|
||||
fn wiring(mut o: Holder) {
|
||||
register(handler);
|
||||
o.cb = handler2;
|
||||
let h = Holder { cb: cb_a };
|
||||
let arr = [cb_a, cb_b];
|
||||
let local = handler;
|
||||
let (t1, t2) = (cb_a, cb_b);
|
||||
invoke_all(arr);
|
||||
invoke(foo_single);
|
||||
}
|
||||
|
||||
static CB: fn() = handler;
|
||||
|
||||
const MAX_LIMIT: u32 = OTHER_LIMIT;
|
||||
const OTHER_LIMIT: u32 = 99;
|
||||
|
||||
fn reads_limits() -> u32 {
|
||||
MAX_LIMIT + OTHER_LIMIT
|
||||
}
|
||||
|
||||
fn shadowed_read() {
|
||||
let MAX_LIMIT = 5;
|
||||
let _ = MAX_LIMIT;
|
||||
}
|
||||
|
||||
mod inner {
|
||||
pub fn helper_pub() {}
|
||||
fn hidden() {}
|
||||
pub struct Item2 {
|
||||
pub v: u32,
|
||||
}
|
||||
}
|
||||
|
||||
fn mount() {
|
||||
let r = routes![a::b::index_h, health_h];
|
||||
let c = catchers![not_found_h];
|
||||
let skipped = rocket::routes![a::x];
|
||||
}
|
||||
|
||||
routes![top_level_h];
|
||||
@@ -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'];
|
||||
const GRAMMAR_LANGUAGES: Language[] = ['typescript', 'tsx', 'javascript', 'java', 'python', 'go', 'c', 'cpp', 'rust'];
|
||||
|
||||
describe.skipIf(!kernelBuilt)('kernel↔wasm grammar parity', () => {
|
||||
beforeAll(async () => {
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
/**
|
||||
* Kernel↔wasm Rust extraction parity (R7b of the kernel migration).
|
||||
*
|
||||
* Asserts the native walker (codegraph-kernel/src/rustlang.rs) produces the
|
||||
* SAME ExtractionResult as the wasm TreeSitterExtractor — nodes, edges, and
|
||||
* unresolved refs compared as canonicalized multisets — over the checked-in
|
||||
* torture fixture (torture.rs: impl/trait quirks incl. the
|
||||
* `impl Trait for Generic<T>` trait-receiver bug, unit-struct skip, phantom
|
||||
* const identifiers, use-binding refs incl. nested groups + wildcard-emits-
|
||||
* nothing, chained-call re-encode, turbofish, Rocket route macros body-only,
|
||||
* fn-ref shapes, value-ref shadowing, attribute-broken docstrings, dead-code
|
||||
* isAsync) and its CRLF variant (derived in-memory — #1329 docstring
|
||||
* semantics).
|
||||
*
|
||||
* The full-repo sweep lives in scripts/kernel-parity.mjs (ripgrep/tokio/
|
||||
* rust-analyzer for the §5 gate); 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 (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 Rust extraction parity', () => {
|
||||
beforeAll(async () => {
|
||||
await initGrammars();
|
||||
await loadGrammarsForLanguages(['rust']);
|
||||
});
|
||||
|
||||
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, 'rust');
|
||||
expect(viaKernel, `kernel extraction failed for ${filePath}`).not.toBeNull();
|
||||
|
||||
process.env.CODEGRAPH_KERNEL = '0';
|
||||
const viaWasm = extractFromSource(filePath, source, 'rust');
|
||||
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);
|
||||
}
|
||||
|
||||
it('torture fixture: impl/trait quirks, use bindings, chains, fn-refs, value-refs, route macros', () => {
|
||||
const file = path.join(FIXTURE_DIR, 'torture.rs');
|
||||
assertParity('fixtures/torture.rs', fs.readFileSync(file, 'utf8'), 20);
|
||||
});
|
||||
|
||||
// CRLF variant — the shape every Windows autocrlf checkout has. Derived in
|
||||
// memory so no platform or editor can silently normalize it away; pins the
|
||||
// JS-multiline-^ docstring semantics for `///` runs (#1329).
|
||||
it('torture fixture CRLF parity', () => {
|
||||
const file = path.join(FIXTURE_DIR, 'torture.rs');
|
||||
const crlf = fs.readFileSync(file, 'utf8').replace(/(?<!\r)\n/g, '\r\n');
|
||||
assertParity('fixtures/torture.rs (crlf)', crlf, 20);
|
||||
});
|
||||
|
||||
it('files with parse errors defer to the wasm extractor (recovery is encoding-dependent)', () => {
|
||||
const broken = 'fn f( {\n return }} 12 (\n';
|
||||
process.env.CODEGRAPH_KERNEL_LANGS = 'all';
|
||||
delete process.env.CODEGRAPH_KERNEL;
|
||||
expect(tryKernelExtract('src/broken.rs', broken, 'rust')).toBeNull();
|
||||
process.env.CODEGRAPH_KERNEL = '0';
|
||||
const viaWasm = extractFromSource('src/broken.rs', broken, 'rust');
|
||||
delete process.env.CODEGRAPH_KERNEL;
|
||||
expect(viaWasm.nodes.some((n) => n.kind === 'file')).toBe(true);
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user