View Issue Details

IDProjectCategoryView StatusLast Update
0034232FPCCompilerpublic2019-03-30 16:35
ReporterBenito van der Zander Assigned To 
Status newResolutionopen 
Product Version3.1.1 
Summary0034232: sizeof does not work on nested generic type
DescriptionHere sizeof appears to be 0

program Project1;
{$mode objfpc}{$H+}
generic TTest<TKey, TValue> = packed object
    TPair = packed record
      key: TKey;
      value: TValue;
    TPairSizeEquivalent = packed array[1..sizeof(TPair)] of byte;
TTestStringString = specialize TTest<string, string>;


because the array definition raises

test.pas(19,56) Error: High range limit < low range limit
test.pas(19,56) Error: Upper bound of range is less than lower bound

but it should have sizeof(TKey) + sizeof(TValue)
Fixed in Revision
Attached Files


Benito van der Zander

2018-09-09 14:24

reporter   ~0110590

Last edited: 2018-09-09 14:26

View 2 revisions

I thought this was caused by nesting or packed object, but it still fails the same when taken apart:

program Project1;
{$mode objfpc}{$H+}
generic TTest<TPair> = class
  type TPairSizeEquivalent = array[1..sizeof(TPair)] of byte;

generic TPair<TKey, TValue> = record
  key: TKey;
  value: TValue;

generic TTestWrapper<TKey, TValue> = class
    TTest2 = specialize TTest<specialize TPair<TKey, TValue>>;
TTestStringString = specialize TTestWrapper<string, string>;


Ryan Joseph

2019-03-23 18:56

reporter   ~0114996

Benito, this bug will be fixed when they approve my "constants in generics" patch (issue 0035140). Consider it resolved but you have to wait until the patch is applied.

Sven Barth

2019-03-24 17:04

manager   ~0115023

@Ryan: that has nothing to do with constants.

The problem is that the compiler does not know the sizes of the generic parameter types TKey and TValue, thus it assumes 0, thus the size of TPair is also 0. Maybe it would be better if the compiler would at least assume 1, as zero sized types aren't *that* common :/

Ryan Joseph

2019-03-24 17:10

reporter   ~0115026

It doesn't have anything to do but I encountered this bug when working on constants also so I fixed it.

There is now a node flag which is used to tell the compiler that types are derived from generics so the range check error is suppressed. I needed this for constants also that are undefined at the time of declaration.

Sven Barth

2019-03-30 16:35

manager   ~0115131

Could you separate these nodeflag changes from your generic constants patch?

Issue History

Date Modified Username Field Change
2018-09-06 14:44 Benito van der Zander New Issue
2018-09-09 11:15 Cyrax Tag Attached: generics
2018-09-09 14:24 Benito van der Zander Note Added: 0110590
2018-09-09 14:26 Benito van der Zander Note Edited: 0110590 View Revisions
2019-03-23 18:56 Ryan Joseph Note Added: 0114996
2019-03-24 17:04 Sven Barth Note Added: 0115023
2019-03-24 17:10 Ryan Joseph Note Added: 0115026
2019-03-30 16:35 Sven Barth Note Added: 0115131