Skip to content

Commit 47ef3d0

Browse files
authored
[Flang] Avoid crash when a function return is undefined (#151577)
Properly terminate the StatementContext cleanup when a function return value is undefined. Fixes #126452
1 parent 2444c4a commit 47ef3d0

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

flang/lib/Lower/Bridge.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1732,7 +1732,13 @@ class FirConverter : public Fortran::lower::AbstractConverter {
17321732
Fortran::lower::SymbolBox resultSymBox = lookupSymbol(resultSym);
17331733
mlir::Location loc = toLocation();
17341734
if (!resultSymBox) {
1735-
mlir::emitError(loc, "internal error when processing function return");
1735+
// Create a dummy undefined value of the expected return type.
1736+
// This prevents improper cleanup of StatementContext, which would lead
1737+
// to a crash due to a block with no terminator. See issue #126452.
1738+
mlir::FunctionType funcType = builder->getFunction().getFunctionType();
1739+
mlir::Type resultType = funcType.getResult(0);
1740+
mlir::Value undefResult = builder->create<fir::UndefOp>(loc, resultType);
1741+
genExitRoutine(false, undefResult);
17361742
return;
17371743
}
17381744
mlir::Value resultVal = resultSymBox.match(
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
!RUN: %flang -c %s -### 2>&1
2+
function s(x) result(i)
3+
!CHECK-WARNING: Function result is never defined
4+
integer::x
5+
procedure():: i
6+
end function
7+
end

0 commit comments

Comments
 (0)