Repository navigation
fix(minifier): drop redeclarations of removed declarations - #29415
Conversation
Merging this PR will not alter performance
Comparing Footnotes
|
Monitor OxcCommit:
|
Minify Size
No size change across 109,516 files. Full report (Test Minify Size summary): top files and packages each way, and before/after diffs of the files that changed most. Output bytes of oxc's full minify pipeline (compress + mangle + whitespace) over monitor-oxc's |
6c560d8 to
1dc0ee9
Compare
15f297a to
0186b88
Compare
e17f6e5 to
1cedb33
Compare
c1d6971 to
5c12e37
Compare
Merge activity
|
When the minifier removed a declaration of a redeclared symbol, its entry in `Scoping::symbol_redeclarations` stayed. In `catch (a) { var a }` the stale entry kept the catch parameter looking redeclared (and, with #28739, linked to the enclosing var binding), so the catch binding was only removed when the output was minified a second time.
Closes #27548
### Example
Input ([playground for this example](`` https://playground.oxc.rs/?t=codegen&options=%7B%22run%22%3A%7B%22compress%22%3Atrue%2C%22whitespace%22%3Atrue%7D%7D&code=export+function+f%28%29%7Btry%7Bx%28%29%3B%7Dcatch%28a%29%7Bvar+a%3B%7D%7D); `` playground.oxc.rs runs the released Oxc version, so it shows the behavior before this PR):
```js
export function f(){try{x();}catch(a){var a;}}
```
`cargo run -p oxc_minifier --example minifier -- input.js --nospace --twice` prints the first run, then a second run on that output.
Before output (parent commit d4c3cbe):
```text
export function f(){try{x()}catch(a){}}
export function f(){try{x()}catch{}}
same = false
```
After output:
```text
export function f(){try{x()}catch{}}
export function f(){try{x()}catch{}}
same = true
```
Before, the first run keeps the unused catch binding `a` and only a second run removes it. After, it is removed on the first run and the output is stable.
### Fix
`symbol_redeclarations` is kept in sync by the existing drop bookkeeping:
- `DroppedSubtreeCollector` (used by every `drop_*` / `replace_*` helper) records `(symbol, binding span)` for declarations of redeclared symbols. A `var` redeclaring a catch parameter also declares the enclosing var binding, so both are recorded (`redeclared_symbols`).
- `KeepVar`, the only place that re-emits a dropped binding with its symbol and span, records what it re-emits, and retires the old declaration when it rebinds a re-emitted `var` to another symbol. `remove_unused_variable_declaration` records the `KeepVar` declarators it removes.
- At pass flush, removed declarations are dropped with `Scoping::remove_symbol_declaration`. The program is walked only when a removed declaration was also re-emitted, to keep those still present.
- A span shared by several declarations of the same symbol (e.g. synthesized `SPAN`s) cannot tell which one was dropped, so those declarations are never pruned (`has_unique_span`). Such input keeps the previous behavior: correct output, with the catch binding removed on a second run.
A stale entry also affects more than the catch binding. Several checks treat a non-empty `symbol_redeclarations` as a signal to stay conservative: parameter inlining (`inline.rs`), function-summary dead code removal (`remove_dead_code.rs`), single-use assignment folding (`minimize_statements.rs`) and declaration-derived value facts (`ecma_context.rs`). A stale entry only disables those checks, which is safe but leaves the table inexact. After this PR the table is exact again.
Debug builds check the walk gate, allocation-free, through the `RedeclaredBindings` visitor: at flush, no pruned declaration is still in the program unless `KeepVar` re-emitted it.
Compared with #27596: no walk over every redeclaration on every pass, and pruning is driven by the existing drop helpers.
### Performance
A version that walked the program on every pass dropping a redeclared declaration cost −3.98% on `minifier[kitchen-sink.tsx]` (CodSpeed); skipping the walk unless `KeepVar` re-emitted a removed declaration brought it to +0.20% instructions (Callgrind, kitchen-sink.tsx, before the shared-span handling was simplified, which only removed work). CodSpeed reports no performance change. Pass count and output on kitchen-sink.tsx are unchanged.
### Test plan
- `optional_catch_binding_after_removed_var` (fails before, passes after, with idempotency checks): the #27548 repro, repeated `var`, dead-code removal through `KeepVar`, nested catches, and cases where the catch must stay.
- `redeclarations_with_synthetic_spans`: every binding gets `SPAN`, so the debug check runs on shared-span declarations.
- `cargo test -p oxc_minifier`, `cargo clippy -p oxc_minifier --all-targets -- -D warnings`.
- Debug build over the minsize corpus with the debug check: no failures.
- `cargo minsize`: no size change. `cargo allocs`: kitchen-sink.tsx +1 sys alloc.
- Node runtime comparison of original vs minified (with and without `--mangle`) for 8 catch/`var` inputs, calling each export with `x()` throwing: identical results.
- Monitor Oxc: Test262 passes and Minify Size reports no change. The transformer, compressor, formatter and Runtime (svelte) suites also fail on other PRs' runs today; codegen, whitespace and mangler fail as in this PR's earlier runs.
AI assistance (OpenAI Codex; Claude Code with Claude Opus 5.5, reasoning effort high) was used to implement this change, write the tests and write this description. The Codex model and reasoning effort were not recorded.
5c12e37 to
c589002
Compare
Oxc now removes stale redeclaration records after dropping declarations. Regenerate the minified catch_var_self_import output, where three unused catch parameters disappear. The fixture's execution assertions pass before and after and the catch bindings still used by the program remain. First-bad-Oxc-Commit: c58900246158b0986888c8bd3f03661206703a78 Oxc-Revision: c58900246158b0986888c8bd3f03661206703a78
### 💥 BREAKING CHANGES - f573c93 syntax: [**BREAKING**] Remove `NumberBase` and `BigintBase` types (#29920) (overlookmotel) - b90b3aa ast: [**BREAKING**] Remove `base` field from `NumericLiteral` and `BigIntLiteral` (#29919) (overlookmotel) - 8c2abc4 diagnostics: [**BREAKING**] Implement `SourceCode` for `&T` (#29852) (overlookmotel) - 2bc8e89 diagnostics: [**BREAKING**] `NamedSource` support `&str` as name (#29851) (overlookmotel) ### 🚀 Features - 2384875 minifier: Remove redundant `| 0` under bitwise operators (#29991) (Cameron) - abf889f minifier: Flatten nested string concatenations (#30015) (Cameron) - 5f51bd3 minifier: Fold division with short numeric results (#29994) (Cameron) - 1102f24 minifier: Flatten nested untagged template literals (#29993) (Cameron) - 12897a0 minifier: Fold switch with one case and default into if-else (#29990) (Cameron) - f9e90de minifier: Fold numeric identities like `x - 0` and `x * 1` (#29926) (Dunqing) - c265eda minifier,ecmascript: Fold additive zero and safe offsets on int32/uint32 expressions (#29865) (Andre_Ramirez) - 37ec2e2 compat: Add standard decorators compatibility data (#29448) (Dunqing) - 22e4d0b runtime: Add standard decorator helpers (#29447) (Dunqing) - a67ac1f data_structures: Add brand types (#29756) (overlookmotel) ### 🐛 Bug Fixes - 38cd470 transformer: Share auto-accessor lowering and fix its private storage names (#29449) (Dunqing) - 6838760 packages/codegen: Parenthesize for-of targets that start with `let` (#30071) (Cameron) - 13dd5ac packages/codegen: Parenthesize comma expressions in enum initializers (#30070) (Cameron) - c4e4fcc packages/codegen: Parenthesize private `in` checks in for initializers (#30073) (Cameron) - 32882ef packages/codegen: Parenthesize comma expressions in export assignments (#30072) (Cameron) - 7d8998d packages/codegen: Parenthesize comma expressions in JSX containers (#30074) (Cameron) - c98b8b1 packages/codegen: Parenthesize TypeScript instantiation operands (#30068) (Cameron) - d615de4 codegen: Parenthesize TypeScript instantiation expressions used as operands (#30065) (Cameron) - 90b5af9 codegen: Keep `()` on minified `new` expressions with type arguments (#30062) (Cameron) - 4b1efd3 transformer/legacy-decorator: Move class self-references to alias symbol (#30056) (Dunqing) - adf284e mangler: Use debug names for private members (#30061) (Cameron) - bbe2fba transformer/legacy-decorator: Avoid class name shadowing in static setters (#30058) (Dunqing) - 7fa5d50 codegen: Keep comments from ending a line after code (#29814) (Yuji Sugiura) - 41dbc08 mangler: Keep names set by redeclarations with keepNames (#30055) (Dunqing) - 8921865 transformer/legacy-decorator: Keep static private brand checks on the original class (#30054) (Dunqing) - abb3593 transformer/legacy-decorator: Share anonymous default class bindings (#29753) (Dunqing) - bbc6772 lexer: Read << as a shift where TypeScript opens no type arguments (#30051) (Shanon Jackson) - 52b5506 transformer/class-properties: Preserve names of anonymous classes (#29931) (Dunqing) - 8488767 codegen: Parenthesize private `in` tests in `for` initializers (#30037) (Cameron) - 395913e parser: Parse `<=` after `as` and `satisfies` (#30028) (Cameron) - 9570bc8 parser: Require `class` keyword for decorated class export (#30027) (Cameron) - e9e80b1 codegen: Parenthesize for-of heads that print a leading `let` or `async` (#30025) (Cameron) - a54befb codegen: Avoid double parentheses around `as`/`satisfies` for-of heads (#30024) (Cameron) - 157673a codegen: Parenthesize comma expressions in export assignments (#30022) (Cameron) - bedf3f5 codegen: Parenthesize comma expressions in enum initializers (#30021) (Cameron) - 11bafb6 codegen: Parenthesize comma expressions in JSX containers (#30020) (Cameron) - 967a97c codegen: Print a computed key for renamed `__proto__` shorthand (#30019) (Cameron) - 64b7176 minifier: Preserve throwing `String.fromCodePoint` calls (#30014) (Cameron) - f17a216 ecmascript: Evaluate unary expressions in `ToNumber` (#30009) (Cameron) - 181dd40 ecmascript: Canonicalize RegExp flags in string conversion (#30008) (Cameron) - cf35a2e ecmascript: Respect shadowed `NaN` in strict equality (#30005) (Cameron) - 2d9246e minifier: Don't inline block-level vars visible to an outer direct eval (#29992) (Cameron) - 08023ad transformer: Keep TypeScript assign semantics for preserved classes (#29930) (Dunqing) - 32798b3 minifier: Don't treat reassigned `@__NO_SIDE_EFFECTS__` functions as pure (#29974) (oxc-captain[bot]) - 0167451 parser: Treat `import x = require()` as module syntax (#29908) (camc314) - e27f727 semantic: Align JSDoc nullable type suggestions with TypeScript (#29947) (camc314) - 831fbca transformer: Don't fold computed key assignments into non-computed method keys (#29912) (Dunqing) - 6db561f transformer: Keep correct keys on leftover fields with setPublicClassFields (#29911) (Dunqing) - e4e2ac4 parser,semantic: Reject invalid types in type arguments (#29945) (camc314) - bdbed9e transformer/async-to-generator: Avoid invalid and shadowing inferred function names (#29815) (YT) - a0f67fa parser: Parse TSX `<T extends U` as start of generic arrow function (#29910) (overlookmotel) - 8c84c5f parser: Never parse `<` as start of generic arrow function in JSX files (#29909) (overlookmotel) - f5022f2 lexer: Read yield and await by enclosing function in modules (#29890) (Shanon Jackson) - 168daf3 lexer: Keep async and generator scope across class members and generic async arrows (#29889) (Shanon Jackson) - 777d2d1 parser: Report import/export in unambiguous code resolved to script (#29902) (camc314) - e385fe8 parser: Don't treat `await` in function parameters and class fields as top-level await (#29901) (camc314) - 5442f8a parser: Report misplaced import/export before checking parse goal (#29898) (camc314) - c589002 minifier: Drop redeclarations of removed declarations (#29415) (Dunqing) - 24a1aaf parser: Discard comments from rewound speculative parses (#29847) (camc314) - 1aadbe7 isolated-declarations: Preserve const literal initializers through parentheses (#29041) (camc314) - a7ea78b transformer: Keep classes whose decorators are preserved (#29748) (Dunqing) - 5ece804 transformer/async-to-generator: Remove leading digits from inferred function names (#29816) (YT) - 15bdce3 parser: `parse_expression` produce correct error for overlong source (#29761) (overlookmotel) - b6ff506 react-compiler: Do not treat the receiver of a JSX ref property load as a ref (#29754) (Boshen) - 2f6a11f semantic: Keep catch parameters in the catch clause scope (#29635) (Dunqing) - 60718a1 minifier: Keep unused classes with a static computed `prototype` key (#27966) (baltasarblanco) - d59aa8e transformer/legacy-decorator: Apply static member decorators last (#27744) (Yunus Ozen) - 97307e3 transformer/legacy-decorator: Emit `String`/`Number` metadata for enums declared after the class (#29724) (Max Schwenk) - 3586c66 transformer: Preserve object rest assignment evaluation (#29645) (Boshen) - 4fb4887 allocator: Require Sync allocator for Drain Send (#26152) (Cameron) - f860f1b semantic: Resolve a private name to the enclosing class that declares it (#29714) (Nadir Ali) - 7b2b207 parser: Parse contextual keywords after relational expressions (#25097) (camc314) - b7e2beb minifier: Preserve indirect calls when inlining eval aliases (#29725) (Yunfei He) - 279185a react-compiler: Ignore ESLint suppressions when hooks and memo validations are enabled (#29655) (Boshen) - d69ba66 napi/parser: Walk skipped ESTree fields in lazy visitor (#29381) (Ryan Tsao) - f22757c transformer/class-properties: Preserve receivers in TS-wrapped private calls (#29684) (camc314) - 0e1ef65 transformer/exponentiation-operator: Scope default parameter temporaries (#29685) (camc314) ### ⚡ Performance - 209f078 mangler: Include digits in trailing character frequency order (#29437) (camc314) - 5e7abd1 parser: Remove unnecessary conversion when parsing BigInts (#29913) (overlookmotel) - 5ac3942 parser: Mark missing-digit lexer errors cold (#29806) (Boshen) - 892f086 various: Avoid copying source text in `OxcDiagnostic::render_with_source_code` callers (#29850) (overlookmotel) - 27777a9 diagnostics: Loosen lifetime bound on `OxcDiagnostic::with_source_code` (#29849) (overlookmotel) - a2eed1b parser: Read each comment once (#29794) (Boshen) - ec4090d semantic: Size the `Scoping` arena from symbol, reference and scope counts (#29809) (Boshen) - 7c39be0 minifier: Avoid rebuilding variable declarations when keeping all declarators (#29767) (camc314) - 0537b09 parser: Take the name of a JSX identifier from its token (#29746) (Boshen) - 1c2cd7c parser: Take the value of a string literal without escapes from its source text (#29745) (Boshen) - 1d84d3f parser: Take the value of JSX text from its source text (#29744) (Boshen) - 41e3075 parser: Check for the `<` of type arguments without a call (#29743) (Boshen) - 6e783b1 parser: Keep the token read after `import` (#29742) (Boshen) - eb9c7a4 parser: Parse `(<div />)` as a parenthesized expression directly (#29741) (Boshen) - 7acf20a parser: Parse JSX at the start of an expression without speculation (#29740) (Boshen) - aea847b estree: Merge compact `serialize_field` writes (#28399) (codspeed[bot]) - 414532b parser: Cache failed type argument speculation (#29518) (Boshen) - d91c961 parser: Take comments from the lexer without copying inline storage (#29623) (Boshen) - dda1781 parser: Finish plain strings and integers in their token handler (#29622) (Boshen) - 6f5d371 parser: Choose how to scan a block comment from its first bytes (#29621) (Boshen) - d80d731 parser: Do less work to record each comment (#29620) (Boshen) - ce2b7aa parser: Lex the token after a line break in its token handler (#29619) (Boshen) - 7833ea0 parser: Finish identifier and keyword tokens in their token handler (#29618) (Boshen) - bb08acb parser: Dispatch tokens through handlers which finish the token (#29617) (Boshen) - a1ed5af parser: Parse `(…)` once with a cover grammar for arrow functions (#29603) (Boshen) ### 📚 Documentation - 158fcf5 minifier: Fix duplicated word in comment (#30059) (Dunqing) - 3ff249b skills: Share PR title guidance with triagebot (#29980) (Cameron) - 983ebc7 ast: Clarify which keys `PropertyKey::static_name` supports (#29905) (camc314) - 1da6ea8 skills: Merge PR guidance into pr-guide skill (#29866) (Dunqing) - 9740083 minifier: Clarify when variable declarations are merged (#29768) (camc314) --------- Co-authored-by: Cameron <cameron.clark@hey.com>
Oxc now removes stale redeclaration records after dropping declarations. Regenerate the minified catch_var_self_import output, where three unused catch parameters disappear. The fixture's execution assertions pass before and after and the catch bindings still used by the program remain. First-bad-Oxc-Commit: c58900246158b0986888c8bd3f03661206703a78 Oxc-Revision: c58900246158b0986888c8bd3f03661206703a78
## Summary - Upgrade Oxc Rust dependencies to 0.160.0. - Upgrade @oxc-project/runtime, @oxc-project/types, oxc-parser, and oxc-transform to 0.160.0. - Update the Rust `url` dependency to 2.5.9 for the security check. [RUSTSEC-2026-0592](https://rustsec.org/advisories/RUSTSEC-2026-0592.html) ## Root causes ### oxc-project/oxc#29748 Oxc keeps class elements together when standard decorators or auto-accessors are preserved. The snapshots show decorated fields retaining their decorators and static field initialization staying inside the class. ### oxc-project/oxc#29415 Oxc clears stale redeclaration records when it drops declarations. The minified `catch_var_self_import` snapshot now omits three unused catch parameters and retains the bindings still used by the fixture. ### oxc-project/oxc#29919 Oxc removes the numeric and BigInt literal base fields. Rolldown updates the affected AST constructors while preserving literal values, raw text, and spans. ### oxc-project/oxc#29907 Oxc encodes whether a property key is computed in the `PropertyKey` variant. Rolldown updates key construction and inspection, preserves quoted and computed options in Vite import glob, and uses computed keys for own `__proto__` properties in JSON and namespace objects. The minified JSON snapshot now prints the computed strings with backticks. ### oxc-project/oxc#29930 Oxc restores TypeScript assignment semantics for public static fields in classes with preserved accessors. The auto-accessor fixtures emit in-place static blocks so assignments call inherited setters and preserve initializer order. ### oxc-project/oxc#29994 Oxc folds `3 / 6` to `.5` in the JavaScript constant and TypeScript enum entries of the cross-module constant-folding snapshot. Both outputs evaluate to the same values. ### oxc-project/oxc#29753 Oxc shares the class binding between static field lowering and legacy decoration for anonymous default exports. Two classes gain explicit names in the snapshot, and a decorator can read initialized static fields when targeting ES2015. ### oxc-project/oxc#29447 Oxc adds six standard decorator helpers to its runtime package. Regenerate the embedded ESM and CommonJS modules so Rolldown’s runtime plugin can load the helpers in standalone bundles.
When the minifier removed a declaration of a redeclared symbol, its entry in
Scoping::symbol_redeclarationsstayed. Incatch (a) { var a }the stale entry kept the catch parameter looking redeclared (and, with #28739, linked to the enclosing var binding), so the catch binding was only removed when the output was minified a second time.Closes #27548
Example
Input (playground for this example; playground.oxc.rs runs the released Oxc version, so it shows the behavior before this PR):
cargo run -p oxc_minifier --example minifier -- input.js --nospace --twiceprints the first run, then a second run on that output.Before output (parent commit d4c3cbe):
After output:
Before, the first run keeps the unused catch binding
aand only a second run removes it. After, it is removed on the first run and the output is stable.Fix
symbol_redeclarationsis kept in sync by the existing drop bookkeeping:DroppedSubtreeCollector(used by everydrop_*/replace_*helper) records(symbol, binding span)for declarations of redeclared symbols. Avarredeclaring a catch parameter also declares the enclosing var binding, so both are recorded (redeclared_symbols).KeepVar, the only place that re-emits a dropped binding with its symbol and span, records what it re-emits, and retires the old declaration when it rebinds a re-emittedvarto another symbol.remove_unused_variable_declarationrecords theKeepVardeclarators it removes.Scoping::remove_symbol_declaration. The program is walked only when a removed declaration was also re-emitted, to keep those still present.SPANs) cannot tell which one was dropped, so those declarations are never pruned (has_unique_span). Such input keeps the previous behavior: correct output, with the catch binding removed on a second run.A stale entry also affects more than the catch binding. Several checks treat a non-empty
symbol_redeclarationsas a signal to stay conservative: parameter inlining (inline.rs), function-summary dead code removal (remove_dead_code.rs), single-use assignment folding (minimize_statements.rs) and declaration-derived value facts (ecma_context.rs). A stale entry only disables those checks, which is safe but leaves the table inexact. After this PR the table is exact again.Debug builds check the walk gate, allocation-free, through the
RedeclaredBindingsvisitor: at flush, no pruned declaration is still in the program unlessKeepVarre-emitted it.Compared with #27596: no walk over every redeclaration on every pass, and pruning is driven by the existing drop helpers.
Performance
A version that walked the program on every pass dropping a redeclared declaration cost −3.98% on
minifier[kitchen-sink.tsx](CodSpeed); skipping the walk unlessKeepVarre-emitted a removed declaration brought it to +0.20% instructions (Callgrind, kitchen-sink.tsx, before the shared-span handling was simplified, which only removed work). CodSpeed reports no performance change. Pass count and output on kitchen-sink.tsx are unchanged.Test plan
optional_catch_binding_after_removed_var(fails before, passes after, with idempotency checks): the minifier: symbol_redeclarations are not removed after declaration is removed #27548 repro, repeatedvar, dead-code removal throughKeepVar, nested catches, and cases where the catch must stay.redeclarations_with_synthetic_spans: every binding getsSPAN, so the debug check runs on shared-span declarations.cargo test -p oxc_minifier,cargo clippy -p oxc_minifier --all-targets -- -D warnings.cargo minsize: no size change.cargo allocs: kitchen-sink.tsx +1 sys alloc.--mangle) for 8 catch/varinputs, calling each export withx()throwing: identical results.AI assistance (OpenAI Codex; Claude Code with Claude Opus 5.5, reasoning effort high) was used to implement this change, write the tests and write this description. The Codex model and reasoning effort were not recorded.