[flang-commits] [PATCH] D140140: [flang] Correct folding of EXTENDS_TYPE_OF()

Peter Klausler via Phabricator via flang-commits flang-commits at lists.llvm.org
Thu Dec 15 11:33:10 PST 2022


klausler created this revision.
klausler added a reviewer: clementval.
klausler added a project: Flang.
Herald added a subscriber: jdoerfert.
Herald added a project: All.
klausler requested review of this revision.

There was a falsely known case with a polymorphic type.


https://reviews.llvm.org/D140140

Files:
  flang/docs/Extensions.md
  flang/lib/Evaluate/type.cpp


Index: flang/lib/Evaluate/type.cpp
===================================================================
--- flang/lib/Evaluate/type.cpp
+++ flang/lib/Evaluate/type.cpp
@@ -366,9 +366,23 @@
 std::optional<bool> DynamicType::ExtendsTypeOf(const DynamicType &that) const {
   if (IsUnlimitedPolymorphic() || that.IsUnlimitedPolymorphic()) {
     return std::nullopt; // unknown
-  } else if (!AreCompatibleDerivedTypes(evaluate::GetDerivedTypeSpec(that),
-                 evaluate::GetDerivedTypeSpec(*this), true)) {
-    return false;
+  }
+  const auto *thisDts{evaluate::GetDerivedTypeSpec(*this)};
+  const auto *thatDts{evaluate::GetDerivedTypeSpec(that)};
+  if (!thisDts || !thatDts) {
+    return std::nullopt;
+  } else if (!AreCompatibleDerivedTypes(thatDts, thisDts, true)) {
+    // Note that I check *thisDts, not its parent, so that EXTENDS_TYPE_OF()
+    // is .true. when they are the same type.  This is technically
+    // an implementation-defined case in the standard, but every other
+    // compiler works this way.
+    if (IsPolymorphic() && AreCompatibleDerivedTypes(thisDts, thatDts, true)) {
+      // 'that' is *this or an extension of *this, and so runtime *this
+      // could be an extension of 'that'
+      return std::nullopt;
+    } else {
+      return false;
+    }
   } else if (that.IsPolymorphic()) {
     return std::nullopt; // unknown
   } else {
Index: flang/docs/Extensions.md
===================================================================
--- flang/docs/Extensions.md
+++ flang/docs/Extensions.md
@@ -520,3 +520,8 @@
   or `BLOCK DATA` subprogram to also be the name of an local entity in its
   scope, with a portability warning, since that global name is not actually
   capable of being "used" in its scope.
+
+## De Facto Standard Features
+
+* `EXTENDS_TYPE_OF()` returns `.TRUE.` if both of its arguments have the
+  same type, a case that is technically implementation-defined.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140140.483266.patch
Type: text/x-patch
Size: 1935 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/flang-commits/attachments/20221215/075436f2/attachment.bin>


More information about the flang-commits mailing list