[fpc-devel] Gaps in a non-contiguous enum

Ondrej Pokorny lazarus at kluug.net
Tue Aug 16 12:51:39 CEST 2022


Am 16.08.2022 um 09:37 schrieb Michael Van Canneyt via fpc-devel:
> On Tue, 16 Aug 2022, Juha Manninen via fpc-devel wrote:
>> On Thu, Aug 11, 2022 at 9:37 PM Martin Frb via fpc-devel <
>> fpc-devel at lists.freepascal.org> wrote:
>>
>>> fpc code is still "unexpected".
>>> It takes into account the MinValue
>>>
>>> type TMyEnum = (a=5, b=7);
>>>
>>> GetEnumName(pt, 5) // a
>>> GetEnumName(pt, 6) // b // should be 7
>>>
>>> if it is just the nth name, then it should start at 0 (or if needs 
>>> be at
>>> 1).
>>>
>>
>> And function GetEnumValue() is buggy.
>
> It is not buggy. It does not support enumerateds with gaps, because 
> RTTI does not support enumerateds with gaps.
> It never has.

Well it is buggy, but a little bit differently: FPC indeed creates 
PropInfo for enumerated types with gaps!

Allow me to slightly edit your demo code:

program RTTITest;

{$ifdef DCC}
// Delphi
{$APPTYPE CONSOLE}
{$else}
// FPC
{$mode objfpc}{$h+}
{$endif}

uses
   SysUtils, Classes, TypInfo;

type
   TFruitMore = (fmApple=1, fmOrange=3, fmBanana=5, fmGrapes, fmPear);

   TMyClass = class(TPersistent)
   private
     FFruit: TFruitMore;
   Published
     property Fruit : TFruitMore Read FFruit Write FFruit;
   end;

var
   PI : PPropInfo;
   aClass : TMyClass;
   TypeData : PTypeData;
begin
   aClass:=TMyClass.Create;
   TypeData:=GetTypeData(aClass.ClassInfo);
   Writeln('PropCount: ', TypeData^.PropCount); // Delphi writes 0, FPC 
writes 1

   PI:=GetPropInfo(aClass,'Fruit',tkAny);
   if PI=nil then
     Writeln('Property info is null')
   else
   begin
     Writeln('Property name: ', PI^.Name);
     Writeln('Property kind: ', GetEnumName(TypeInfo(TTypeKind), 
Ord(PI^.PropType^.Kind)));
   end;

   Readln;
end.

--- code end

Run it in Delphi and FPC and compare the outputs:

Delphi output:
PropCount: 0
Property info is null

FPC output:
PropCount: 1
Property name: Fruit
Property kind: tkEnumeration

As you can see FPC creates a valid PropInfo for the property with valid 
TypeInfo (PI^.PropType) of the kind tkEnumeration! This is definitely wrong.

So the solution is either:

1.) don't create PropInfo for the Fruit property like Delphi.
- or -
2.) PropType must not be thEnumeration. It should be either tkInteger or 
a new custom value like tkCEnumeration or whatever. Because 
thEnumeration means it has the enumeration TypeInfo with GetEnumName and 
GetEnumValue.

Ondrej



More information about the fpc-devel mailing list