Specialization of a generic type declared with parameter restriction cannot be used as a parameter of a generic routine

Summary

FPC is unable to compile a generic routine if its result or any parameter is a specialization of a generic type in which the parameter is declared with a restriction, neither in objfpc mode nor in delphi mode.

System Information

  • Operating system: Windows, Linux
  • Processor architecture: x86, x86-64
  • Compiler version: 3.3.1-10802-g57107c33
  • Device: Computer

Steps to reproduce

Just try compiling the example project

Example Project

unit test_unit;
{$mode objfpc}{$h+}
interface
type
  generic TTestObj<T: class> = class(TObject)
    Field: T;
  end;

  generic function  Fun<T: class>(aParam: Integer): specialize TTestObj<T>;
  generic procedure Proc1<T: class>(aParam: Integer; out o: specialize TTestObj<T>);
  generic procedure Proc2<T: class>(o: specialize TTestObj<T>);

implementation

generic function Fun<T>(aParam: Integer): specialize TTestObj<T>;
begin
end;

generic procedure Proc1<T>(aParam: Integer; out o: specialize TTestObj<T>);
begin
end;

generic procedure Proc2<T>(o: specialize TTestObj<T>);
begin
end;

end. 

FPC output example

Hint: (11030) Start of reading config file /home/..../fpc/bin/x86_64-linux/fpc.cfg
Hint: (11031) End of reading config file /home/..../fpc/bin/x86_64-linux/fpc.cfg
Free Pascal Compiler version 3.3.1-10802-g57107c3310 [2022/04/05] for x86_64
Copyright (c) 1993-2022 by Florian Klaempfl and others
Note: (11020) Switching assembler to default source writing assembler
(1002) Target OS: Linux for x86-64
(3104) Compiling test.lpr
(3104) Compiling test_unit.pas
/home/..../bugs/gen_proc/test_unit.pas(18,63) Error: (4009) Class type expected, but got "T"
/home/..../bugs/gen_proc/test_unit.pas(18,18) Error: (3028) Overloaded functions have the same parameter list
/home/..../bugs/gen_proc/test_unit.pas(12,21) Error: (5088) Found declaration: Fun$1(LongInt):TTestObj$1<TEST_UNIT.Fun$1T>;
/home/..../bugs/gen_proc/test_unit.pas(23,72) Error: (4009) Class type expected, but got "T"
/home/..../bugs/gen_proc/test_unit.pas(28,51) Error: (4009) Class type expected, but got "T"
/home/..../bugs/gen_proc/test_unit.pas(35) Fatal: (10026) There were 5 errors compiling module, stopping
Fatal: (1018) Compilation aborted
Error: /home/..../fpc/bin/x86_64-linux/ppcx64 returned an error exitcode
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information