[flang-commits] [flang] [Flang][Semantics] Fix crash on invalid function result declaration (PR #194648)
via flang-commits
flang-commits at lists.llvm.org
Tue Apr 28 07:55:59 PDT 2026
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-semantics
Author: ShashwathiNavada
<details>
<summary>Changes</summary>
Fixes https://github.com/llvm/llvm-project/issues/194596.
When the function result symbol is encountered while the compiler is already completing the function result type, flang could recursively re-enter _CompleteFunctionResultType()_ and crash on invalid code.
Example:
```
character*(a) function bb() result(a) ! ERROR
implicit integer(a)
A = "a"
end
```
Instead of crashing on conflicting declarations, flang now reports an “already declared” error and stops further recursion.
---
Full diff: https://github.com/llvm/llvm-project/pull/194648.diff
1 Files Affected:
- (modified) flang/lib/Semantics/resolve-names.cpp (+11-1)
``````````diff
diff --git a/flang/lib/Semantics/resolve-names.cpp b/flang/lib/Semantics/resolve-names.cpp
index b6907cc792d76..409a793534399 100644
--- a/flang/lib/Semantics/resolve-names.cpp
+++ b/flang/lib/Semantics/resolve-names.cpp
@@ -496,6 +496,7 @@ class FuncResultStack {
// Result symbol
Symbol *resultSymbol{nullptr};
bool inFunctionStmt{false}; // true between Pre/Post of FunctionStmt
+ bool completingType{false}; // re-entrancy guard for CompleteFunctionResultType
// Functions with previous implicitly-typed references get those types
// checked against their later definitions.
const DeclTypeSpec *previousImplicitType{nullptr};
@@ -2874,6 +2875,7 @@ void FuncResultStack::CompleteFunctionResultType() {
if (info && &info->scope == &scopeHandler_.currScope() &&
info->resultSymbol) {
if (info->parsedType) {
+ auto completingTypeScope{common::ScopedSet(info->completingType, true)};
scopeHandler_.messageHandler().set_currStmtSource(info->source);
if (const auto *type{
scopeHandler_.ProcessTypeSpec(*info->parsedType, true)}) {
@@ -2908,7 +2910,15 @@ void FuncResultStack::CompleteFunctionResultType() {
void FuncResultStack::CompleteTypeIfFunctionResult(Symbol &symbol) {
if (FuncInfo * info{Top()}) {
if (info->resultSymbol == &symbol) {
- CompleteFunctionResultType();
+ if (info->completingType) {
+ if (!scopeHandler_.context().HasError(symbol)) {
+ scopeHandler_.SayAlreadyDeclared(symbol.name(),
+ info->resultName ? info->resultName->source : symbol.name());
+ scopeHandler_.context().SetError(symbol);
+ }
+ } else {
+ CompleteFunctionResultType();
+ }
}
}
}
``````````
</details>
https://github.com/llvm/llvm-project/pull/194648
More information about the flang-commits
mailing list