FPC allows modifying loop variable

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

FPC allows modifying loop variable

Graeme Geldenhuys-6
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
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: FPC allows modifying loop variable

Graeme Geldenhuys-6
On 2016-07-22 13:49, Graeme Geldenhuys wrote:
> 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?


I forgot to mention, I used FPC 2.6.4.


Regards,
  Graeme

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: FPC allows modifying loop variable

Jonas Maebe-2
In reply to this post by Graeme Geldenhuys-6
On 22/07/16 14:49, Graeme Geldenhuys wrote:
> 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?

FPC has no interprocedural dataflow analysis that could catch something
like that.


Jonas
_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: FPC allows modifying loop variable

Graeme Geldenhuys-6
On 2016-07-22 14:04, Jonas Maebe wrote:
> FPC has no interprocedural dataflow analysis that could catch something
> like that.

I just tested with Delphi 7, and FPC is Delphi compatible. ;-)  D7
doesn't seem to have inter-procedural dataflow analysis either.

Regards,
  Graeme

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal