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

fix(es/minifier): Respect span hygiene from expr simplifier #6899

Merged
merged 13 commits into from Feb 7, 2023

Conversation

kdy1
Copy link
Member

@kdy1 kdy1 commented Feb 5, 2023

Description:

Related issue:

@kdy1 kdy1 added this to the Planned milestone Feb 5, 2023
@kdy1 kdy1 self-assigned this Feb 5, 2023
@kdy1
Copy link
Member Author

kdy1 commented Feb 5, 2023

Investigation

Playground

This looks like an issue of span hygiene.

After the first pass, the input becomes

console.log("The result should be 0. And it is:", (function(n, e) {
    var r = Math.abs(n - e);
    function t(e) {
        return e < n;
    }
    function u(n) {
        return n > e;
    }
    function c(n1) {
        ;
        ;
        return n1 < n || n1 > e;
    }
    return {
        length: r,
        max: e,
        min: n,
        constrain: function(r) {
            ;
            return c(r) ? r < n ? n : e : r;
        },
        reachedAny: c,
        reachedMax: u,
        reachedMin: t,
        removeOffset: function(n) {
            return r ? n - r * Math.ceil((n - e) / r) : n;
        }
    };
})(0, 3).constrain(-1));

but if I pass this to the minifier it passes.


Investigation 2

I verifed the behavior using NOINLINE.

Playground

console.log("The result should be 0. And it is:", function(n, e) {
    var r = Math.abs(n - e);

    function t(e) {
        return e < n
    }

    function u(n) {
        return n > e
    }

    function c(n) {
        return t(n) || u(n)
    }
    return {
        length: r,
        max: e,
        min: n,
        constrain: function(r) {
            return /*#__NOINLINE__*/c(r) ? (/*#__NOINLINE__*/t(r) ? n : e) : r
        },
        reachedAny: c,
        reachedMax: u,
        reachedMin: t,
        removeOffset: function(n) {
            return r ? n - r * Math.ceil((n - e) / r) : n
        }
    }
}(0, 3).constrain(-1));

Investigation 3

Playground

inlining of t(r) was the cause

Investigation 4

https://play.swc.rs/?version=1.3.32&code=H4sIAAAAAAAAA22QTQqDQAyF9z3Fw9UMqFi60%2F7QA3TXC%2Bg4rYJkYCbTTfXuVWtVitkE8r73QqIMOdPouDFPEdwrDaudbxiuMr4pUWgkMa5UombULg1CPDwprg0JCqEl3jv09cotLE645VzFeeEEIerVbDeqPwtYzI6hrGZvCRpH0Djs%2FngvaIMnnKE3ebXJ8zBt2zFtsq3UBVf9L9jmNaXLkXadt3KpQblA8LcT0uEZKewMd%2BG0qxNJiIOM53gR7aXMPhRXXXZ6AQAA&config=H4sIAAAAAAAAA22TTZLjIAyF78I6i6ksetEHmF2fgSIgHLoBuZBI4krl7iMSj42d3qGPZ%2Fnph7v6Jqs%2B72o0haC0E02ZzU19KrDJkC1hZHUQmSBvIsHjoNiUAbhJ6PjneJTriEgwCw4qhRz81JJZTGMBonaWj2qCzLToTCl4XcMTYgST34E2pENmGKCslxZjNCOBvpgNTVJKIMxbVhmcHguOPc4ucMAs4QIdGKcturUYFwpYDhfoRJJHRJnE3hY6ONVhaI2cKVxMrIZXGdyeDZHfLuiMgVj72ll%2Boa3fF9tUG7wuwLWsub4x5K3mB0DMRkOUTYId9zKUPfpFFrKXkfK0UJl35yzDICXqEPxaZ%2FMOhUOXqoCrFlqh9g1uLFNwoMF76XsHr4HtuUvH0wjo11j6ZzoDr1DvdmymbSXe4F8xxv1gZp4Mn%2FeMpnTC%2BJYiAZ%2FRvWFpBuMeFtnn27inNTuQZoPrLip1Yds71ow6tie4dFt2Q77SQ8TTus%2FyVpPJw7KljwbQ1QbuzwY%2BH%2FGHeqyP9v9q0dcsFAM%2FGa9ZPf4B9HvVti4EAAA%3D

Investigation 5

  TRACE  visit_mut_stmt: {
    var e#22;
    return function(n#5) {
        ;
        ;
        return n#5 < n#2 || n#5 > e#2;
    }(r#6) ? r#6 < n#2 ? n#2 : e#2 : r#6;
}

@kdy1 kdy1 changed the title fix(es/minifier): Fix minification of booleans fix(es/minifier): Respect span hygiene from expr simplifier Feb 5, 2023
Copy link
Member Author

@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_transforms_optimization

@kdy1 kdy1 requested a review from jridgewell February 5, 2023 05:43
@kdy1 kdy1 marked this pull request as ready for review February 5, 2023 05:43
@kdy1 kdy1 mentioned this pull request Feb 5, 2023
@kdy1
Copy link
Member Author

kdy1 commented Feb 7, 2023

Skipping the review as this is a very trivial change

@kdy1 kdy1 enabled auto-merge (squash) February 7, 2023 04:26
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 432dca6 into swc-project:main Feb 7, 2023
@kdy1 kdy1 deleted the issue-6896 branch February 7, 2023 05:14
@kdy1 kdy1 modified the milestones: Planned, v1.3.33 Feb 8, 2023
@kdy1 kdy1 modified the milestones: Planned, v1.3.34 Feb 8, 2023
@swc-project swc-project locked as resolved and limited conversation to collaborators Jun 5, 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.

Optimisation breaks boolean statement
2 participants