From 8b0b49840d4f04f3d17a07038ad16a0f096e463d Mon Sep 17 00:00:00 2001 From: CountBleck Date: Sat, 9 Mar 2024 18:20:27 -0800 Subject: [PATCH] Compile incrementors in for loops within the initializer's flow 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. --- src/compiler.ts | 1 + tests/compiler/issues/2825.json | 5 +++++ tests/compiler/issues/2825.ts | 3 +++ 3 files changed, 9 insertions(+) create mode 100644 tests/compiler/issues/2825.json create mode 100644 tests/compiler/issues/2825.ts diff --git a/src/compiler.ts b/src/compiler.ts index 648b6fa24d..8e5ee85615 100644 --- a/src/compiler.ts +++ b/src/compiler.ts @@ -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) ); diff --git a/tests/compiler/issues/2825.json b/tests/compiler/issues/2825.json new file mode 100644 index 0000000000..f5f36f2649 --- /dev/null +++ b/tests/compiler/issues/2825.json @@ -0,0 +1,5 @@ +{ + "stderr": [ + "TS2304: Cannot find name 'j'." + ] +} \ No newline at end of file diff --git a/tests/compiler/issues/2825.ts b/tests/compiler/issues/2825.ts new file mode 100644 index 0000000000..810b1fcade --- /dev/null +++ b/tests/compiler/issues/2825.ts @@ -0,0 +1,3 @@ +for (let i = 0; i < 2; j++) { + let j = 0; +} \ No newline at end of file