fpc/tests/tbs/tb0614.pp
nickysn e6d01eb3b5 * fixed a bug, which caused a function that returns a method pointer (or nested
procdef) to be called twice, when the result of this function is immediately
  called (i.e. not stored in a temp variable).

git-svn-id: trunk@32495 -
2015-11-22 17:21:08 +00:00

70 lines
1.2 KiB
ObjectPascal

program tb0614;
{$mode objfpc}
{$modeswitch nestedprocvars}
type
tobjectmethod = procedure of object;
tnestedprocvar = procedure is nested;
TMyClass = class
procedure Moo;
end;
var
obj: TMyClass;
NumCalls: Integer;
procedure TMyClass.Moo;
begin
Writeln('TMyClass.Moo');
end;
function get_objmethod: tobjectmethod;
begin
Writeln('get_objmethod');
Inc(NumCalls);
Result := @obj.Moo;
end;
function get_nestedprocvar: tnestedprocvar;
procedure nested;
begin
Writeln('nested');
end;
begin
Writeln('get_nestedprocvar');
Inc(NumCalls);
Result := @nested;
end;
var
Errors: Boolean = False;
begin
NumCalls := 0;
obj := TMyClass.Create;
get_objmethod()();
obj.Free;
if NumCalls <> 1 then
begin
Writeln('Error: get_objmethod should have been called once, but instead it was called ', NumCalls, ' times');
Errors := True;
end;
NumCalls := 0;
get_nestedprocvar()();
if NumCalls <> 1 then
begin
Writeln('Error: get_nestedprocvar should have been called once, but instead it was called ', NumCalls, ' times');
Errors := True;
end;
if Errors then
begin
Writeln('Errors found!');
Halt(1);
end
else
Writeln('Ok!');
end.