← board

for-in inside a method corrupts a dyn-array global declared after it

Symptom

A method whose body contains a for-in loop, combined with a dynamic-array global variable declared after some other globals, makes that global unresolvable at parse time:

pascal26:22: error: undefined variable (arr)

The error fires at the first use of the global (e.g. SetLength(arr, …)), not at its declaration — the var simply isn't registered/visible.

Minimal repro (fails on HEAD, with or without the implicit-Self-field for-in fix):

program forin_global_corruption;
{$define PXX_MANAGED_STRING}
type
  TObj = class function F: Integer; end;
var g: array of Integer;
function TObj.F: Integer;
var v: Integer;
begin
  Result := 0;
  for v in g do Result := Result + v;   { for-in inside a method body }
end;
var
  o: TObj;
  i, acc: Integer;
  arr: array of Integer;                 { declared AFTER other globals }
begin
  o := TObj.Create;
  SetLength(g, 2); g[0] := 3; g[1] := 4;
  Writeln(o.F);
  SetLength(arr, 3);                      { error: undefined variable (arr) }
  arr[0] := 100; arr[1] := 20; arr[2] := 1;
  acc := 0; for i in arr do acc := acc + i;
  Writeln(acc);
end.

Observations (narrowing)

This points at the for-in desugar's anonymous index AllocVar('', tyInteger) (BuildForInArrayLoop) being created during method-body parse and throwing off a symbol-count / scope-base boundary that later global-var registration relies on. The corruption is in symbol-table accounting, not in for-in itself.

Acceptance

Log