fix(ecmascript): skip array length evaluation if there are any spread elements #13162
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
oxc/crates/oxc_ecmascript/src/constant_evaluation/mod.rs
Lines 501 to 502 in 4d0a05e
If there are spread elements in an array, say
[...[1, 2, 3]], thenarr.elements.len()no longer represents the value of.lengthproperty of the array, which will cause mis-optimization.PoC of the bug
test.js:This bug was originally discovered in vitejs/rolldown-vite#375.
Maybe sometimes the
.lengthcan be evaluated recursively, but here we just skip the evaluation as a quick fix.What's more, I think it would be better to add some tests like
test_eval("[1, 2, 3].length", Some(Number(3.0)))andtest_eval("[...a].length", None), but I cannot find a suitable place and found it's not trivial to implement oneCtxforevaluate_valuein test files. Please let me know if you have any comments. Thanks.