Skip to content

ObjFPC + advancedrecords + generics + const + sizeof = Invalid Expression, sometimes

Summary

In the ObjFPC mode with AdvancedRecords modeswitch turned on constants inside record templates containing SizeOf intrinsic are evaluated sometimes.

Please see example below.

System Information

  • Operating system: Linux for x86-64
  • Processor architecture: x86-64
  • Compiler version: version 3.2.2 [2021/07/09] for x86_64
  • Device: Asus UX31A (laptop)

Steps to reproduce

{ Free Pascal Compiler version 3.2.2 [2021/07/09] for x86_64 }
{ Target OS: Linux for x86-64 }

program IllegalExpression_Const_SizeOf;

{$mode objfpc}
{$H+}
{$modeswitch advancedrecords}

type
  generic T1 <T2> = record
    const
      S1 = SizeOf(T2);         // OK
      S2 = S1 * 8;             // OK
      // S3 = SizeOf(T2) * 8;  // Compilation Error: Illegal expression // ???
  end;

  T3 = specialize T1 <UInt32>;

begin
  WriteLn('S1 = ', T3.S1);
  WriteLn('S2 = ', T3.S2);
  // WriteLn('S3 = ', T3.S3);
end.

What is the current bug behavior?

Compiler error (Illegal expression) when lines relevant to constant S3 are uncommented.

What is the expected (correct) behavior?

S3 = S2

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