Compiler allows overloaded functions with same parameters but different result types, gets confused
Original Reporter info from Mantis: ahydra
-
Reporter name: Colin Haywood
Original Reporter info from Mantis: ahydra
- Reporter name: Colin Haywood
Description:
See the program in the "Steps to Reproduce" section. This compiles fine until it reaches the final line (b := ), where the error is
Error: Incompatible types: got "LongInt" expected "AnsiString"
The compiler only ever uses the first instance of a function that's been overloaded on result type only, making for a confusing error. It should either reject attempts to overload functions like this (requiring different parameter lists, as per the docs), or accept but use the correct instance based on the type of variable that the function result is being assigned to (so lines where the function is called but the result is discarded wouldn't be allowed).
Steps to reproduce:
program overload_test;
{$mode objfpc}{$H+}
uses
sysutils;
var
a: integer;
b: string;
function thingy(r: integer; s: string): integer; overload;
begin
Result := r;
end;
function thingy(r: integer; s: string): string; overload;
begin
Result := s;
end;
begin
thingy(3, 'abc');
thingy(4, 'def');
a := thingy(3, 'abc');
b := thingy(4, 'def');
end.
Mantis conversion info:
- Mantis ID: 24805
- OS: Windows
- OS Build: 32bit
- Version: 2.6.2