[fpc-pascal] FPC allows modifying loop variable

Graeme Geldenhuys mailinglists at geldenhuys.co.uk
Fri Jul 22 14:49:49 CEST 2016


Hi,

I just discovered a surprising bug in my application. I had a variable
"b" defined which I used in a FOR loop. FPC forbids you to modify that
loop variable inside the for loop - which is understandable.

However, the loop called a local procedure, and that local procedure
modified the loop variable. FPC never complained about it. Is this a FPC
bug?

Here is a test program to show the issue.

=========================
program test;

{$mode objfpc}{$H+}

uses
  SysUtils, classes;

type
  TMyTest = class(TObject)
  public
    procedure Run;
  end;

procedure TMyTest.Run;
var
  b: integer;

  procedure ModifyB;
  begin
    b := 5; // FPC never complains - BUG?
  end;

begin
  for b := 1 to 10 do
  begin
    writeln(b);
    // b := 5;   // FPC will give a compiler error. CORRECT.
    ModifyB;
  end;
end;


var
  t: TMyTest;
begin
  t := TMyTest.Create;
  t.Run;
  t.Free;
end.
=========================
Regards,
  Graeme

-- 
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key:  http://tinyurl.com/graeme-pgp



More information about the fpc-pascal mailing list