Code generation error while declaring and initializing a variable to 0

Issue #174 invalid
Jarto Tarpio created an issue

There is a major bug in code generation when a variable is declared and initialized to 0. In the example code below, “var foo: Integer := 0” is completely eliminated while older versions of DWS properly set foo to 0.

procedure TestBug;
var sum: Integer;
begin
  for var i:=0 to 3 do begin
    var foo: Integer := 0; //This is eliminated by the compiler!
    for var j:=0 to 3 do begin
      inc(foo);
      inc(sum,foo);
    end;
  end;
  WriteLn(sum);
end;

This works:

procedure TestBug;
var sum: Integer;
begin
  for var i:=0 to 3 do begin
    var foo: Integer;
    foo := 0;
    for var j:=0 to 3 do begin
      inc(foo);
      inc(sum,foo);
    end;
  end;
  WriteLn(sum);
end;

As well as this:

procedure TestBug;
var sum, foo: Integer;
begin
  for var i:=0 to 3 do begin
    foo := 0;
    for var j:=0 to 3 do begin
      inc(foo);
      inc(sum,foo);
    end;
  end;
  WriteLn(sum);
end;

This elimination is also done only if the variable is set to 0. For example, this is not eliminated by the compiler:

var foo: Integer := 1;

Comments (2)

  1. Jarto Tarpio reporter

    I’ve been digging deeper and this bug actually exists in cwbudde’s DWS, so sorry for a false alarm. I’ll report it separately to Christian.

    In case you pull changes from him, this commit is causing the problem:

    004bf2ac0a8d8adf0a9090ac0396e27f80ad7917

    Removed an unnecessary double initialization of variables

  2. Log in to comment