View Issue Details

IDProjectCategoryView StatusLast Update
0036065FPCCompilerpublic2019-09-13 17:36
ReporterZaaphodAssigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Platformi386OSWindowsOS Version7 & 10
Product Version3.3.1Product Build 
Target VersionFixed in Version 
Summary0036065: Illegal Counter Variable in For Loop if initialized in variable declaration
DescriptionInitializing the control variable in the variable declaration as in the following example causes a compiler error:
 Error: Illegal counter variable

Var
   I:Byte=0;
Begin
   For I := 1 to 100 do
      Begin
         Write(I,' ');
      End;
End.
Steps To ReproduceTry to run this program:

Var
   I:Byte=0;
Begin
   For I := 1 to 100 do
      Begin
         Write(I,' ');
      End;
End.

It should just work, but it gives a compiler error:
 Error: Illegal counter variable

It should be functionally equivalent to:

Var
   I:Byte;
Begin
   I:=0;
   For I := 1 to 100 do
      Begin
         Write(I,' ');
      End;
End.

Which runs fine.
Additional InformationControl variables should not be required to be dedicated to a for loop.
The programmer should be free to re-cycle the control variable if they wish and write a program like this:
Var
   I:Byte=0;
Begin
   While I < 5 do
      Begin
         Inc(I);
         Writeln(I);
      End;
   I := Pos('Q','ABCDEFGHIJKLMNOPQRSTUVWXYZ');
   Writeln('I=',I);
   For I := 1 to 100 do
      Begin
         Write(I,' ');
      End;
   Writeln;
   I:=67;
   Repeat
      Write(I,' ');
      Dec(I);
   Until I<=32;
End.

but this also raises the error: Error: Illegal counter variable, however the variable is completely legitimate, initializing it in the VAR declaration with ALL the other variables should be allowed, even though initializing a control variable really serves no purpose, it should be allowed anyway... There are bad consequences to not initializing variables, and it is good programming to ALWAYS initialize ALL variables whether they need it or not. and if 99.9% of your variables are initialized in the VAR declaration it does not make sense to be required to make an exception to initialize a shared variable that will be eventually used in a for loop elsewhere.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files

Activities

Marco van de Voort

2019-09-11 16:02

manager   ~0118036

Note that this depending on mode is a global variable initialization which will be unsafe if your code runs multiple times.

If you are doing this for "always initialization" reasons, initialize in code, since in declaration initialization is ambiguous in FPC.

Bart Broersma

2019-09-11 18:56

reporter   ~0118039

As discussed on ML, Delphi does not allow this as well...

Marco van de Voort

2019-09-11 21:07

manager   ~0118043

And Delphi documents it afaik, requiring loopvar to be "simple type".

Thaddy de Koning

2019-09-13 10:43

reporter   ~0118058

Which is also wrong, it needs to be ordinal.

Marco van de Voort

2019-09-13 16:10

manager   ~0118063

Sorry, simple variable.

Serge Anvarov

2019-09-13 17:36

reporter   ~0118064

As far as I know, at some point Delphi developers wanted to prohibit the use of global variables for loops, but because of the old (simplest) code that used them, they accept it (backward compatibility), but began to issue a warning.
It is clear that for new code, where initialization is allowed at declaration, this exception is not supported (bad style is not supported).
FPC in this sense is compatible with Delphi.

Issue History

Date Modified Username Field Change
2019-09-11 15:57 Zaaphod New Issue
2019-09-11 16:02 Marco van de Voort Note Added: 0118036
2019-09-11 18:56 Bart Broersma Note Added: 0118039
2019-09-11 21:07 Marco van de Voort Note Added: 0118043
2019-09-13 10:43 Thaddy de Koning Note Added: 0118058
2019-09-13 16:10 Marco van de Voort Note Added: 0118063
2019-09-13 17:36 Serge Anvarov Note Added: 0118064