[Flang] Avoid crash when a function return is undefined (#151577)
Properly terminate the StatementContext cleanup when a function return value is undefined. Fixes #126452
This commit is contained in:
parent
2444c4a698
commit
47ef3d069b
@ -1732,7 +1732,13 @@ private:
|
||||
Fortran::lower::SymbolBox resultSymBox = lookupSymbol(resultSym);
|
||||
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(
|
||||
|
7
flang/test/Lower/undef-func-result.f90
Normal file
7
flang/test/Lower/undef-func-result.f90
Normal file
@ -0,0 +1,7 @@
|
||||
!RUN: %flang -c %s -### 2>&1
|
||||
function s(x) result(i)
|
||||
!CHECK-WARNING: Function result is never defined
|
||||
integer::x
|
||||
procedure():: i
|
||||
end function
|
||||
end
|
Loading…
x
Reference in New Issue
Block a user