Missing type check in constant parameter: string vs. char
Original Reporter info from Mantis: jdehaan
-
Reporter name: Jdehaan
Original Reporter info from Mantis: jdehaan
- Reporter name: Jdehaan
Description:
If you define a generic function with constant string parameter, then an error is generated if a char value is passed to the constant parameter when the specialised function is called.
The length of the string must be > 1. If I remove the space in front of the ' !', it gives a type mismatch (expected 'AnsiString').
program gentest;
generic function ConstString<const P,Q,S: string>: PChar;
var
size: Integer;
begin
Size := SizeOf(P) + SizeOf(Q) + SizeOf(S);
writeln(Size);
Result := P+Q+S;
end;
var
s: PChar;
begin
s := specialize ConstString<'Hello', ' world', ' !'>; // this works; string ' !' has size 2
got "Char" expected "AnsiString"
writeln(s);
end.
Steps to reproduce:
use a char to pass to the constant parameter:
program gentest;
generic function ConstString<const P,Q,S: string>: PChar;
var
size: Integer;
begin
Size := SizeOf(P) + SizeOf(Q) + SizeOf(S);
writeln(Size);
Result := P+Q+S;
end;
var
s: PChar;
begin
s := specialize ConstString<'Hello', ' world', '!'>; // error gentest.lpr(16,50) Error: Incompatible types: got "Char" expected "AnsiString"
writeln(s);
end.
Additional information:
Compile Project, Target: gentest: Exit code 1, Errors: 3
gentest.lpr(16,50) Error: Incompatible types: got "Char" expected "AnsiString"
gentest.lpr(16,8) Error: Wrong number of parameters specified for call to "ConstString$3"
gentest.lpr(3,18) Error: Found declaration: ConstString$3:^Char;
Mantis conversion info:
- Mantis ID: 39030
- OS: Big Sur
- OS Build: 11.4
- Build: FPC 3.3.1 x86_64-darwin-cocoa
- Platform: MacOs
- Version: 3.3.1
- Fixed in version: 3.3.1
- Fixed in revision: 49569 (#51326e94)