瀏覽代碼

* support passing [nil] to an array of class/objcclass/javaclass/intf/...
in {$mode objfpc} (mantis #19452)

git-svn-id: trunk@28862 -

Jonas Maebe 10 年之前
父節點
當前提交
f1c45eeae3
共有 3 個文件被更改,包括 44 次插入1 次删除
  1. 1 0
      .gitattributes
  2. 26 1
      compiler/htypechk.pas
  3. 17 0
      tests/webtbs/tw19452.pp

+ 1 - 0
.gitattributes

@@ -13708,6 +13708,7 @@ tests/webtbs/tw19368.pp svneol=native#text/pascal
 tests/webtbs/tw1938.pp svneol=native#text/plain
 tests/webtbs/tw19434a.pp svneol=native#text/plain
 tests/webtbs/tw19434b.pp svneol=native#text/plain
+tests/webtbs/tw19452.pp svneol=native#text/plain
 tests/webtbs/tw1948.pp svneol=native#text/plain
 tests/webtbs/tw19498.pp svneol=native#text/pascal
 tests/webtbs/tw19499.pp svneol=native#text/pascal

+ 26 - 1
compiler/htypechk.pas

@@ -2583,7 +2583,8 @@ implementation
         def_to   : tdef;
         currpt,
         pt       : tcallparanode;
-        eq       : tequaltype;
+        eq,
+        mineq    : tequaltype;
         convtype : tconverttype;
         pdtemp,
         pdoper   : tprocdef;
@@ -2763,6 +2764,30 @@ implementation
                    eq:=compare_defs_ext(n.resultdef,def_to,n.nodetype,convtype,pdoper,cdoptions);
                    n.free;
                  end
+              else if (def_to.typ=arraydef) and
+                      is_class_or_interface_or_dispinterface_or_objc_or_java(tarraydef(def_to).elementdef) and
+                      is_array_constructor(currpt.left.resultdef) and
+                      assigned(tarrayconstructornode(currpt.left).left) then
+                begin
+                  { ensure that [nil] can be converted to "array of tobject",
+                    because if we just try to convert "array of pointer" to
+                    "array of tobject", we get type conversion errors in
+                    non-Delphi modes }
+                  n:=currpt.left;
+                  mineq:=te_exact;
+                  repeat
+                    if tarrayconstructornode(n).left.nodetype=arrayconstructorrangen then
+                      eq:=te_incompatible
+                    else
+                      eq:=compare_defs_ext(tarrayconstructornode(n).left.resultdef,tarraydef(def_to).elementdef,tarrayconstructornode(n).left.nodetype,convtype,pdoper,cdoptions);
+                    if eq=te_incompatible then
+                      break;
+                    if eq<mineq then
+                      mineq:=eq;
+                    n:=tarrayconstructornode(n).right;
+                  until not assigned(n);
+                  eq:=mineq;
+                end
               else
               { generic type comparision }
                begin

+ 17 - 0
tests/webtbs/tw19452.pp

@@ -0,0 +1,17 @@
+{ %norun }
+
+{$mode objfpc}
+type
+  TMyObject = class
+  public
+    constructor Create(ar: array of TMyObject);
+  end;
+
+constructor TMyObject.Create(ar: array of TMyObject);
+begin
+end;
+
+begin
+  TMyObject.Create([nil]);
+end.
+