Nested function signature in {$MODE DELPHI} mismatch

Summary

Nested function signature mismatch allowed in {$MODE DELPHI}, cause GPF errors in runtime.

System Information

  • Operating system: Windows
  • Processor architecture: x86-64
  • Compiler version: 3.2.2

Steps to reproduce

See example:

Example Project

program test_nested_func;

{$mode DELPHI}

uses
  SysUtils, Classes;

var
  MyList: TList;
  i: Integer;
  Item: TComponent;

function CompareNormal(Item1, Item2: Pointer): Integer;
begin
  Result := CompareText(TComponent(Item1).Name, TComponent(Item2).Name);
end;

procedure SortMyList();

  function CompareNested(Item1, Item2: Pointer): Integer;
  begin
    Result := CompareText(TComponent(Item1).Name, TComponent(Item2).Name);  // crashes here because Item2 pointer is wrong
  end;

begin
  MyList.Sort(@CompareNormal); // ok
  MyList.Sort(@CompareNested); // crash
end;

begin
  MyList := TList.Create;
  // populate list
  for i := 1 to 65 do
  begin
    Item := TComponent.Create(nil);
    Item.Name := 'Item' + IntToStr(i);
    MyList.Add(Item);
  end;

  SortMyList();
end.

What is the current bug behavior?

Signatures of nested and normal procedures mismath, but compiler allows it.

What is the expected (correct) behavior?

Nested and normal functions signatures compatible. At least, produce compiler error, as in {$mode OBJFPC}.

Edited by Sergey Bodrov