[PATCH] D121160: [flang] Fix module file missing USE for shadowed derived type

Peter Klausler via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Mar 7 17:13:14 PST 2022


This revision was automatically updated to reflect the committed changes.
Closed by commit rG665d41593f9d: [flang] Fix module file missing USE for shadowed derived type (authored by klausler).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D121160/new/

https://reviews.llvm.org/D121160

Files:
  flang/lib/Semantics/mod-file.cpp
  flang/test/Semantics/modfile44.f90


Index: flang/test/Semantics/modfile44.f90
===================================================================
--- /dev/null
+++ flang/test/Semantics/modfile44.f90
@@ -0,0 +1,54 @@
+! RUN: %python %S/test_modfile.py %s %flang_fc1
+! Ensure that m2.mod explicitly USEs a generic interface from m1
+! so it can utilize its shadowed derived type.
+module m1
+  implicit none
+  type :: xyz
+    integer :: n
+  end type
+  interface xyz
+    module procedure xzy
+  end interface
+ contains
+  function xzy(j) result(res)
+    integer, intent(in) :: j
+    type(xyz) :: res
+    res%n = j
+  end function
+end module
+
+!Expect: m1.mod
+!module m1
+!interface xyz
+!procedure::xzy
+!end interface
+!type::xyz
+!integer(4)::n
+!end type
+!contains
+!function xzy(j) result(res)
+!integer(4),intent(in)::j
+!type(xyz)::res
+!end
+!end
+
+module m2
+  implicit none
+ contains
+  function foo(j) result(res)
+    use :: m1, only: xyz
+    integer, intent(in) :: j
+    type(xyz) :: res
+    res = xyz(j)
+  end function
+end module
+
+!Expect: m2.mod
+!module m2
+!contains
+!function foo(j) result(res)
+!use m1,only:xyz
+!integer(4),intent(in)::j
+!type(xyz)::res
+!end
+!end
Index: flang/lib/Semantics/mod-file.cpp
===================================================================
--- flang/lib/Semantics/mod-file.cpp
+++ flang/lib/Semantics/mod-file.cpp
@@ -1043,7 +1043,17 @@
   for (const auto &pair : scope_) {
     const Symbol &symbol{*pair.second};
     if (const auto *useDetails{symbol.detailsIf<UseDetails>()}) {
-      if (useSet_.count(useDetails->symbol().GetUltimate()) > 0) {
+      const Symbol &ultimate{useDetails->symbol().GetUltimate()};
+      bool needed{useSet_.count(ultimate) > 0};
+      if (const auto *generic{ultimate.detailsIf<GenericDetails>()}) {
+        // The generic may not be needed itself, but the specific procedure
+        // &/or derived type that it shadows may be needed.
+        const Symbol *spec{generic->specific()};
+        const Symbol *dt{generic->derivedType()};
+        needed = needed || (spec && useSet_.count(*spec) > 0) ||
+            (dt && useSet_.count(*dt) > 0);
+      }
+      if (needed) {
         need_.push_back(symbol);
       }
     }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D121160.413664.patch
Type: text/x-patch
Size: 2207 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220308/b3009555/attachment.bin>


More information about the llvm-commits mailing list