Commit `1743df7b` breaks compilation of generics with consts
Summary
Trying to compile program with types, that use const generics starting from compiler revision 1743df7b80edf021173e1ed7e4fc24586cd58f95
yields linking errors (in case of programs) or crashes in runtime (in case of dynamic libraries; however it has the same mechanism - linker can't find a symbol)
System Information
- Operating system: Linux (Ubuntu 24.04)
- Processor architecture: x86_64
-
Compiler version:
Free Pascal Compiler version 3.3.1-15955-g1743df7b80-dirty [2024/07/12] for x86_64
(1743df7b80edf021173e1ed7e4fc24586cd58f95
) and above (commits that were earlier do not exhibit the issue) - Device: Computer
Steps to reproduce
Try to compile a program with type, that uses const as generic (please see the example project).
Example Project
program test_smallestprog;
{$Mode DelphiUnicode}
uses
SysUtils;
type
TTest<const A: UInt64> = record
public
function ToString(B: UInt64): UnicodeString;
end;
// There should be at least one method for linking to fail
function TTest<A>.ToString(B: UInt64): UnicodeString;
begin
Result := (A + B).ToString;
end;
type
TMyTest = TTest<1234>;
var A: TMyTest;
begin
WriteLn(A.ToString(23456));
end.
What is the current bug behavior?
On the affected versions of compiler output is like so (if compiling a program):
Free Pascal Compiler version 3.3.1-15955-g1743df7b80-dirty [2024/07/12] for x86_64
Copyright (c) 1993-2024 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test_smallestprog.pas
test_smallestprog.pas(16,15) Warning: Implicit string type conversion from "AnsiString" to "UnicodeString"
test_smallestprog.pas(20,25) Note: Private const "<record type>.A" never used
Linking test_smallestprog
/usr/bin/ld.bfd: test_smallestprog.o: in function `main':
test_smallestprog.pas:(.text.n_main+0x59): undefined reference to `P$TEST_SMALLESTPROG$_$TTEST$1$CRC0903643E_CRC206D85AC_$__$$_TOSTRING$QWORD$$UNICODESTRING'
test_smallestprog.pas(25,1) Error: Error while linking
test_smallestprog.pas(25,1) Fatal: There were 1 errors compiling module, stopping
Fatal: Compilation aborted
Also, affected versions of compiler report Private const ... never used
, but apparently it is used
What is the expected (correct) behavior?
On a compiler revision cdda954ea746dfcb6d9625d51095422b1bf0fc93
(previous commit):
Free Pascal Compiler version 3.3.1-15954-gcdda954ea7-dirty [2024/07/12] for x86_64
Copyright (c) 1993-2024 by Florian Klaempfl and others
Target OS: Linux for x86-64
Compiling test_smallestprog.pas
test_smallestprog.pas(16,15) Warning: Implicit string type conversion from "AnsiString" to "UnicodeString"
test_smallestprog.pas(16,15) Warning: Implicit string type conversion from "AnsiString" to "UnicodeString"
Linking test_smallestprog
30 lines compiled, 0.1 sec, 446944 bytes code, 191128 bytes data
2 warning(s) issued
Versions of compiler that are not affected also do not report that constant is not used