View Issue Details

IDProjectCategoryView StatusLast Update
0037650FPCCompilerpublic2020-09-05 03:42
ReporterRyan Joseph Assigned ToSven Barth  
Status closedResolutionfixed 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0037650: String range error with generic constants
DescriptionUsing the "string" type with generic constants triggers a range check error. Similar to static arrays the range checking should suppressed until the specialization phase.
Steps To Reproduce{$mode objfpc}

program test;

  generic TMyClass<const U: integer> = class
    // ERROR: String length must be a value from 1 to 255
    type TKey = string[U];
TagsNo tags attached.
Fixed in Revision46766
Attached Files


Ryan Joseph

2020-08-28 05:42

reporter   ~0125165

I'll preparing a patch for this myself which I will submit shortly.

Ryan Joseph

2020-08-28 06:03

reporter   ~0125166

I think this one small change is enough to fix it. This correctly triggers the range checking error while the type is being specialized.
patch.diff (838 bytes)   
diff --git a/compiler/pexpr.pas b/compiler/pexpr.pas
index 9b0757b2d0..2d3689af13 100644
--- a/compiler/pexpr.pas
+++ b/compiler/pexpr.pas
@@ -114,7 +114,13 @@ implementation
-                if (tordconstnode(p).value<=0) then
+                { the node is a generic param while parsing a generic def
+                  so disable the range checking for the string }
+                if assigned(current_genericdef) and 
+                  not current_genericdef.is_specialization and 
+                  (nf_generic_para in p.flags) then
+                  tordconstnode(p).value:=255;
+                if tordconstnode(p).value<=0 then
patch.diff (838 bytes)   

Thaddy de Koning

2020-08-28 09:08

reporter   ~0125168

The patch seems to work, indeed, did not yet test possible side effects (zero based strings?)

Ryan Joseph

2020-08-30 17:46

reporter   ~0125219

Just following up on this. I think the patch is safe but it needs to be vetted at usual. Who here has the authority to actually apply these patches? I have another bug fix patch related to generics that's been sitting there for well over a month. Not sure if I'm supposed to follow up or just wait for a response, even if it takes many months.

jamie philbrook

2020-08-30 19:08

reporter   ~0125224

So in the above example displayed, I don't see {$H+} for huge strings, is this intended ? Otherwise you have short strings...

Ryan Joseph

2020-08-31 04:33

reporter   ~0125229

{$H+} doesn't affect anything. All the patch does is disable ranging checking for short strings with generic constant parameters. This is similar to what happens with array[0..U] but I simply forgot to apply this for short string types.

Sven Barth

2020-09-04 17:45

manager   ~0125374

Sorry, it took me a while to find the time for this. I've applied your patch with slight adjustments (this way it will also work once nested generics are allowed).

Please test and close if okay.

Issue History

Date Modified Username Field Change
2020-08-28 05:42 Ryan Joseph New Issue
2020-08-28 05:42 Ryan Joseph Note Added: 0125165
2020-08-28 06:03 Ryan Joseph Note Added: 0125166
2020-08-28 06:03 Ryan Joseph File Added: patch.diff
2020-08-28 09:08 Thaddy de Koning Note Added: 0125168
2020-08-30 17:46 Ryan Joseph Note Added: 0125219
2020-08-30 19:08 jamie philbrook Note Added: 0125224
2020-08-31 04:33 Ryan Joseph Note Added: 0125229
2020-09-04 17:45 Sven Barth Assigned To => Sven Barth
2020-09-04 17:45 Sven Barth Status new => resolved
2020-09-04 17:45 Sven Barth Resolution open => fixed
2020-09-04 17:45 Sven Barth Fixed in Version => 3.3.1
2020-09-04 17:45 Sven Barth Fixed in Revision => 46766
2020-09-04 17:45 Sven Barth FPCTarget => -
2020-09-04 17:45 Sven Barth Note Added: 0125374
2020-09-05 03:42 Ryan Joseph Status resolved => closed