Recent

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

Laurent92

  • Newbie
  • Posts: 5
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: 5510
    • 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: 3946
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.

Laurent92

  • Newbie
  • Posts: 5
Re: Generic function specialization compilation error
« Reply #3 on: January 22, 2025, 10:33:25 am »
When I add a class type restriction to the generic methods, it compiles without error.

Maybe this helps the compiler because T can be anything without any restrictions.

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

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. Assembling main
  7. Linking main
  8. ld: warning: -multiply_defined is obsolete
  9. -macosx_version_min has been renamed to -macos_version_min
  10. 50 lines compiled, 0.6 sec
  11. 1 note(s) issued
  12.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 5858
  • Compiler Developer
Re: Generic function specialization compilation error
« Reply #4 on: January 23, 2025, 10:17:13 pm »
Now someone should test fpc 3.2 fixes branch.
No issues with fixes 3.2 but tested on x86_64 only.

The internal error only exists in some of the code generators (including aarch64, but excluding x86_64), so this is likely a platform specific issue...

Laurent92

  • Newbie
  • Posts: 5
Re: Generic function specialization compilation error
« Reply #5 on: January 27, 2025, 11:25:37 am »
@PascalDragon
Ok, thanks the info!  :)

 

TinyPortal © 2005-2018