Description
Found while verifying #1949's fix
After implementing #1949's native CHA typed-dispatch fallback and re-running the repo's own src/ self-build repro, native's edge count for calls/receiver narrowed substantially against wasm but a residual 16-caller gap remains, all still targeting NativeDbProxy.prepare/.transaction — but this residual gap is a different root cause, not the CHA-dispatch issue #1949 fixed.
Repro
node dist/cli.js build src --engine native --no-incremental --db /tmp/native.db
node dist/cli.js build src --engine wasm --no-incremental --db /tmp/wasm.dbCompare edges from openReadonlyOrFail (src/db/connection.ts):
- native:
receiveredge fordb→LockedDatabase(confidence 0.9); nocallsedge toprepare/pragmaat all. - wasm:
receiveredge fordb→BetterSqlite3Database(confidence 1.0);callsedges toBetterSqlite3Database.prepare/.pragma(0.6, proximity) AND toNativeDbProxy.prepare/.pragma(0.8, CHA-expanded).
openReadonlyOrFail casts its local db to BetterSqlite3Database directly (as unknown as BetterSqlite3Database, line 389). LockedDatabase (type LockedDatabase = BetterSqlite3Database & { __lockPath?: string }) is a different local variable, in a different function in the same file, cast via as unknown as LockedDatabase at line 171.
Root cause (diagnosed, not yet fixed)
Both engines' typeMap is a flat, per-file, name-only structure (HashMap<&str, (&str, f64)> in build_type_map / Map<string, TypeMapEntry> via setTypeMapEntry) — there is no per-function scoping. When two different functions in the same file each have their own local/parameter named db typed differently, both entries key-collide under the plain name "db".
Both engines apply the identical tie-break rule ("first inserted wins on a confidence tie" — if tm.confidence > e.get().1 in Rust's build_type_map, if (!existing || confidence > existing.confidence) in TS's setTypeMapEntry), so in principle they should agree — but they don't, meaning the two extractors are seeding a different set or order of typeMap entries for these same-named locals in this file (verified on two independent examples: db/connection.ts's LockedDatabase-vs-BetterSqlite3Database collision, and features/flow.ts's bfsCallees(db: ReturnType<typeof openReadonlyOrFail>, ...) parameter, which requires seeing through a ReturnType<typeof fn> type-utility or another same-file db: BetterSqlite3Database annotation to resolve correctly — wasm gets there, native doesn't).
Affected callers (16 in src/, all still missing in native after #1949)
db/connection.ts:openReadonlyOrFail, domain/graph/builder/stages/build-structure.ts:{loadUnchangedFilesFromDb,refreshAffectedDirectoryMetrics,updateChangedFileMetrics}, domain/search/search/keyword.ts:ftsSearchData, domain/search/search/prepare.ts:prepareSearch, features/complexity-query.ts:{checkHasGraph,fetchAllComplexityMetrics,queryComplexityRows}, features/flow.ts:{bfsCallees,findLeafNodes,listEntryPointsData}, shared/generators.ts:{iterRoles,iterWhere}.
Scope note
This is an extraction/typeMap-architecture gap (which type name a same-named local resolves to), not a resolution-layer gap — #1949's CHA fallback in resolve_call_targets_core only fires when a typeMap-resolved type is already available; here the type resolved is simply wrong (or absent) before resolution ever runs. Fixing this properly likely means either scoping typeMap entries by enclosing function/definition (a larger change to TypeMapInput/ExtractorOutput.typeMap in both engines) or auditing why the two extractors' insertion order/confidence differs for as-cast and ReturnType<typeof fn> patterns specifically. Left open with this diagnosis rather than folding into #1949, which is scoped to the resolution layer only.
Activity
fix(native): add CHA typed-dispatch fallback to resolve_call_targets_…
fix(typemap): scope same-named locals/params by enclosing function (o…
fix(dart): parameter shadowing a class field must resolve its own type
fix(dart): populate typeMap for field/parameter types (#2477)
fix(native): add missing factory-method type-map heuristic (#2533)
fix(js/ts): seed typeMap from as-casts, resolving the #2235 real-worl…