View Issue Details

IDProjectCategoryView StatusLast Update
0018234FPCCompilerpublic2010-12-20 16:17
ReporterAndrzej Borucki Assigned ToJonas Maebe  
PrioritynormalSeverityfeatureReproducibilityalways
Status resolvedResolutionduplicate 
Platformx86OSWindowsXP 
Product Version2.4.2 
Summary0018234: No warning if loop variable is used after for-loop without "break"
DescriptionValue after for loop sometimes is used to distinguish if was break.
In Delphi value is next to last bound, in FreePascal value is last bound.
If FreePascal solution has more sense, should be Delphi solution for
{$MODE Delphi}
or warning: value after loop may be undefined

Steps To Reproduce{$MODE Delphi}
var i: integer;
begin
  for i:=0 to 9 do;
  writeln(i);{delphi say 10, FreePascal say 9}
  for i:=0 to 9 do if i=9 then break;
  writeln(i);{delphi say 9, FreePascal say 9}
end.
TagsNo tags attached.
Fixed in Revision
FPCOldBugId0
FPCTarget
Attached Files

Relationships

duplicate of 0012508 new no warning for loop variable usage after "for" 

Activities

Vincent Snijders

2010-12-16 22:07

manager   ~0044274

See also http://www.hu.freepascal.org/lists/fpc-pascal/2007-March/013428.html

Jonas Maebe

2010-12-16 23:36

manager   ~0044276

The result is also undefined in Delphi (Kylix) if there is no "break":

$ cat tt3.pp
var i: integer;
begin
  for i:=0 to 9 do;
  writeln(i);{delphi say 10, FreePascal say 9}
  for i:=0 to 9 do if i=9 then break;
  writeln(i);{delphi say 9, FreePascal say 9}
end.


$ dcc tt3.pp
Borland Delphi for Linux Version 14.5
Copyright (c) 1983,2002 Borland Software Corporation
tt3.pp(4) Warning: FOR-Loop variable 'i' may be undefined after loop
tt3.pp(8)
9 lines, 0.00 seconds, 22172 bytes code, 3088 bytes data.

Adding a warning is possible, but it requires several changes (the simple "solution" would result in wrong warnings that the variable is not used at all in the function in case it's not used anymore after the loop; and it also needs different handling depending on whether there's a "break" inside the loop, as demonstrated above).

Andrzej Borucki

2010-12-18 07:30

reporter   ~0044317

Since the value may be undefined, if would better to cause not only warning but even error, if you use variable after loop (as well as the use of any local variable without initialization) ?

Jonas Maebe

2010-12-18 20:19

manager   ~0044354

> would better to cause not only warning but even error

There are some safety-critical languages that do this, but Pascal is not one of them. Furthermore, the current analysis by the compiler of which variables are initialised and which are not is far from perfect, so it would also cause problems with compiling valid code.

Issue History

Date Modified Username Field Change
2010-12-16 21:18 Andrzej Borucki New Issue
2010-12-16 22:07 Vincent Snijders Note Added: 0044274
2010-12-16 23:36 Jonas Maebe Note Added: 0044276
2010-12-16 23:37 Jonas Maebe FPCOldBugId => 0
2010-12-16 23:37 Jonas Maebe Summary Value after for loop in Delphi mode => No warning if loop variable is used after for-loop without "break"
2010-12-18 07:30 Andrzej Borucki Note Added: 0044317
2010-12-18 20:19 Jonas Maebe Note Added: 0044354
2010-12-20 16:17 Jonas Maebe Relationship added duplicate of 0012508
2010-12-20 16:17 Jonas Maebe Duplicate ID 0 => 12508
2010-12-20 16:17 Jonas Maebe Status new => resolved
2010-12-20 16:17 Jonas Maebe Resolution open => duplicate
2010-12-20 16:17 Jonas Maebe Assigned To => Jonas Maebe