Browse Source

* fix #40593: don't check for a static method when assigning a methodpointer to a function reference
+ added test

Sven/Sarah Barth 1 year ago
parent
commit
47c21fd1bc
2 changed files with 48 additions and 2 deletions
  1. 7 2
      compiler/defcmp.pas
  2. 41 0
      tests/webtbs/tw40593.pp

+ 7 - 2
compiler/defcmp.pas

@@ -2600,8 +2600,13 @@ implementation
            include(pa_comp,cpo_warn_incompatible_univ);
          { check return value and options, methodpointer is already checked }
          po_comp:=[po_interrupt,po_iocheck,po_varargs,po_far];
-         { check static only if we compare method pointers }
-         if def1.is_methodpointer and def2.is_methodpointer then
+         { check static only if we compare method pointers (and function
+           references don't count as methodpointers here) }
+         if def1.is_methodpointer and
+             (
+               def2.is_methodpointer and
+               not (po_is_function_ref in def2.procoptions)
+             ) then
            include(po_comp,po_staticmethod);
          if (m_delphi in current_settings.modeswitches) then
            exclude(po_comp,po_varargs);

+ 41 - 0
tests/webtbs/tw40593.pp

@@ -0,0 +1,41 @@
+program tw40593;
+
+{$DEFINE USEOBJFPC}
+{$IFDEF USEOBJFPC}
+{$mode objfpc}
+{$modeswitch functionreferences}
+{$ELSE}
+{$MODE DELPHI}
+{$ENDIF}
+
+Type
+  TSomeClass = class(TObject)
+    class procedure Y (X : Integer); static;
+  end;
+  TSomeProc = reference to procedure (X : integer);
+
+Var
+  P : TSomeProc;
+  V : Integer;
+
+class procedure TSomeClass.Y(X : Integer);
+begin
+  //Writeln(X);
+  V := X;
+end;
+
+
+var
+  C : TSomeClass;
+begin
+  C:=TSomeClass.Create;
+  {$IFDEF USEOBJFPC}
+  P:[email protected];
+  {$ELSE}
+  P:=C.Y;
+  {$ENDIF}
+  P(42);
+  if V <> 42 then
+    Halt(1);
+end.
+