[flang-commits] [flang] [flang] Fix crash when handling benign USE conflict (PR #121977)

Peter Klausler via flang-commits flang-commits at lists.llvm.org
Tue Jan 7 10:38:08 PST 2025


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

When the same name is used for distinct derived types in two modules, and at least one of those modules also defines a generic interface of the same name, name resolution crashes when both modules are USE'd into the same scope.  The crash is due to some pointers into the symbol table becoming invalid when a symbol is replaced with a UseErrorDetails; set them to null. Also allow for extending a UseErrorDetails in place rather than emitting a spurious error message.

Fixes https://github.com/llvm/llvm-project/issues/121718.

>From a4aa5e25b5bbaffd7da96db2904748de605fa458 Mon Sep 17 00:00:00 2001
From: Peter Klausler <pklausler at nvidia.com>
Date: Tue, 7 Jan 2025 10:33:01 -0800
Subject: [PATCH] [flang] Fix crash when handling benign USE conflict

When the same name is used for distinct derived types in two
modules, and at least one of those modules also defines a generic
interface of the same name, name resolution crashes when both
modules are USE'd into the same scope.  The crash is due to
some pointers into the symbol table becoming invalid when
a symbol is replaced with a UseErrorDetails; set them to null.
Also allow for extending a UseErrorDetails in place rather
than emitting a spurious error message.

Fixes https://github.com/llvm/llvm-project/issues/121718.
---
 flang/lib/Semantics/resolve-names.cpp |  6 ++++++
 flang/test/Semantics/bug121718.f90    | 31 +++++++++++++++++++++++++++
 2 files changed, 37 insertions(+)
 create mode 100644 flang/test/Semantics/bug121718.f90

diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index 122c0a2ebb646a..724f1b28078356 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -3162,6 +3162,10 @@ ModuleVisitor::SymbolRename ModuleVisitor::AddUse(
 // Convert it to a UseError with this additional location.
 static bool ConvertToUseError(
     Symbol &symbol, const SourceName &location, const Scope &module) {
+  if (auto *ued{symbol.detailsIf<UseErrorDetails>()}) {
+    ued->add_occurrence(location, module);
+    return true;
+  }
   const auto *useDetails{symbol.detailsIf<UseDetails>()};
   if (!useDetails) {
     if (auto *genericDetails{symbol.detailsIf<GenericDetails>()}) {
@@ -3319,6 +3323,8 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
         combinedDerivedType = CreateLocalUseError();
       } else {
         ConvertToUseError(*localSymbol, location, *useModuleScope_);
+        localDerivedType = nullptr;
+        localGeneric = nullptr;
         combinedDerivedType = localSymbol;
       }
     }
diff --git a/flang/test/Semantics/bug121718.f90 b/flang/test/Semantics/bug121718.f90
new file mode 100644
index 00000000000000..e99391f227d72e
--- /dev/null
+++ b/flang/test/Semantics/bug121718.f90
@@ -0,0 +1,31 @@
+! RUN: %flang_fc1 2>&1 | FileCheck %s --allow-empty
+! CHECK-NOT: error
+! Regression test simplified from LLVM bug 121718.
+! Ensure no crash and no spurious error message.
+module m1
+  type foo
+    integer x
+  end type
+ contains
+  subroutine test
+    print *, foo(123)
+  end
+end
+module m2
+  interface foo
+    procedure f
+  end interface
+  type foo
+    real x
+  end type
+ contains
+  complex function f(x)
+    complex, intent(in) :: x
+    f = x
+  end
+end
+program main
+  use m1
+  use m2
+  call test
+end



More information about the flang-commits mailing list