-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Faulty compression leads to arrays with holes / Removes assignments passed by value #6039
Comments
A bit more info:
let arr = [];
function foo() {
let walker = 0;
// this works
const bar = (defaultValue) => {
const myIndex = walker;
walker += 1;
if (arr.length < myIndex + 1) {
arr[myIndex] = defaultValue;
}
}
return bar
}
const bar = foo();
bar(null);
console.log(arr); // [null]
let arr = [];
function foo() {
let walker = 0;
// this works
return function bar(defaultValue) {
const myIndex = walker;
walker += 1;
if (arr.length < myIndex + 1) {
arr[myIndex] = defaultValue;
}
}
}
const bar = foo();
bar(null);
console.log(arr); // [null] The closure seems to necessary, moving Last but not least a playground link |
Seems like this issue is partially fixed - |
Oh I think this can be an issue of |
**Description:** `var_initialized` should be `true` even if the declaration of variable comes after its usage. **Related issue:** - Closes #6039.
This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Describe the bug
Alright, I've got my case:
When compiled with
swc
using this.swcrc
file:Outputs code that causes the walker to
+1
before theif
-statement that assigns the first default value. So for the first item we are setting the index 1, which creates a whole on the array.The final log outputs:
{ arr: [ <1 empty item>, null, null, null, null ] }
, but since the very beginning there's a hole at the beginning of the array.package.json
used:If we set
"compress": false,
on the.swcrc
file, then the bug goes away, aka the last log would be :{ arr: [ null, null, null, null ] }
- no log ever shows the hole in the array.What does this mean for Next.js? Well that perhaps a new version with a fixed version of SWC is needed to be able to use the minifier. I'll try to report the bug to SWC as best as I can so that they can fix it.
This was first observed with libraries that use this custom React hook: https://github.com/react-component/overflow/blob/master/src/hooks/useBatchFrameState.tsx - You see the core of the issue is that:
The compression removes
myIndex
and replaces it withwalkingIndex
, which means that by the time the if statement runs, the indexing value is at1
, and we setdefaultValue
as second place, leaving a whole behind in the array. Reference: vercel/next.js#41125Input code
Config
Expected behavior
Same behaviour between
jsc.minify.compress
set tofalse
and set totrue
. In this case, that when the variable can be passed as value, and not reference, the compressor should not optimise away theintermediate
holder.In the above,
a
ought to survive compression because it gets passedn
by value.Actual behavior
The compressor incorrectly removes the variable that received it's assignment by value, as if it has been done by reference, which impacts actual runtime behaviour.
Version
1.3.4
Additional context
No response
The text was updated successfully, but these errors were encountered: