Skip to content

Commit

Permalink
Compile incrementors in for loops within the initializer's flow
Browse files Browse the repository at this point in the history
It turns out that incrementors were compiled with the body's flow, which
meant that the incrementor had access to local variables declared in the
body. Now, incrementors no longer have access to such variables.

Fixes #2825.
  • Loading branch information
CountBleck committed Mar 12, 2024
1 parent de174c5 commit 8b0b498
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/compiler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2600,6 +2600,7 @@ export class Compiler extends DiagnosticEmitter {
if (possiblyLoops) {
let incrementor = statement.incrementor;
if (incrementor) {
this.currentFlow = flow;
bodyStmts.push(
this.compileExpression(incrementor, Type.void, Constraints.ConvImplicit | Constraints.WillDrop)
);
Expand Down
5 changes: 5 additions & 0 deletions tests/compiler/issues/2825.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"stderr": [
"TS2304: Cannot find name 'j'."
]
}
3 changes: 3 additions & 0 deletions tests/compiler/issues/2825.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
for (let i = 0; i < 2; j++) {
let j = 0;
}

0 comments on commit 8b0b498

Please sign in to comment.