From eea088c5f59610ce7cd966d1f9c53e032de39f3d Mon Sep 17 00:00:00 2001 From: florian Date: Sun, 14 Oct 2018 07:38:13 +0000 Subject: [PATCH] * allow nil to be assigned to generic types, resolves #34037 * niln is also a constant node git-svn-id: trunk@39934 - --- .gitattributes | 1 + compiler/defcmp.pas | 3 ++- compiler/node.pas | 8 +++++--- compiler/pexpr.pas | 2 +- tests/webtbs/tw34037.pp | 21 +++++++++++++++++++++ 5 files changed, 30 insertions(+), 5 deletions(-) create mode 100644 tests/webtbs/tw34037.pp diff --git a/.gitattributes b/.gitattributes index 9925c4b539..eb81fe1a94 100644 --- a/.gitattributes +++ b/.gitattributes @@ -16378,6 +16378,7 @@ tests/webtbs/tw33840.pp -text svneol=native#text/pascal tests/webtbs/tw33898.pp -text svneol=native#text/pascal tests/webtbs/tw3402.pp svneol=native#text/plain tests/webtbs/tw34021.pp -text svneol=native#text/pascal +tests/webtbs/tw34037.pp svneol=native#text/pascal tests/webtbs/tw3411.pp svneol=native#text/plain tests/webtbs/tw34124.pp svneol=native#text/pascal tests/webtbs/tw3418.pp svneol=native#text/plain diff --git a/compiler/defcmp.pas b/compiler/defcmp.pas index 3f5882f762..af4b40d931 100644 --- a/compiler/defcmp.pas +++ b/compiler/defcmp.pas @@ -289,7 +289,8 @@ implementation if assigned(tstoreddef(def_from).genconstraintdata) or assigned(tstoreddef(def_to).genconstraintdata) then begin - if def_from.typ<>def_to.typ then + { constants could get another deftype (e.g. niln) } + if (def_from.typ<>def_to.typ) and not(fromtreetype in nodetype_const) then begin { not compatible anyway } doconv:=tc_not_possible; diff --git a/compiler/node.pas b/compiler/node.pas index b8600000bf..21b421107c 100644 --- a/compiler/node.pas +++ b/compiler/node.pas @@ -197,11 +197,13 @@ interface 'specializen'); { a set containing all const nodes } - nodetype_const = [ordconstn, + nodetype_const = [niln, + ordconstn, pointerconstn, stringconstn, guidconstn, - realconstn]; + realconstn, + setconstn]; type { all boolean field of ttree are now collected in flags } @@ -657,7 +659,7 @@ implementation function is_constnode(p : tnode) : boolean; begin - is_constnode:=(p.nodetype in [niln,ordconstn,realconstn,stringconstn,setconstn,pointerconstn,guidconstn]); + is_constnode:=(p.nodetype in nodetype_const); end; diff --git a/compiler/pexpr.pas b/compiler/pexpr.pas index 56374d89ef..e6f99ba68e 100644 --- a/compiler/pexpr.pas +++ b/compiler/pexpr.pas @@ -1939,7 +1939,7 @@ implementation not is_objectpascal_helper(tdef(srsymtable.defowner)) then internalerror(2013011401); { convert const node to temp node of the extended type } - if node.nodetype in (nodetype_const+[niln,addrn]) then + if node.nodetype in (nodetype_const+[addrn]) then begin extdef:=tobjectdef(srsymtable.defowner).extendeddef; newstatement:=nil; diff --git a/tests/webtbs/tw34037.pp b/tests/webtbs/tw34037.pp new file mode 100644 index 0000000000..2d7ffbfe64 --- /dev/null +++ b/tests/webtbs/tw34037.pp @@ -0,0 +1,21 @@ +program project1; +{$mode objfpc}{$H+} + +uses + Classes, FGL; + +type + TBaseClass = class (TObject) + end; + + generic TFPGObjectListEx = class (specialize TFPGObjectList) + function GetItemByID(AID: Integer): T; + end; + +function TFPGObjectListEx.GetItemByID(AID: Integer): T; +begin + Result:=nil; //T(nil); +end; + +begin +end.