[flang-commits] [flang] f5f6ca6 - [flang] Fix crash in UseErrorDetails construction case (#168126)
via flang-commits
flang-commits at lists.llvm.org
Wed Nov 19 08:54:25 PST 2025
Author: Peter Klausler
Date: 2025-11-19T08:54:21-08:00
New Revision: f5f6ca659992ae6d26b2a96304ceb65a1fd63ad6
URL: https://github.com/llvm/llvm-project/commit/f5f6ca659992ae6d26b2a96304ceb65a1fd63ad6
DIFF: https://github.com/llvm/llvm-project/commit/f5f6ca659992ae6d26b2a96304ceb65a1fd63ad6.diff
LOG: [flang] Fix crash in UseErrorDetails construction case (#168126)
When a derived type has the same name as a generic function, and is
use-associated into a scope along with other distinct derived types of
the same name, we crash. Don't crash.
Fixes https://github.com/llvm/llvm-project/issues/168099.
Added:
flang/test/Semantics/bug168099.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 82e872870c6b1..2a487a6d39d51 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -3967,22 +3967,6 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
useProcedure = &useUltimate;
}
- // Creates a UseErrorDetails symbol in the current scope for a
- // current UseDetails symbol, but leaves the UseDetails in the
- // scope's name map.
- auto CreateLocalUseError{[&]() {
- EraseSymbol(*localSymbol);
- CHECK(localSymbol->has<UseDetails>());
- UseErrorDetails details{localSymbol->get<UseDetails>()};
- details.add_occurrence(location, useSymbol);
- Symbol *newSymbol{&MakeSymbol(localName, Attrs{}, std::move(details))};
- // Restore *localSymbol in currScope
- auto iter{currScope().find(localName)};
- CHECK(iter != currScope().end() && &*iter->second == newSymbol);
- iter->second = MutableSymbolRef{*localSymbol};
- return newSymbol;
- }};
-
// When two derived types arrived, try to combine them.
const Symbol *combinedDerivedType{nullptr};
if (!useDerivedType) {
@@ -4008,8 +3992,19 @@ void ModuleVisitor::DoAddUse(SourceName location, SourceName localName,
combinedDerivedType = localDerivedType;
} else {
// Create a local UseErrorDetails for the ambiguous derived type
- if (localGeneric) {
- combinedDerivedType = CreateLocalUseError();
+ if (localSymbol->has<UseDetails>() && localGeneric) {
+ // Creates a UseErrorDetails symbol in the current scope for a
+ // current UseDetails symbol, but leaves the UseDetails in the
+ // scope's name map.
+ UseErrorDetails details{localSymbol->get<UseDetails>()};
+ EraseSymbol(*localSymbol);
+ details.add_occurrence(location, useSymbol);
+ Symbol *newSymbol{&MakeSymbol(localName, Attrs{}, std::move(details))};
+ // Restore *localSymbol in currScope
+ auto iter{currScope().find(localName)};
+ CHECK(iter != currScope().end() && &*iter->second == newSymbol);
+ iter->second = MutableSymbolRef{*localSymbol};
+ combinedDerivedType = newSymbol;
} else {
ConvertToUseError(*localSymbol, location, useSymbol);
localDerivedType = nullptr;
diff --git a/flang/test/Semantics/bug168099.f90 b/flang/test/Semantics/bug168099.f90
new file mode 100644
index 0000000000000..bc08933d7a1a6
--- /dev/null
+++ b/flang/test/Semantics/bug168099.f90
@@ -0,0 +1,28 @@
+!RUN: %python %S/test_errors.py %s %flang_fc1
+module m1
+ type pair
+ end type
+ interface pair
+ module procedure f
+ end interface
+ contains
+ type(pair) function f(n)
+ integer, intent(in) :: n
+ f = pair()
+ end
+end
+module m2
+ type pair
+ end type
+end
+module m3
+ type pair
+ end type
+end
+program main
+ use m1
+ use m2
+ use m3
+ !ERROR: Reference to 'pair' is ambiguous
+ type(pair) error
+end
More information about the flang-commits
mailing list