[flang-commits] [flang] [flang] Fix module file generation when generic shadows derived type (PR #78618)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Thu Jan 18 12:28:05 PST 2024


https://github.com/klausler created https://github.com/llvm/llvm-project/pull/78618

Fortran allows the name of a generic interface to be the same as the name of a derived type or specific procedure.  When this happens, it causes the code in module file generation to miss the symbol of a derived type when scanning for symbols in initialization expressions that need to be imported.  Fix.

>From 71beced4920ff7e2c7f3478f9ac352e28915978a Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Thu, 18 Jan 2024 12:22:05 -0800
Subject: [PATCH] [flang] Fix module file generation when generic shadows
 derived type

Fortran allows the name of a generic interface to be the same as the
name of a derived type or specific procedure.  When this happens, it
causes the code in module file generation to miss the symbol of a
derived type when scanning for symbols in initialization expressions
that need to be imported.  Fix.
---
 flang/lib/Semantics/mod-file.cpp   |  7 +++++++
 flang/test/Semantics/modfile62.f90 | 31 ++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)
 create mode 100644 flang/test/Semantics/modfile62.f90

diff --git a/flang/lib/Semantics/mod-file.cpp b/flang/lib/Semantics/mod-file.cpp
index ffdc3155f9a2214..d7d8b6ea0d44b22 100644
--- a/flang/lib/Semantics/mod-file.cpp
+++ b/flang/lib/Semantics/mod-file.cpp
@@ -183,6 +183,13 @@ static void HarvestInitializerSymbols(
       if (symbol->scope()) {
         HarvestInitializerSymbols(set, *symbol->scope());
       }
+    } else if (const auto &generic{symbol->detailsIf<GenericDetails>()};
+               generic && generic->derivedType()) {
+      const Symbol &dtSym{*generic->derivedType()};
+      CHECK(dtSym.has<DerivedTypeDetails>());
+      if (dtSym.scope()) {
+        HarvestInitializerSymbols(set, *dtSym.scope());
+      }
     } else if (IsNamedConstant(*symbol) || scope.IsDerivedType()) {
       if (const auto *object{symbol->detailsIf<ObjectEntityDetails>()}) {
         if (object->init()) {
diff --git a/flang/test/Semantics/modfile62.f90 b/flang/test/Semantics/modfile62.f90
new file mode 100644
index 000000000000000..8a145bd46060471
--- /dev/null
+++ b/flang/test/Semantics/modfile62.f90
@@ -0,0 +1,31 @@
+! RUN: %python %S/test_modfile.py %s %flang_fc1
+module m
+  use iso_c_binding, only: c_ptr, c_null_ptr
+  type foo
+    type(c_ptr) :: p = c_null_ptr
+  end type
+  interface foo ! same name as derived type
+    procedure f
+  end interface
+ contains
+  type(foo) function f()
+  end
+end
+
+!Expect: m.mod
+!module m
+!use,intrinsic::__fortran_builtins,only:__builtin_c_ptr
+!use,intrinsic::iso_c_binding,only:c_ptr
+!use,intrinsic::iso_c_binding,only:c_null_ptr
+!private::__builtin_c_ptr
+!type::foo
+!type(c_ptr)::p=__builtin_c_ptr(__address=0_8)
+!end type
+!interface foo
+!procedure::f
+!end interface
+!contains
+!function f()
+!type(foo)::f
+!end
+!end



More information about the flang-commits mailing list