Skip to content

Error: Identifier not found "specialize" in except..end block

Summary

Explicit specialization of a generic function/procedure/class doesn't compile if found inside of except..end block.

System Information

  • Operating system: Windows 10x64
  • Processor architecture: x86-64 (AMD Ryzen 5)
  • Compiler version: Free Pascal Compiler version 3.3.1-15296-g6a28ac53da [2024/02/27] (using Lazarus 3.99 (rev main_3_99-1537-g0261f3b5ee))
  • Device: Computer

Steps to reproduce

  1. Create a try..except block.
  2. Specialize a generic function/procedure/class inside except..end block.
  3. Compile
  4. Error: Identifier not found "specialize"

Example Project

generic procedure Foo<T>;
begin
  WriteLn('Bar');
end;

begin
  try
    specialize Foo<Integer>; // this one works
  except
    specialize Foo<Integer>; // Error: Identifier not found "specialize"
  end;
end.

A downloadable project for convenience: generic-specialization-bug.zip

Note that if we "wrap" specialization into a normal procedure/function, it works without issues:

generic procedure Foo<T>;
begin
  WriteLn('Bar');
end;

procedure Wrapper; inline;
begin
  specialize Foo<Integer>;
end;

begin
  try
    Wrapper; // this one works
  except
    Wrapper; // this works too
  end;
end.

Also compiles without issues in Delphi mode: https://forum.lazarus.freepascal.org/index.php/topic,68227.msg526826.html#msg526826

What is the current bug behavior?

Error: Identifier not found "specialize"

What is the expected (correct) behavior?

Code compiles and runs without issues

Potentially related to

#40771

Edited by EugeneLoza
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information