Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

compress: Fix unsound optimization of new RegExp(…) #7091

Merged
merged 2 commits into from Mar 17, 2023

Conversation

andersk
Copy link
Contributor

@andersk andersk commented Mar 16, 2023

In #4932, an optimization was added that transforms new RegExp(…) to RegExp(…). It seems to have been motivated by this sentence in the ECMAScript specification:

Thus the function call RegExp(…) is equivalent to the object creation expression new RegExp(…) with the same arguments.

But that sentence turns out to be wrong. It contradicts the detailed steps in the following section of the specification, as well as the empirical behavior of JavaScriptCore, SpiderMonkey, and V8:

> r = /a/;
/a/
> RegExp(r) === r
true
> new RegExp(r) === r
false

Restrict this optimization to the case where we can prove one of the first two arguments is a string.

Fixes #6941.

Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

swc-bump:

  • swc_ecma_minifier

crates/swc_ecma_minifier/src/compress/pure/misc.rs Outdated Show resolved Hide resolved
crates/swc_ecma_minifier/src/compress/pure/misc.rs Outdated Show resolved Hide resolved
In swc-project#4932, an optimization was added that transforms `new RegExp(…)` to
`RegExp(…)`.  It seems to have been motivated by this sentence in the
ECMAScript specification:

“Thus the function call RegExp(…) is equivalent to the object creation
expression new RegExp(…) with the same arguments.”

But that sentence turns out to be wrong.  It contradicts the detailed
steps in the following section of the specification, as well as the
empirical behavior of JavaScriptCore, SpiderMonkey, and V8:

    > r = /a/;
    /a/
    > RegExp(r) === r
    true
    > new RegExp(r) === r
    false

Restrict this optimization to the case where we can prove one of the
first two arguments is a string.

Fixes swc-project#6941.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
Copy link
Member

@kdy1 kdy1 left a comment

Choose a reason for hiding this comment

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

Thank you!


swc-bump:

  • swc_ecma_minifier

@kdy1 kdy1 enabled auto-merge (squash) March 17, 2023 01:23
@kdy1 kdy1 added this to the Planned milestone Mar 17, 2023
Copy link
Collaborator

@swc-bot swc-bot left a comment

Choose a reason for hiding this comment

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

Automated review comment generated by auto-rebase script

@kdy1 kdy1 merged commit 493a4f7 into swc-project:main Mar 17, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.41 Mar 17, 2023
@andersk andersk deleted the new-regexp branch March 17, 2023 08:53
@swc-project swc-project locked as resolved and limited conversation to collaborators Apr 16, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Minifier should not drop new for RegExp
3 participants