Recent

Author Topic: Generic function specialization compilation error  (Read 513 times)

Laurent92

  • Newbie
  • Posts: 1
Generic function specialization compilation error
« on: November 27, 2024, 03:15:53 pm »
Hello,
I got this compilation error while experimenting with generic classes and functions and I have no idea what could have caused it.

Code: Text  [Select][+][-]
  1. Free Pascal Compiler version 3.2.2 [2021/05/16] for aarch64
  2. Copyright (c) 1993-2021 by Florian Klaempfl and others
  3. Target OS: Darwin for AArch64
  4. Compiling main.pas
  5. fgl.pp(1668,8) Note: Call to subroutine "function TFPGMap<System.ShortString,System.Pointer>.IndexOf(const AKey:ShortString):LongInt;" marked as inline is not inlined
  6. main.pas(44,1) Fatal: Internal error 2002071001
  7. Fatal: Compilation aborted
  8. Error: /usr/local/bin/ppca64 returned an error exitcode

Is this a bug of the compiler (should I open an issue on GitLab?) or is it simply not possible to do that with Objet Pascal?

Code: Pascal  [Select][+][-]
  1. program main;
  2.  
  3. uses
  4.   TypInfo, FGL;
  5.  
  6. type
  7.   TIndex = 0..99;
  8.   TGenericObjects = specialize TFPGMap<ShortString, Pointer>;
  9.  
  10.   generic TGenericObject<T> = class(TObject)
  11.   private
  12.     FList: array[TIndex] of T;
  13.   public
  14.     procedure Add(AIndex: TIndex; AElement: T);
  15.   end;
  16.  
  17.   TGenericObjectFactory = class(TObject)
  18.   public
  19.     generic function GetGenericObject<T>: specialize TGenericObject<T>;
  20.     generic procedure Add<T>(AIndex: TIndex; AElement: T);
  21.   private
  22.     FGenericObjects: TGenericObjects;
  23.   end;
  24.  
  25. generic function TGenericObjectFactory.GetGenericObject<T>: specialize TGenericObject<T>;
  26. var
  27.   GenericObjectType: PTypeInfo;
  28. begin
  29.   GenericObjectType := TypeInfo(T);
  30.   Result := specialize TGenericObject<T>(FGenericObjects[GenericObjectType^.Name]^);
  31. end;
  32.  
  33. generic procedure TGenericObjectFactory.Add<T>(AIndex: TIndex; AElement: T);
  34. var
  35.   GenericObject: specialize TGenericObject<T>;
  36. begin
  37.   GenericObject := specialize GetGenericObject<T>; // <-- this line causes the bug
  38.   GenericObject.Add(AIndex, AElement);
  39. end;
  40.  
  41. procedure TGenericObject.Add(AIndex: TIndex; AElement: T);
  42. begin
  43.   FList[Aindex] := AElement;
  44. end;
  45.  
  46. begin
  47. end.

Bart

  • Hero Member
  • *****
  • Posts: 5497
    • Bart en Mariska's Webstek
Re: Generic function specialization compilation error
« Reply #1 on: November 27, 2024, 03:21:28 pm »
No internal error using fpc main, so probably fixed.
Now someone should test fpc 3.2 fixes branch.

Bart

TRon

  • Hero Member
  • *****
  • Posts: 3787
Re: Generic function specialization compilation error
« Reply #2 on: November 27, 2024, 04:02:21 pm »
Now someone should test fpc 3.2 fixes branch.
No issues with fixes 3.2 but tested on x86_64 only.
I do not have to remember anything anymore thanks to total-recall.

 

TinyPortal © 2005-2018