From 7a34677b2ab134fab30a19c6bb6249425e802daf Mon Sep 17 00:00:00 2001 From: Sven/Sarah Barth Date: Fri, 2 Dec 2022 17:06:18 +0100 Subject: [PATCH] * as long as the type passed into a TypeInfo() is not an undefined or error def the resulting value will always be constant at compile time, so it can be compared to another then no matter if typenode or not + added tests --- compiler/nadd.pas | 7 ++++--- tests/tbs/tb0699.pp | 16 ++++++++++++++++ tests/tbs/tb0700.pp | 14 ++++++++++++++ tests/tbs/tb0701.pp | 24 ++++++++++++++++++++++++ tests/tbs/tb0702.pp | 22 ++++++++++++++++++++++ 5 files changed, 80 insertions(+), 3 deletions(-) create mode 100644 tests/tbs/tb0699.pp create mode 100644 tests/tbs/tb0700.pp create mode 100644 tests/tbs/tb0701.pp create mode 100644 tests/tbs/tb0702.pp diff --git a/compiler/nadd.pas b/compiler/nadd.pas index 4b8eb188a2..aacfc28706 100644 --- a/compiler/nadd.pas +++ b/compiler/nadd.pas @@ -1429,13 +1429,14 @@ implementation righttarget:=actualtargetnode(@right)^; if (nodetype in [equaln,unequaln]) and (lefttarget.nodetype=inlinen) and (righttarget.nodetype=inlinen) and (tinlinenode(lefttarget).inlinenumber=in_typeinfo_x) and (tinlinenode(righttarget).inlinenumber=in_typeinfo_x) and - (tinlinenode(lefttarget).left.nodetype=typen) and (tinlinenode(righttarget).left.nodetype=typen) then + not (tinlinenode(lefttarget).left.resultdef.typ in [undefineddef,errordef]) and + not (tinlinenode(righttarget).left.resultdef.typ in [undefineddef,errordef]) then begin case nodetype of equaln: - result:=cordconstnode.create(ord(ttypenode(tinlinenode(lefttarget).left).resultdef=ttypenode(tinlinenode(righttarget).left).resultdef),bool8type,false); + result:=cordconstnode.create(ord(tinlinenode(lefttarget).left.resultdef=tinlinenode(righttarget).left.resultdef),bool8type,false); unequaln: - result:=cordconstnode.create(ord(ttypenode(tinlinenode(lefttarget).left).resultdef<>ttypenode(tinlinenode(righttarget).left).resultdef),bool8type,false); + result:=cordconstnode.create(ord(tinlinenode(lefttarget).left.resultdef<>tinlinenode(righttarget).left.resultdef),bool8type,false); else Internalerror(2020092901); end; diff --git a/tests/tbs/tb0699.pp b/tests/tbs/tb0699.pp new file mode 100644 index 0000000000..53714fe627 --- /dev/null +++ b/tests/tbs/tb0699.pp @@ -0,0 +1,16 @@ +{ %FAIL } +{ %OPT=-Sew } + +{ Note: we are speculating for "Unreachable code" warnings here } + +program tb0699; + +procedure Test(aArg: LongInt); +begin + if TypeInfo(aArg) <> TypeInfo(LongInt) then + Writeln('False'); +end; + +begin + +end. diff --git a/tests/tbs/tb0700.pp b/tests/tbs/tb0700.pp new file mode 100644 index 0000000000..e6c0ac9a94 --- /dev/null +++ b/tests/tbs/tb0700.pp @@ -0,0 +1,14 @@ +{ %FAIL } +{ %OPT=-Sew } + +{ Note: we are speculating for "Unreachable code" warnings here } + +program tb0700; + +var + arr: array of LongInt; +begin + arr := Nil; + if TypeInfo(arr[0]) <> TypeInfo(LongInt) then + Writeln('False'); +end. diff --git a/tests/tbs/tb0701.pp b/tests/tbs/tb0701.pp new file mode 100644 index 0000000000..4f3c346407 --- /dev/null +++ b/tests/tbs/tb0701.pp @@ -0,0 +1,24 @@ +{ %FAIL } +{ %OPT=-Sew } + +{ Note: we are speculating for "Unreachable code" warnings here } + +program tb0701; + +{$mode objfpc} + +type + TTest = class + f: LongInt; + procedure Test; + end; + +procedure TTest.Test; +begin + if TypeInfo(f) <> TypeInfo(LongInt) then + Writeln('False'); +end; + +begin + +end. diff --git a/tests/tbs/tb0702.pp b/tests/tbs/tb0702.pp new file mode 100644 index 0000000000..c8392e9955 --- /dev/null +++ b/tests/tbs/tb0702.pp @@ -0,0 +1,22 @@ +{ %OPT=-Sew } + +{ don't optimize TypeInfo comparisons if undefined types are involved } + +program tb0702; + +{$mode objfpc} + +type + generic TTest = class + procedure Test; + end; + +procedure TTest.Test; +begin + if TypeInfo(S) = TypeInfo(LongInt) then + Writeln('Test'); +end; + +begin + +end.