Skip to content

fix(ecmascript): resolve identifier value type via tracked constants#22234

Merged
Dunqing merged 3 commits into
oxc-project:mainfrom
TheAlexLichter:fix/optional-chain-undefined-base-fold
May 18, 2026
Merged

fix(ecmascript): resolve identifier value type via tracked constants#22234
Dunqing merged 3 commits into
oxc-project:mainfrom
TheAlexLichter:fix/optional-chain-undefined-base-fold

Conversation

@TheAlexLichter
Copy link
Copy Markdown
Member

@TheAlexLichter TheAlexLichter commented May 7, 2026

value_type for Expression::Identifier returned Undetermined for any non-global identifier, even when the binding had zero writes and a known constant initializer. This causes code like slot?.() on a let slot; with all writers tree-shaken away not being folded to void 0, leaving dead code in the output.

This PR changes that by checking GlobalContext::get_constant_value_for_reference_id for non-global identifier references and mapping the resulting ConstantValue to a ValueType via a new private helper.

Once the identifier resolves to a tracked nullish value, the existing nullish branch in fold_chain_expr collapses the chain to void 0. The same lookup also works for the opposite case (#21923).

The lookup is gated by making sure there are no write references, so anything that is being written anywhere keeps its Undetermined type.

Closes rolldown/rolldown#9281
Related (but does not resolve) #21923

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 7, 2026

Merging this PR will not alter performance

✅ 48 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing TheAlexLichter:fix/optional-chain-undefined-base-fold (4faa0a3) with main (c27a8cf)2

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

  2. No successful run was found on main (dcc6d38) during the generation of this report, so c27a8cf was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@TheAlexLichter TheAlexLichter marked this pull request as ready for review May 7, 2026 17:39
@TheAlexLichter TheAlexLichter requested a review from Dunqing May 7, 2026 20:47
Dunqing added 3 commits May 18, 2026 16:45
`value_type` returned `Undetermined` for any non-global identifier, even
when the binding had zero writes and a known constant initializer. As a
result `slot?.()` on a `let slot;` (with all writers tree-shaken away)
was not folded to `void 0`, leaving dead optional calls in the output.

Consult `get_constant_value_for_reference_id` for non-global identifier
references and map the resulting `ConstantValue` to a `ValueType`. This
lets `fold_chain_expr` (and other consumers of `value_type`) recognise
read-only `let`/`const` bindings whose value is statically known.

Closes rolldown/rolldown#9281

# Conflicts:
#	crates/oxc_minifier/tests/peephole/dead_code_elimination.rs
The Identifier branch in `Expression::value_type` cloned the tracked
ConstantValue (potential BigInt / owned-String heap clone) only to
read its discriminant.

Add `GlobalContext::value_type_for_reference_id` and a
`ConstantValue::value_type` projection, so the minifier impl can
borrow the tracked value and skip the clone. The existing
`get_constant_value_for_reference_id` path is unchanged for callers
that need the full value.

Extend the dead-code-elimination tests with Null (via `?.`) and
non-nullish (String/BigInt via `??`) cases.
@Dunqing Dunqing force-pushed the fix/optional-chain-undefined-base-fold branch from 2bcb1c8 to 4faa0a3 Compare May 18, 2026 08:49
Copy link
Copy Markdown
Member

@Dunqing Dunqing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! I sent a few commits that polished and fixed CI.

@Dunqing Dunqing merged commit 0f26de6 into oxc-project:main May 18, 2026
37 checks passed
@TheAlexLichter TheAlexLichter deleted the fix/optional-chain-undefined-base-fold branch May 18, 2026 09:18
camc314 pushed a commit that referenced this pull request May 18, 2026
### 🐛 Bug Fixes

- 0f26de6 ecmascript: Resolve identifier value type via tracked
constants (#22234) (Alexander Lichter)
- c27a8cf minifier: Normalize `{ x: x }` shorthand so adjacent-if merge
is idempotent (#22401) (Dunqing)
- e431a0e parser: Break extends clause loop on fatal error (#22517)
(Boshen)
- e9ec7c6 minifier: Fold optional chains by base nullishness (#22236)
(Alexander Lichter)
- e6090e7 transformer: Keep enum IIFE when a non-inlinable value
reference remains (#22501) (Dunqing)
- 931b7d6 transformer: Inline const enum members through type-cast
wrappers (#22500) (Dunqing)
- b9615b2 codegen: Preserve string quotes in require() calls during
minification (#22475) (zennnnnnn11)
- c73c159 transformer/async-to-generator: Reparent parameter initializer
scopes (#22507) (camc314)
- ecfd3ca transformer/async-to-generator: Move only parameter bindings
(#22503) (camc314)
- 3ce3431 transformer/explicit-resource-managment: Preserve shadowed
for-head block (#22451) (camc314)

### ⚡ Performance

- ce92c6c semantic: `#[inline]` `Scoping::get_binding` (#22414)
(Dunqing)
- 98be95c regular_expression: Track regex flags via bitflags (#22427)
(Boshen)
- dbbc059 jsdoc: Skip should_attach_jsdoc when no remaining comments
(#22409) (Boshen)
- 217d7d8 minifier: Index `SymbolValues` by `SymbolId` (#22441)
(Dunqing)
- d782b78 minifier: Use BitSet for LiveUsageCollector live references
(#22425) (Boshen)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: optional call not tree shaken

2 participants