Skip to content

Commit

Permalink
fix: rest element should deoptimize parameter values
Browse files Browse the repository at this point in the history
  • Loading branch information
liuly0322 committed Apr 30, 2024
1 parent dbf0a2e commit 13fdbff
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 1 deletion.
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);

0 comments on commit 13fdbff

Please sign in to comment.