Skip to content

Commit

Permalink
Deoptimize right side in for-of loops
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Apr 20, 2023
1 parent 09031e7 commit 77dcec3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/ast/nodes/ForOfStatement.ts
Expand Up @@ -3,7 +3,7 @@ import { NO_SEMICOLON, type RenderOptions } from '../../utils/renderHelpers';
import type { InclusionContext } from '../ExecutionContext';
import BlockScope from '../scopes/BlockScope';
import type Scope from '../scopes/Scope';
import { EMPTY_PATH } from '../utils/PathTracker';
import { EMPTY_PATH, UNKNOWN_PATH } from '../utils/PathTracker';
import type MemberExpression from './MemberExpression';
import type * as NodeType from './NodeType';
import type VariableDeclaration from './VariableDeclaration';
Expand Down Expand Up @@ -60,6 +60,7 @@ export default class ForOfStatement extends StatementBase {
protected applyDeoptimizations(): void {
this.deoptimized = true;
this.left.deoptimizePath(EMPTY_PATH);
this.right.deoptimizePath(UNKNOWN_PATH);
this.context.requestTreeshakingPass();
}
}
3 changes: 3 additions & 0 deletions test/function/samples/deoptimize-loop-element/_config.js
@@ -0,0 +1,3 @@
module.exports = {
description: 'deoptimizes the loop element when it is reassigned'
};
7 changes: 7 additions & 0 deletions test/function/samples/deoptimize-loop-element/main.js
@@ -0,0 +1,7 @@
const foo = [{ value: 1 }];

for (const item of foo) {
item.value = 0;
}

assert.ok(foo[0].value === 0 ? true : false);

0 comments on commit 77dcec3

Please sign in to comment.