[flang-commits] [flang] [Flang] Avoid crash when a function return is undefined (PR #151577)
via flang-commits
flang-commits at lists.llvm.org
Thu Jul 31 12:00:33 PDT 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-flang-fir-hlfir
Author: Carlos Seo (ceseo)
<details>
<summary>Changes</summary>
Properly terminate the StatementContext cleanup when a function return value is undefined.
Fixes #<!-- -->126452
---
Full diff: https://github.com/llvm/llvm-project/pull/151577.diff
2 Files Affected:
- (modified) flang/lib/Lower/Bridge.cpp (+7)
- (added) flang/test/Lower/undef-func-result.f90 (+8)
``````````diff
diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp
index 1adfb96ab9a98..802bdae9a5f05 100644
--- a/flang/lib/Lower/Bridge.cpp
+++ b/flang/lib/Lower/Bridge.cpp
@@ -1733,6 +1733,13 @@ class FirConverter : public Fortran::lower::AbstractConverter {
mlir::Location loc = toLocation();
if (!resultSymBox) {
mlir::emitError(loc, "internal error when processing function return");
+ // Create a dummy undefined value of the expected return type.
+ // This prevents improper cleanup of StatementContext, which would lead
+ // to a crash due to a block with no terminator. See issue #126452.
+ mlir::FunctionType funcType = builder->getFunction().getFunctionType();
+ mlir::Type resultType = funcType.getResult(0);
+ mlir::Value undefResult = builder->create<fir::UndefOp>(loc, resultType);
+ genExitRoutine(false, undefResult);
return;
}
mlir::Value resultVal = resultSymBox.match(
diff --git a/flang/test/Lower/undef-func-result.f90 b/flang/test/Lower/undef-func-result.f90
new file mode 100644
index 0000000000000..e8f3c8d2cad7d
--- /dev/null
+++ b/flang/test/Lower/undef-func-result.f90
@@ -0,0 +1,8 @@
+! RUN: %python %S/test_errors.py %s %flang_fc1
+! XFAIL: *
+function s(x) result(i)
+!ERROR: internal error when processing function return
+integer::x
+procedure():: i
+end function
+end
``````````
</details>
https://github.com/llvm/llvm-project/pull/151577
More information about the flang-commits
mailing list