Skip to content

bug(codegen): var declared in a constant-bound (unrolled) loop loses its value when read after the loop #2308

Description

@proggeramlug

Summary

A var declared inside a constant-bound (compile-time unrolled) counted loop and read after the loop loses its last-iteration value — the read sees the initial/undefined slot instead. Surfaced while fixing #1803 (PR #2307); it is independent of that fix and reproduces on main both with and without it.

Repro

function h(): number {
  let sum = 0;
  for (let i = 0; i < 3; i++) { var t = i * 2; sum += t; }
  return sum + t;   // t should be the last iteration's value (4)
}
console.log(h());
// node: 10  (sum 6 + t 4)
// perry: 6  (t read as 0)

Scope

Notes

Narrow/contrived (reading a loop-body var after the loop with constant bounds); filing for backlog completeness. Not a blocker for ajv (#1680) — its loops are for...in, not unrolled counted loops.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions