SC9001 ICE: varRef type object != binding dyn on bundled circular module-level consts

Aperta
#4,405 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
javascript, node.js, typescript
Ambito
backend, compilers

Direzione di ricerca

Start by reading the compiler’s handling of module-level const bindings, returned closures, and varRef type validation; the issue does not name source files or tests for these paths. Reproduce with the 12-line JavaScript example using scriptc build, then compare it with the controls that compile. Done when the bundled JavaScript builds successfully and its runtime output remains 1.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Summary

scriptc build hits SC9001 (internal compiler error: varRef "…" type object != binding dyn) on legal JavaScript that bundlers emit: a module-level const closes over another module-level const that is initialized later by a factory returning new SomeClass(...).

scriptc coverage of the same file is 100% static. Node runs it. The identical program as TypeScript compiles. So this is a codegen/IR-validation ICE, not a coverage or language-service crash.

Reduced from rolldown-bundled effect/Schema (t3code vp pack chunk Schema-CHz9fEw2.mjs, four ICEs at the Go-nil codec consts nullAsMissing / nullAsEmptyArray / emptyRecord / nullAsEmptyRecord). Please do not ask applications to unminify that chunk by hand — the 12-line program below is sufficient.

Reproduction

repro.mjs:

class Box {
  constructor(v) {
    this.v = v;
  }
}
function apply(f) {
  return f;
}
const earlier = apply(() => later);
const make = (v) => new Box(v);
const later = make(1);
console.log(earlier().v);
$ node repro.mjs
1

$ scriptc coverage repro.mjs
# 6/6 statements static (100%)

$ scriptc build repro.mjs -o out
repro.mjs:9:29 - error SC9001: internal compiler error: in %fn.309985bfdfc6: varRef "%g.e.later" type object != binding dyn — please report this

   8 | }
   9 | const earlier = apply(() => later);
     |                             ^~~~~
  10 | const make = (v) => new Box(v);

1 error.

Minified to one line, same ICE:

class Box{constructor(v){this.v=v}}function apply(f){return f}const earlier=apply(ast=>later),make=v=>new Box(v),later=make(1);console.log(earlier(0).v)

apply can be the identity (return f). What matters is that the closing lambda is returned from a call, not assigned directly.

Controls that compile and print 1

Same program as TypeScript (repro.ts with a typed apply / make) — scriptc build succeeds.

Reordering so later is initialized first:

const make = (v) => new Box(v);
const later = make(1);
const earlier = apply(() => later);

Direct new instead of a factory (still a circular const):

const earlier = apply(() => later);
const later = new Box(1);

Unbundled modules (the factory and later in a second file, imported by the closer) also compile. That is why unminified npm effect source (scriptc build entry.mjs --npm-static=effect) succeeds while a rolldown bundle of the same library fails.

Original trigger

scriptc build of a rolldown bundle of [email protected] Schema (pretty-printed, original names kept) reports four ICEs, all this pattern:

Schema.unmin.mjs:12937:39 - error SC9001: … varRef "%g.e.nullAsMissing" type object != binding dyn
Schema.unmin.mjs:12941:144 - error SC9001: … varRef "%g.e.nullAsEmptyArray" type object != binding dyn
Schema.unmin.mjs:12942:139 - error SC9001: … varRef "%g.e.emptyRecord" type object != binding dyn
Schema.unmin.mjs:12942:165 - error SC9001: … varRef "%g.e.nullAsEmptyRecord" type object != binding dyn

Those names are module-level consts in effect/src/internal/schema/toCodec.ts:

const nilAsMissingAtEncoded = applyToSelfOrLastLinkEncoding((ast) => {
  return orNull(ast, decodeNull(never, nullAsMissing));
});
const nilAsEmptyAtEncoded = applyToSelfOrLastLinkEncoding((ast) => {
  // …
  return orNull(ast, decodeNull(replaceChecks(emptyRecord, ast.checks), nullAsEmptyRecord));
});
const nullTo = (decode) => new Transformation(decode, forbidden(() => "…"));
const nullAsMissing = nullTo(transformOptional(() => none()));
const nullAsEmptyArray = nullTo(transform(() => []));
const nullAsEmptyRecord = nullTo(transform(() => ({})));
const emptyRecord = record(string, never);

Circular module-level consts through a helper that only stores the callback are legal JS (const TDZ is not hit because applyToSelfOrLastLinkEncoding does not call f during init). Rolldown concatenates them into one module; scriptc then types the binding as dyn and the varRef as object.

Workaround for consumers

Compile TypeScript / npm source (ordinary per-file modules), not bundler output. t3code will feed scriptc build src/binMain.ts rather than vp pack chunks for this reason. That is a consumer dodge, not a fix.

Environment

  • scriptc 0.4.17 and 0.4.16 (identical ICE on the 12-line repro; not a 0.4.17 regression)
  • @scriptc/cli-linux-x64-gnu, Node.js 24.13.1, linux x64
  • effect 4.1.5 for the original Schema bundle; the reduced repro has no dependencies

Relationship to existing reports

Searched open and closed SC9001 / ICE issues (varRef, binding dyn, emptyRecord, nullAsMissing, minified circular consts). Closest hits are other SC9001 shapes (recordKeyGet, bytesSet, toString protocol) — none are varRef type object vs binding dyn. Distinct from #4381 (tsc-ts SIGSEGV on one large file) and from #4171 (coverage abort on @effect/platform-node).

Lingua principale
TypeScript
Stelle
6.7k
Fork
229
Merge medio
14h 29m
PR unite (30g)
787

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di vercel-labs/scriptc

Tutte le issue di vercel-labs/scriptc

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.