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: rest element should deoptimize parameter values #5503

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/ast/nodes/shared/FunctionBase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import type ExportDefaultDeclaration from '../ExportDefaultDeclaration';
import Identifier from '../Identifier';
import * as NodeType from '../NodeType';
import RestElement from '../RestElement';
import type SpreadElement from '../SpreadElement';
import SpreadElement from '../SpreadElement';
import type VariableDeclarator from '../VariableDeclarator';
import { Flag, isFlagSet, setFlag } from './BitFlags';
import type { ExpressionEntity, LiteralValueOrUnknown } from './Expression';
Expand Down Expand Up @@ -100,6 +100,9 @@ export default abstract class FunctionBase extends NodeBase {
const parameter = this.params[position];
// Only the "this" argument arg[0] can be null
const argument = args[position + 1]!;
if (argument instanceof SpreadElement) {
this.deoptimizeParameterVariableValues();
}
if (hasRest || parameter instanceof RestElement) {
hasRest = true;
argument.deoptimizePath(UNKNOWN_PATH);
Expand Down
3 changes: 3 additions & 0 deletions test/function/samples/rest-element-argument/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = defineTest({
description: 'restelement argument should deoptimize parameter value'
});
21 changes: 21 additions & 0 deletions test/function/samples/rest-element-argument/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let hasArg0 = false;
let hasArg1 = false;
let hasArg2 = false;

function foo(arg0, arg1, arg2) {
if (arg0) {
hasArg0 = true;
}
if (arg1) {
hasArg1 = true;
}
if (arg2) {
hasArg2 = true;
}
}

foo(...['arg0', 'arg1']);

assert.equal(hasArg0, true);
assert.equal(hasArg1, true);
assert.equal(hasArg2, false);