Compiler picks up wrong overload for TMemoryStream.Write(TBytes, Longint);
Original Reporter info from Mantis: kluug.net @onpok
-
Reporter name: Ondrej Pokorny
Original Reporter info from Mantis: kluug.net @onpok
- Reporter name: Ondrej Pokorny
Description:
FPC calls the "Write(const Buffer; Count: Longint)" overload even when the "Write(const Buffer: TBytes; Count: Longint)" should be called.
This is because the TMemoryStream.Write declaration misses the "overload" keyword. See docs: https://www.freepascal.org/docs-html/ref/refsu79.html "There is only one case where the overload modifier is mandatory: if a function must be overloaded that resides in another unit."
The docs should be enhanced as well - it is mandatory for object methods across different classes; not only functions across different units.
Steps to reproduce:
program Project1;
{$mode objfpc}
uses
SysUtils, Classes;
var
B1, B2: TBytes;
S: TMemoryStream;
I: Integer;
begin
B1 := [1, 2, 3, 4, 5];
S := TMemoryStream.Create;
S.Write(B1, Length(B1)); // <<< BUG: wrong overload is called
S.Position := 0;
SetLength(B2, Length(B1));
S.ReadData(B2, Length(B2));
S.Free;
if not(Length(B2) = Length(B1)) then
Halt(999);
for I := Low(B1) to High(B1) do
if B1[I] <> B2[I] then
Halt(I);
Writeln('OK');
Readln;
end.
Additional information:
Patch attached.