fix(react_compiler): align memoization cache-slot allocation with Babel#24157
Merged
Conversation
`lower_expression` had no arm for `Expression::RegExpLiteral`, so every regex literal fell through the catch-all and was lowered to an `undefined` primitive. This dropped the regex from the output (e.g. `x.replace(undefined, "")`) and, because a primitive does not allocate, denied it the reactive scope that `babel-plugin-react-compiler` gives it. The result was one fewer `_c()` cache slot per affected function, shifting all subsequent `$[i]` indices by -1. The HIR variant `InstructionValue::RegExpLiteral`, its codegen, `may_allocate`, aliasing/type inference and all visitors were already implemented; only the lowering arm was missing. Add it (mirrors TS `BuildHIR` `case 'RegExpLiteral'`). A RegExp literal now lowers to a real value and receives its own reactive scope, matching Babel's sentinel-memoized nested scope.
Merging this PR will not alter performance
Comparing Footnotes
|
Boshen
marked this pull request as ready for review
July 5, 2026 01:22
Boshen
added a commit
that referenced
this pull request
Jul 6, 2026
### 🚀 Features - 260425f semantic/examples: Include unresolved references (#24214) (camc314) - 2d9b0b3 minifier: Fold boolean-literal ternary branches in value contexts (#24110) (Dunqing) - 61fbf10 ast: Implement `ReplaceWith` on all AST types (#24013) (overlookmotel) - 7db7a29 allocator: Add `ReplaceWith` trait (#24012) (overlookmotel) - 4eb074e mangler: Add `reserved` option for names that must not be mangled (#24041) (Dunqing) - 2e62012 data_structures: Add `StringExt` trait (#24006) (overlookmotel) - 60e7160 minifier: Drop side-effect-free IIFEs whose result is unused (#23967) (Dunqing) - 26dd9e2 ast: Add method to widen inherited enum ref to parent ref (#23961) (overlookmotel) ### 🐛 Bug Fixes - e8b50ee transformer: Clean up semantics for stripped TypeScript syntax (#24180) (camc314) - d966d0b react_compiler: Remove clippy allows (#24168) (Boshen) - 854ef8d react_compiler: Compile generic functions instead of over-bailing on type-param hoisting (#24158) (Boshen) - 093586c react_compiler: Align memoization cache-slot allocation with Babel (#24157) (Boshen) - 09c8f59 react_compiler: Normalize snapshot fixture paths (#24142) (camc314) - f13df97 react_compiler: Drop stray empty statement from catch bindings (#24133) (Boshen) - cb2a505 react_compiler: Codegen destructuring reassignment targets (#24131) (Boshen) - b82c394 react_compiler: Propagate codegen invariants instead of emitting empty bodies (#24128) (Boshen) - 5771982 react_compiler: Render unchanged programs as source in fixture snapshots (#24129) (Boshen) - 4b16e1a transformer/async-to-generator: Preserve direct eval scope flags (#24136) (camc314) - 4e9194f react_compiler: Lower `delete obj.prop` to Property/ComputedDelete (#24123) (Boshen) - 0b25582 ast: Type binding node `typeAnnotation` as `TSTypeAnnotation | null` (#23113) (Boshen) - 018c0e5 transformer: Hoist lowered async declarations (#22770) (camc314) - 652fbaf mangler: Keep names of destructured exported bindings (#24036) (Dunqing) - e274415 minifier: Don't drop global calls that throw despite pure arguments (#23917) (Dunqing) - 59abb30 minifier: Only merge string literals in `try_fold_add` when the inner operator is `+` (#23622) (Jerry Zhao) ### ⚡ Performance - c5ca77b transformer: Avoid cloning refresh options (#24191) (camc314) - bf1a151 react_compiler: Compile out debug printers (#24184) (Boshen) - abb44a0 transformer: Build fixed object-rest arguments (#24190) (camc314) - a4db731 isolated_declarations: Use `ReplaceWith` instead of `TakeIn` (#24016) (overlookmotel) - ff10855 transformer: Use `ReplaceWith` instead of `TakeIn` (#24015) (overlookmotel) - bd49aff ecmascript: Avoid heap-allocating Math.min/max/imul operands (#23941) (Lawrence Lin) - e4b708b react_compiler: Skip compiled files before prefilters (#24171) (Boshen) - c59f2fe rust: Return impl ExactSizeIterator from slice-backed accessors (#24144) (Boshen) - 5d6d04a codegen: SWAR-skip boring byte runs in sourcemap line/column scan (#24023) (Boshen) - a55e0be traverse: Reduce string operations in `get_var_name_from_node` (#24007) (overlookmotel) - e6d48e1 transformer/nullish_coalescing: Move cold path into separate function (#23989) (overlookmotel) - c4e35b5 transformer/object_rest_spread: Pre-allocate capacity in `Vec` (#23988) (overlookmotel) - 527b8e5 transformer/decorators: Narrow type earlier (#23987) (overlookmotel) ### 📚 Documentation - 30d17f5 allocator: Clarify docs for `TakeIn::take_in_box` (#24093) (overlookmotel) - 675e6a8 ast: Correct doc comment for `PrivateFieldExpression` (#24008) (overlookmotel) - e4c30e6 minifier: Explain what `dce` mode means (#23994) (Dunqing) - 37cbf88 ast_macros: Document fields of `StructDetails` (#23959) (overlookmotel) - 4de3e54 ast: Correct doc comment (#23948) (overlookmotel) Co-authored-by: Boshen <1430279+Boshen@users.noreply.github.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Root cause
On ~57 ecosystem files, oxc allocated one fewer
_c()cache slot thanbabel-plugin-react-compilerin a function (e.g. oxc_c(25)where Babel emits_c(26); oxc[26,40]where Babel[27,40]), shifting every subsequent$[i]index by −1.The construct responsible is the regex literal. In
react_compiler_lowering/build_hir.rs,lower_expressionhad no arm foroxc::Expression::RegExpLiteral, so every regex literal fell through the catch-all that "bails to undefined":Two consequences:
useId().replace(/:/g, "")compiled tot1.replace(undefined, ""), andpattern.test(value)compiled toundefined.test(value).undefinedprimitive does not allocate, somay_allocatenever gave it a reactive scope. Babel treats a regex literal as an allocation and memoizes it (a nestedSymbol.for("react.memo_cache_sentinel")scope when it has no deps), reserving one extra slot. That missing slot was the −1.All the downstream machinery for regex was already implemented — the HIR variant
InstructionValue::RegExpLiteral, its codegen (codegen_reactive_function.rs),may_allocate(returnstrue), aliasing/type inference, and every visitor. Only the lowering arm was missing.The fix
Add the single missing arm in
lower_expression, mirroring TSBuildHIR'scase 'RegExpLiteral':The regex now lowers to a real value and receives its own reactive scope, matching Babel's sentinel-memoized nested scope. Minimal repro output now equals Babel byte-for-byte:
Before/after
_c(N)on ecosystem filescreate-better-t-stack/.../charts/dash-tail-stroke.tsx_c(25)_c(26)create-better-t-stack/.../charts/area-chart.tsx_c(26),_c(40)_c(27),_c(40).../day-picker.tsx_c(77)_c(78).../DynamicLink.tsx_c(15)_c(16).../Image.jsx_c(23)_c(24)Across the 420 regex-containing ecosystem source files: 48 went from mismatch → matching Babel's slot multiset, with 0 regressions (files that matched Babel before still match). The handful still differing do so for unrelated pre-existing reasons (whole extra/missing scopes), not the regex slot.
Snapshot note
Exactly one fixture snapshot changes:
regexp-literal.snap— the dedicated regex fixture, whose old golden captured theundefined.test(value)bug. The new snapshot is byte-identical to the upstream Babelregexp-literal.expect.mdgolden (const pattern = /foo/g; … pattern.test(value)).CI=1 cargo test -p oxc_react_compileris green;cargo fmt/cargo clippy --all-targets -D warningsclean.