Skip to content

fix(es): avoid incorrect instanceof optimizations - #12110

Open
Leo (teamleaderleo) wants to merge 6 commits into
swc-project:mainfrom
teamleaderleo:fix/instanceof-observable-effects
Open

fix(es): avoid incorrect instanceof optimizations#12110
Leo (teamleaderleo) wants to merge 6 commits into
swc-project:mainfrom
teamleaderleo:fix/instanceof-observable-effects

Conversation

@teamleaderleo

@teamleaderleo Leo (teamleaderleo) commented Aug 10, 2026

Copy link
Copy Markdown

Description:

Closes #12111.

Avoid folding instanceof from operand shape alone. The operator may invoke Symbol.hasInstance, and an invalid right-hand operand may throw, so folds such as object-like values to true or primitive-like values to false are not generally valid.

For example:

({ __proto__: null }) instanceof Object

evaluates to false, but could previously fold to true.

This keeps SWC's existing Terser-compatible treatment of discarded instanceof expressions unchanged. Tests under tests/terser are unchanged.

The change removes the operand-shape-based instanceof folds in the minifier and expression simplifier and adds SWC-owned regressions for cases where the result is used.

in / pure_getters are unchanged.

BREAKING CHANGE:

None.

Validation:

cargo fmt --all -- --check
cargo clippy -p swc_ecma_transforms_optimization --all-targets -- -D warnings
cargo clippy -p swc_ecma_minifier --all-targets -- -D warnings
focused instanceof used-result minifier fixture

@teamleaderleo
Leo (teamleaderleo) requested review from a team as code owners August 10, 2026 15:32
@changeset-bot

changeset-bot Bot commented Aug 10, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 9f838a5

The changes in this PR will be included in the next version bump.

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@CLAassistant

CLAassistant commented Aug 10, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@teamleaderleo
Leo (teamleaderleo) marked this pull request as draft August 10, 2026 15:34
@teamleaderleo

Leo (teamleaderleo) commented Aug 10, 2026

Copy link
Copy Markdown
Author

Updated the draft.

@codspeed-hq

codspeed-hq Bot commented Aug 10, 2026

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 200 untouched benchmarks
⏩ 61 skipped benchmarks1


Comparing teamleaderleo:fix/instanceof-observable-effects (9f838a5) with main (394c7c9)2

Open in CodSpeed

Footnotes

  1. 61 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 (d7d7434) during the generation of this report, so 394c7c9 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report.

@teamleaderleo Leo (teamleaderleo) changed the title fix(es): preserve observable instanceof semantics during optimization fix(es): preserve observable instanceof evaluation Aug 10, 2026
@teamleaderleo
Leo (teamleaderleo) force-pushed the fix/instanceof-observable-effects branch from 3724575 to d9cc58e Compare August 10, 2026 16:34
@teamleaderleo
Leo (teamleaderleo) marked this pull request as ready for review August 10, 2026 16:44
@teamleaderleo
Leo (teamleaderleo) marked this pull request as draft August 10, 2026 16:45
@teamleaderleo
Leo (teamleaderleo) force-pushed the fix/instanceof-observable-effects branch from d9cc58e to 0f2ad51 Compare August 10, 2026 16:47

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d9cc58ec36

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread crates/swc_ecma_transforms_optimization/tests/instanceof.rs Outdated
@teamleaderleo
Leo (teamleaderleo) force-pushed the fix/instanceof-observable-effects branch 2 times, most recently from 9a7abca to 551c0d2 Compare August 10, 2026 17:03
Preserve observable instanceof evaluation across effect extraction, simplification, minifier ignored-result handling, and dead-branch cleanup. Remove stale instanceof fold eligibility, add fixture-based regression coverage, and update the affected expectations.
@teamleaderleo
Leo (teamleaderleo) force-pushed the fix/instanceof-observable-effects branch from 551c0d2 to a8ee1a6 Compare August 10, 2026 17:47
@teamleaderleo
Leo (teamleaderleo) marked this pull request as ready for review August 10, 2026 18:28

@Austaras Austaras left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As I stated in related issue

@teamleaderleo

Copy link
Copy Markdown
Author

Ok, so we keep the existing Terser assumptions for the unused instanceof case, but the other changes are acceptable?

@teamleaderleo Leo (teamleaderleo) changed the title fix(es): preserve observable instanceof evaluation fix(es): avoid incorrect instanceof constant folding Aug 15, 2026
@teamleaderleo

Copy link
Copy Markdown
Author

I updated this PR. I kept the existing discarded-result behavior and reduced the PR to the incorrect instanceof constant folding.

@@ -1,4 +1,4 @@
foo() instanceof bar();

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This seems wrong

@teamleaderleo Leo (teamleaderleo) Aug 15, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Okay, my interpretation: keep the existing discarded instanceof behavior generally,

but leave the full expression intact when one of its operands comes from an expression SWC already marked pure.

I updated the PR that way.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Sorry, I haven't seen foo has been marked as pure. foo() should be removed in this case.

The general idea is not to change any test case under tests/terser.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Thank you for clarifying. I restored the tests/terser expectations and removed the pure-marked instanceof exception. The PR is back to only fixing the incorrect constant folding.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Despite being useless, could you restore some harmless correct constant folding like 1 instanceof Object?

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c1ffb993a0

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "Codex (@codex) review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "Codex (@codex) address that feedback".

Comment thread crates/swc_ecma_minifier/src/compress/optimize/mod.rs Outdated
@teamleaderleo Leo (teamleaderleo) changed the title fix(es): avoid incorrect instanceof constant folding fix(es): avoid incorrect instanceof optimizations Aug 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

Optimizer can miscompile or discard instanceof evaluation

3 participants