[flang-commits] [flang] 4228e28 - [flang] Fix crash in semantics (#106158)

via flang-commits flang-commits at lists.llvm.org
Wed Sep 4 10:51:56 PDT 2024


Author: Peter Klausler
Date: 2024-09-04T10:51:52-07:00
New Revision: 4228e28293458e6ec49bd5487210719ff33c319a

URL: https://github.com/llvm/llvm-project/commit/4228e28293458e6ec49bd5487210719ff33c319a
DIFF: https://github.com/llvm/llvm-project/commit/4228e28293458e6ec49bd5487210719ff33c319a.diff

LOG: [flang] Fix crash in semantics (#106158)

Semantics crashes when merging a USE-associated derived type with a
local generic procedure interface of the same name. (The other direction
works.)

Added: 
    flang/test/Semantics/generic09.f90

Modified: 
    flang/lib/Semantics/resolve-names.cpp

Removed: 
    


################################################################################
diff  --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index ec8f854f64d103..2e86e0afc9bd0e 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -3131,7 +3131,7 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
     combinedDerivedType = useDerivedType;
   } else {
     const Scope *localScope{localDerivedType->scope()};
-    const Scope *useScope{useDerivedType->scope()};
+    const Scope *useScope{useDerivedType->GetUltimate().scope()};
     if (localScope && useScope && localScope->derivedTypeSpec() &&
         useScope->derivedTypeSpec() &&
         evaluate::AreSameDerivedType(
@@ -3307,7 +3307,12 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
     AddGenericUse(newUseGeneric, localName, useUltimate);
     newUseGeneric.AddUse(*localSymbol);
     if (combinedDerivedType) {
-      newUseGeneric.set_derivedType(*const_cast<Symbol *>(combinedDerivedType));
+      if (const auto *oldDT{newUseGeneric.derivedType()}) {
+        CHECK(&oldDT->GetUltimate() == &combinedDerivedType->GetUltimate());
+      } else {
+        newUseGeneric.set_derivedType(
+            *const_cast<Symbol *>(combinedDerivedType));
+      }
     }
     if (combinedProcedure) {
       newUseGeneric.set_specific(*const_cast<Symbol *>(combinedProcedure));

diff  --git a/flang/test/Semantics/generic09.f90 b/flang/test/Semantics/generic09.f90
new file mode 100644
index 00000000000000..6159dd4b701d70
--- /dev/null
+++ b/flang/test/Semantics/generic09.f90
@@ -0,0 +1,47 @@
+! RUN: %flang_fc1 -fdebug-unparse %s 2>&1 | FileCheck %s
+module m1
+  type foo
+    integer n
+    integer :: m = 1
+  end type
+end
+
+module m2
+  use m1
+  interface foo
+    module procedure f1
+  end interface
+ contains
+  type(foo) function f1(a)
+    real, intent(in) :: a
+    f1%n = a
+    f1%m = 2
+  end
+end
+
+module m3
+  use m2
+  interface foo
+    module procedure f2
+  end interface
+ contains
+  type(foo) function f2(a)
+    double precision, intent(in) :: a
+    f2%n = a
+    f2%m = 3
+  end
+end
+
+program main
+  use m3
+  type(foo) x
+!CHECK: foo(n=1_4,m=1_4)
+  x = foo(1)
+  print *, x
+!CHECK: f1(2._4)
+  x = foo(2.)
+  print *, x
+!CHECK: f2(3._8)
+  x = foo(3.d0)
+  print *, x
+end


        


More information about the flang-commits mailing list