Repository navigation
Conversation
Between the "?" and ":" of a conditional, the TypeScript parser tries "(b) : T => body" as an arrow function and keeps it only if a ":" follows the body (#40818). The outcome is stored in `ts_conditional_arrow_attempts` by the offset of the ":" alone. The attempt also reads two parser states: whether the arrow function is async, and whether private names are allowed. One ":" can be reached in two states. A discarded `async (h) :` attempt reads `await /x.../` in its body as a regular expression, and the real parse reads a division. The text after it can then belong to a class in one reading and not in the other, or follow an `async (` head in one reading and a plain `(` head in the other. The real parse took the outcome of the other state: an error for valid input, or an arrow function that silently lost its ": e". The key now holds the offset of the ":" and `arrow_data.allow_await`. The attempt allows private names wherever it runs, so that its outcome does not depend on the class around it. The real parse still reports a private name outside a class. `allow_in` is always true at this point, and a debug assertion says so. A debug build also computes the attempt again for each memo hit of the real parse and compares. The attempt without the memo is its own cold function, so the parser snapshot is no longer in the stack frame of `parse_paren_expr`.
|
Status Reproduced on main (017b243) with a release build. Save each input as a
With this PR (#45939) both print as tsc 6.0.2 reads them. The third input (two lines) is in the PR description. |
|
Updated 12:26 AM PT - Oct 27th, 2026
✅ @robobun, your commit 8b3724c68efc6ca2403ba6fbb6977f33e86dc4c0 passed in 🧪 To try this PR locally: bunx bun-pr 45939That installs a local version of the PR into your bun-45939 --bun |
…itional-arrow-memo-key
…itional-arrow-memo-key # Conflicts: # src/jsc/RuntimeTranspilerCache.rs
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
Memo key and speculative parse src/js_parser/p.rs, src/js_parser/parse/parse_skip_typescript.rs |
The memo key includes allow_await, and the parser checks allow_in. Parser snapshots capture and restore additional flags. A helper performs the speculative parse. Stack overflow and allocation errors propagate; other parse errors produce false. |
Regression cases and cache version test/bundler/transpiler/transpiler.test.js, src/jsc/RuntimeTranspilerCache.rs |
Tests cover nested parses across differing contexts, parenthesized and async arrow heads at depth 40, and discarded JSX attempts. The runtime transpiler cache format version changes from 43 to 44. |
Suggested reviewers: jarred-sumner
Priority:
Merge Risk: 
Minimal · up to 8b372
-
The parser now distinguishes await state and restores flags from discarded attempts. No merge-blocking risk was identified.
Pre-merge checks: 
4
-
Full details: Title check
-
Explanation
The title names the change accurately: the memo for the
a ? (b) : c => dspeculative attempt is re-keyed by the state the attempt reads. The raw summary confirms the memo key now includes the colon offset and arrow_data.allow_await, and the parser snapshot is extended. The title is specific and single-sentence, though at 80 characters it is slightly long and omits the snapshot change.
Full details: Description check
-
Explanation
The description is complete and relevant. It explains the problem, fix, background, downsides, and verification results. It does not use the template headings exactly, but it provides the required information under equivalent sections.
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
There was a problem hiding this comment.
Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.
Beyond the inline findings, I also checked the key packing in src/js_parser/parse/parse_skip_typescript.rs:1610 — AwaitOrYield has discriminants 0..2, which fit in the two bits freed by << 2 before the outcome bit, and ts_conditional_arrow_attempts is intentionally outside parser_snapshot, so entries inserted by nested attempts during the debug re-run are keyed and harmless. The existing 2^40 nesting inputs are preserved as the first iteration of the new loop rather than mutated.
Extended reasoning...
The change re-keys the TypeScript conditional-arrow attempt memo by : offset plus allow_await, widens the memo to u64, extracts the attempt into a cold function that forces allow_private_identifiers, adds a debug re-run assertion on memo hits, bumps the transpiler cache version to 42, and adds transpiler tests. It touches no security-sensitive surface. Two confirmed inline findings (unrestored module flags from a discarded attempt now reachable past private names, and reduced nesting depth from the extra frame) already signal that a human should look; this note only records what else was examined.
… and stays in the frame of its caller The attempt allows private names, so it can read further than before. `has_import_meta`, `has_with_scope` and `needs_jsx_import` that it set stayed set after the restore. The snapshot holds them now. The attempt is inlined into its one release call site again. As a function of its own it made chains of attempts nest less deep.
There was a problem hiding this comment.
The latest push addresses both points from my earlier review (the three module flags are now in ParserSnapshot, with a scanImports test, and the attempt body is #[inline(always)] so no extra frame per nesting level); this run found no bugs. Because the fix rests on a global argument that the memo key now covers every parser state the attempt reads, a human look at that argument is still worthwhile.
What was reviewed:
- Memo key coverage:
arrow_datais built at src/js_parser/parse/mod.rs:561-568 fromopts.is_asyncalone, soallow_awaitis its only varying field;allow_inis forced true by the?at src/js_parser/parse/parse_suffix.rs:452 and theAfterQuestionAndBeforeColonflag only propagates into nested conditional branches where it stays true; strict-mode/super/thischecks in the body only log (mark_strict_mode_featurereturnsOk). - Debug-build re-run on a memo hit: gated on
lexer.is_log_disabledso attempts nested inside another attempt are not re-run, errors are swallowed via.ok(), and it uses the same snapshot/restore path as a miss, so it adds no new side effects; the 40-deep chain test now runs for both heads. << 2in the key is correct for the 3-valuedAwaitOrYield; the stored entry shifts by one more for the outcome bit, and the lookup masks consistently.- Nit: the PR description is stale relative to the code (it says
#[cold]/#[inline(never)]andEXPECTED_VERSION41 to 42; the code has#[inline(always)]and 43 to 44).
Extended reasoning...
The change touches the TypeScript parser's speculative parse of a ? (b) : c => d : e (src/js_parser/parse/parse_skip_typescript.rs), widens and re-keys its memo by : offset plus allow_await, forces allow_private_identifiers during the attempt, extends ParserSnapshot in src/js_parser/p.rs with three more restored flags, bumps the transpiler cache version, and adds transpiler tests. It touches no security-sensitive surface (no auth, injection, or data exposure), only parser correctness and a cache-version bump. Both inline findings from the prior run were addressed by the two newest commits and the bug hunt ran dry with no new findings, but the correctness of the memo key depends on a whole-parser invariant argument (allow_in only debug-asserted, other state assumed log-only) that merits a maintainer's judgment rather than an automated approval. No CODEOWNERS entry covers the changed files.
|
Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-27. The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR. |
Problem
.tsinputs that tsc accepts are read wrong (Notes). One loses its: ewith no error. Others fail withExpected ";" but found ":". No user reported it.ts_conditional_arrow_attempts(src/js_parser/p.rs:395, parser: accept TypeScript arrow return types inside conditionals,export typebefore a newline, andimport type from#40818) is keyed by the:offset alone. The outcome also depends onasyncand on the class around it.async (h) :attempt readsawait /x.../as a regular expression, the real parse as a division. So one:has two states.Fix
:offset andarrow_data.allow_await. The attempt allows private names everywhere. The real parse still reports one outside a class.has_import_meta,has_with_scopeandneeds_jsx_import.test/bundler/transpiler/transpiler.test.js(10 new inputs and one new test, each fails on main). Self-reviewed: 6 concerns raised, 6 addressed.Background
a ? (b) : c => d, tsc keeps the arrow function(b): c => donly if a second:follows its body. parser: accept TypeScript arrow return types inside conditionals,export typebefore a newline, andimport type from#40818 tries that parse and restores the parser.Downsides
parse_paren_exprgrows by 32 B in TypeScript: nesting is 2% less deep (((((1)))): 4,713 to 4,629).Notes
Inputs
Save each as a
.tsfile and runbun build --no-bundle <file>.bun runcannot show the difference: it fails before and after withCannot use 'await' as an identifier in a module.t1.tserror: Expected ";" but found ":"p = /.y/g.test(s) ? (a) => (#q in o) : e;t2.tsq = (p = /.y/g.test(s) ? a : (T) => (#p in o)) => body;, no errorq = (p = /.y/g.test(s) ? (a) => (#p in o) : e) => body;t3.tserror: "await" can only be used inside an "async" functionasync (a, c = /.y/g) => await zThe inputs are built for the memo: they need
awaitbefore a/in the body of a discardedasync (…) :attempt. The test has seven more of this kind. One of them is an error with this PR and was printed without its: gon main:tsc rejects it too, and main rejects it without the
await /x? … /prefix. Inputs of this kind compiled on main only through the stale outcome. Some of them are accepted by tsc and depend on a second bug ((a as T) : U => …is taken for an arrow function). Those come back with the change that fixes that bug, not here.How one
:is reached twiceIn
t1.tsthe parser first triesasync (h) : R => …as an async arrow function. In that attemptawaitis a keyword, so/x? class { p = /is a regular expression, and the text after it is.y / g.test(s) ? (a) : T => #q in o : e. The attempt for(a) :runs outside a class. No:follows the body ofasync (h), so that attempt is discarded. The real parse readsasync(h)as a call, andawait / x ? class { … } : noas a division and a conditional. It reaches the same(a) :inside the class and takes the stored outcome.On main the attempt outside the class fails on
#q, and the real parse took "not an arrow function" from the memo.t3.tsis the same with the other state: the attempt reaches: Tafter(p = await /x? async (a, c = /.y/g), which is not async, and the real parse reaches it afterasync (a, c = /.y/g).Why this key
The attempt parses a return type and an arrow function body. Besides the source after the
:, the body reads:arrow_data.allow_await:parse_arrow_bodymakes it the state of the body. It is in the key.allow_private_identifiers, for#q in o. tsc's parser has no such state, and esbuild removed it (v0.25.8). The attempt now sets it, so its outcome does not depend on the class around it. A private name outside a class is an error of the real parse, as before.allow_in. It is always true at this point: the?sets it for the true branch, and the mark that starts the attempt is set at the same place. A debug assertion states it, and the key leaves it out.awaitandyieldstate of the enclosing function, and itsthisandsuperflags.parse_arrow_bodyreplaces the first two. The other flags only log errors, and a logged error does not change the outcome.Check with a build of the parser that is not committed: at each attempt, compute the outcome again with the memo off, under each of 864 combinations of the states that are not in the key (
awaitandyieldof the enclosing function,is_this_disallowed,allow_super_call,allow_super_property,is_top_level,is_return_disallowed, strict mode of the scope). 51,347 attempts, 44,363,808 runs: no outcome changes, andallow_inis true at all of them. (Run on the version of this PR that still had the private name state in the key.)Other parsers: tsc's
notParenthesizedArrowis a set of token offsets, with noasyncstate. tsc 6.0.2 reports TS1005 forx = y ? async (h) : R => await /x + /.y ? z/g ? 0 : (a) : T => d;. Main and this PR printx = y ? async(h) : (R) => await / x + /.y ? z/g ? 0 : (a) => d;.Not in this PR
emitDecoratorMetadata. Hardening follow-up: fix regressions from #46021 and more crashes #46270 fixed that: the attempt runs with the option off. This PR was a draft until then, because the second run of the attempt in a debug build repeated that write.a ? (b) : c => … : e2,560 deep is 48 KB and takes 3.6 GB). Another change owns that.EXPECTED_VERSIONgoes from 43 to 44, because the output oft2.tschanges. parser: keepasync(...)a call before a:in TypeScript #45743 and parser: a:after(b as B)or<B[]>(b)is not an arrow function return type (TypeScript) #46209 also set 44. A PR that merges later takes the next number.a ? (b) : c => d : eattempt inside another attempt keeps its arrow function #46311 (draft) makes a kept attempt the real parse and stores only failed attempts, by the:offset. By its diff, that memo has noasyncstate either, sot1.tsandt3.tsneed the key of this PR there too. The two PRs change the same function: the one that merges later must be rebased.Checks
asyncstate, the attempt without the private name line, two wrong shifts of the offset, a lookup that never hits, an outcome that is not stored, nothing stored, an entry appended out of order, a hit that reads entry 0, a hit that reads the neighbour entry, a hit that returns the other outcome, and the?withoutallow_in. The inputs of the changed test block catch 11 of 12 in both modes. The twelfth (the offset shifted by one bit, not two) is not a defect: one bit holds the two values thatallow_awaittakes here.size -A:.text82,170,617 to 82,171,129 B.nm -S: parser symbols +389 B (parse_paren_expr+171 B and +122 B in the two TypeScript copies,restore_parser_snapshot+48 B each). The JavaScript copies do not change.parse_paren_expr(sub rspplus pushes): 1,088 B to 1,120 B, and 1,104 B to 1,120 B in the scan-only copy (TypeScript). 448 B and 464 B in JavaScript, unchanged. The attempt is inlined into it, as on main.ulimit -s 1024): parentheses 504 to 495, chains of attempts 389 to 384.#[cold]function of its own. That took the snapshot out of the frame ofparse_paren_expr(1,088 B to 736 B, parentheses 505 to 633 at 1 MB), and made chains of attempts less deep (390 to 350). A review asked for no such trade, so the attempt is inlined again.test/bundler/transpiler/transpiler.test.js, debug build: 259 pass with this PR. With main, the changed block and the new test fail.test/bundler/transpiler/transpiler.test.js(259 pass),test/bundler/esbuild/ts.test.ts(59),test/cli/run/transpiler-cache.test.ts(24),test/bundler/transpiler/runtime-transpiler.test.ts(15),decorators.test.ts(25),decorator-metadata.test.ts(5),es-decorators.test.ts(427),scope-mismatch-panic.test.ts(21),test/bundler/bundler_regressions.test.ts(9),jsx-production.test.ts(32).