View Issue Details

IDProjectCategoryView StatusLast Update
0034543FPCCompilerpublic2019-10-07 09:51
ReporterMarģersAssigned To 
PrioritynormalSeverityminorReproducibilityalways
Status newResolutionopen 
Platformx86_64OSlinuxOS Version
Product Version3.3.1Product Build 
Target VersionFixed in Version 
Summary0034543: for loop Error: range check error while evaluating constants
DescriptionWith range check ON for loop gives rang check error, when loop counter is unsigned integer. From and to values should be variables unsigned integers same size or grater.

Compiler parameters -O3 or -O4 should be provided. My guess, it's optimization error.
Steps To Reproducecompile with -O3 or -O4

{$r+}
procedure range_check_fail;
var v : word;
    vTo : word;
    vNo : word;
begin
     vTo:=3;
     vNo:=0;
     for v:=vNo to vTo do {Error: range check error while evaluating constants (-1 must be between 0 and 65535)}
     begin
     end;
end;
TagsNo tags attached.
Fixed in Revision
FPCOldBugId
FPCTarget
Attached Files
  • testrngx.pas (343 bytes)
    program test_range_check_for_loop;
    {$r+}
    
    procedure range_check_fail;
    var v : dword;
        vTo : word;
        vNo : word;
    begin
         vTo:=3;
         vNo:=0;
         for v:=vNo to vTo do {Error: range check error while evaluating constants (-1 must be between 0 and 65535)}
         begin
         end;
    end;
    
    begin
         range_check_fail;
         writeln('Ok');
    end.
    
    testrngx.pas (343 bytes)

Activities

Marģers

2018-11-12 12:05

reporter  

testrngx.pas (343 bytes)
program test_range_check_for_loop;
{$r+}

procedure range_check_fail;
var v : dword;
    vTo : word;
    vNo : word;
begin
     vTo:=3;
     vNo:=0;
     for v:=vNo to vTo do {Error: range check error while evaluating constants (-1 must be between 0 and 65535)}
     begin
     end;
end;

begin
     range_check_fail;
     writeln('Ok');
end.
testrngx.pas (343 bytes)

Marco van de Voort

2018-11-12 16:01

manager   ~0111935

iterations is vTo-VNo+1. How does this work if the typing is unsigned?

Thaddy de Koning

2018-11-12 16:30

reporter   ~0111936

Last edited: 2018-11-12 16:32

View 3 revisions

I can confirm this. -O3 and higher fails. -O2 works.
If I change the type to SmallInt it works in -O3/4.
This is a bug. (maybe upto?)

Marģers

2018-11-12 16:43

reporter   ~0111937

Last edited: 2018-11-12 16:48

View 5 revisions

@Marco van de Voort, is it expected behavior?
As long vNo <= vTo math vTo-vNo+1 is fine for unsigned type.

Thaddy de Koning

2018-11-13 15:00

reporter   ~0111950

No. Optimizations should have no - or documented limited - side effects. This is not one of those.
Code with optimizations should at a minimum work as code without optimization.

Marco van de Voort

2018-11-13 15:10

manager   ~0111951

I might have misread, and read vno and vto the other way around.

(
In the delphi dialect (contrary to TP), loops with 0 or negative iterations are ignored. Therefore the number of iterations must be calculated,

This subtraction is made in the loopvar type to check, and negative iterations in an unsigned loopvar are thus dangerous.
)

As for the actual bug I'll leave it for one of the compiler devels.

Thaddy de Koning

2018-11-14 17:22

reporter   ~0111975

whom should note it also can reproduced on arm-linux.

Thaddy de Koning

2018-11-20 11:05

reporter   ~0112083

Last edited: 2018-11-20 11:11

View 4 revisions

Strange is that (given r40356, O3 or O4) this works:
procedure range_check_fail;
var v : word;
    vTo : word = 3;
    vNo : word = 0;
begin
     for v:=vNo to vTo do {Error: range check error while evaluating constants (-1 must be between 0 and 65535)}
     begin
     end;
end;

And this doesn't work:

procedure range_check_fail;
var v : word;
    vTo : word;
    vNo : word;
begin
     vTo := word(3);
     vNo := word(0);
     for v:=vNo to vTo do {Error: range check error while evaluating constants (-1 must be between 0 and 65535)}
     begin
     end;
end;

Seems this is unrelated to recent range fixes, because:
procedure range_check_fail;
var v : word;
    vTo : word;
    vNo : word;
begin
     vTo := 4; // +1;
     vNo := 1; // +1;
     for v:=vNo to vTo do {Error: range check error while evaluating constants (-1 must be between 0 and 65535)}
     begin
     end;
end;

That works in any mode, so it looks like the compiler optimizes with a wrong iteration (either internal first starts or ends with x-1).

Marģers

2019-10-03 19:40

reporter   ~0118289

Last edited: 2019-10-03 19:45

View 2 revisions

Since r42275 this bug is fixed in trunk.
3.2.0 still has this problem.

Can be resolved as fixed.

Bart Broersma

2019-10-06 13:42

reporter   ~0118376

Is that merged to 3.2 fixes as well?

Marģers

2019-10-07 09:42

reporter   ~0118388

Last edited: 2019-10-07 09:51

View 2 revisions

r42275 is not merged to 3.2 fixes. look like, it's not scheduled for merge to 3.2. (r42275 is fix for 0035753).

Issue History

Date Modified Username Field Change
2018-11-12 12:05 Marģers New Issue
2018-11-12 12:05 Marģers File Added: testrngx.pas
2018-11-12 16:01 Marco van de Voort Note Added: 0111935
2018-11-12 16:30 Thaddy de Koning Note Added: 0111936
2018-11-12 16:31 Thaddy de Koning Note Edited: 0111936 View Revisions
2018-11-12 16:32 Thaddy de Koning Note Edited: 0111936 View Revisions
2018-11-12 16:43 Marģers Note Added: 0111937
2018-11-12 16:45 Marģers Note Edited: 0111937 View Revisions
2018-11-12 16:46 Marģers Note Edited: 0111937 View Revisions
2018-11-12 16:46 Marģers Note Edited: 0111937 View Revisions
2018-11-12 16:48 Marģers Note Edited: 0111937 View Revisions
2018-11-13 15:00 Thaddy de Koning Note Added: 0111950
2018-11-13 15:10 Marco van de Voort Note Added: 0111951
2018-11-14 17:22 Thaddy de Koning Note Added: 0111975
2018-11-20 11:05 Thaddy de Koning Note Added: 0112083
2018-11-20 11:05 Thaddy de Koning Note Edited: 0112083 View Revisions
2018-11-20 11:09 Thaddy de Koning Note Edited: 0112083 View Revisions
2018-11-20 11:11 Thaddy de Koning Note Edited: 0112083 View Revisions
2019-10-03 19:40 Marģers Note Added: 0118289
2019-10-03 19:45 Marģers Note Edited: 0118289 View Revisions
2019-10-06 13:42 Bart Broersma Note Added: 0118376
2019-10-07 09:42 Marģers Note Added: 0118388
2019-10-07 09:51 Marģers Note Edited: 0118388 View Revisions