<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/126452>126452</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Compilation abnormally terminates when result-name of RESULT is not defined in function subprogram
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            flang
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          ohno-fj
      </td>
    </tr>
</table>

<pre>
    ```
Version of flang : 20.0.0(8aebcebd90b79b90eca828762fd3b0637e430e51)/AArch64
```

When `result-name` of `RESULT` is not defined in `function subprogram`, a compilation terminates abnormally.  
However, the suggestion that `result-name` is undefined is correctly output in the compilation message. 
The above compilation message output is correct. However, it is incorrect to terminate abnormally after that.

The following are the test program, Flang, Gfortran and ifx compilation/execution result.

snen_prc_err_038.f90:
```fortran
function s(x) result(i)
integer::x
procedure():: i
end function
end
```

```
$ flang snen_prc_err_038.f90
./snen_prc_err_038.f90:1:10: warning: Function result is never defined
  function s(x) result(i)
           ^
error: loc("/work/home/ohno/CT/test/fort/tp/reproducerJ/Metro/F03_fe1err/snen_prc_err_038.f90":4:1): internal error when processing function return
error: loc("/work/home/ohno/CT/test/fort/tp/reproducerJ/Metro/F03_fe1err/snen_prc_err_038.f90":2:10): block with no terminator, has %1:2 = "hlfir.declare"(%arg0, %0) <{operandSegmentSizes = array<i32: 1, 0, 0, 1>, uniq_name = "_QFsEx"}> : (!fir.ref<i32>, !fir.dscope) -> (!fir.ref<i32>, !fir.ref<i32>)
error: verification of lowering to FIR failed
flang: /work/groups/ssoft/compiler/llvm/src/llvm-main/flang/include/flang/Lower/StatementContext.h:57: Fortran::lower::StatementContext::~StatementContext(): Assertion `cufs.empty() && "invalid StatementContext destructor call"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang -fc1 -triple aarch64-unknown-linux-gnu -emit-obj -mrelocation-model pic -pic-level 2 -pic-is-pie -target-cpu generic -target-feature +outline-atomics -target-feature +v8a -target-feature +fp-armv8 -target-feature
 +neon -resource-dir /work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/lib/clang/21 -mframe-pointer=non-leaf -o /tmp/snen_prc_err_038-dfd162.o -x f95-cpp-input snen_prc_err_038.f90
 #0 0x0000400046e82310 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libLLVM.so.21.0git+0x5b82310)
 #1 0x0000400046e80378 llvm::sys::RunSignalHandlers() (/work/groups/ssoft/compiler/llvm/aarch64/main-latest/lib/libLLVM.so.21.0git+0x5b80378)
 #2 0x0000400046e804c8 SignalHandler(int) Signals.cpp:0:0
 #3 0x00004000370807a0 (linux-vdso.so.1+0x7a0)
 #4 0x000040004bec6274 raise (/lib64/libc.so.6+0x36274)
 #5 0x000040004beb0a2c abort (/lib64/libc.so.6+0x20a2c)
 #6 0x000040004bebfba0 __assert_fail_base (/lib64/libc.so.6+0x2fba0)
 #7 0x000040004bebfc18 __assert_perror_fail (/lib64/libc.so.6+0x2fc18)
 #8 0x0000000004e06fbc Fortran::lower::StatementContext::~StatementContext() FrontendActions.cpp:0:0
 #9 0x0000000004ec83b8 Fortran::frontend::CodeGenAction::beginSourceFileAction() (/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang+0x4ec83b8)
#10 0x0000000004dd73b0 Fortran::frontend::FrontendAction::beginSourceFile(Fortran::frontend::CompilerInstance&, Fortran::frontend::FrontendInputFile const&) (/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang+0x4dd73b0)
#11 0x0000000004dc69fc Fortran::frontend::CompilerInstance::executeAction(Fortran::frontend::FrontendAction&) (/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang+0x4dc69fc)
#12 0x0000000004ddbdb4 Fortran::frontend::executeCompilerInvocation(Fortran::frontend::CompilerInstance*) (/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang+0x4ddbdb4)
#13 0x0000000004dbf2a8 fc1_main(llvm::ArrayRef<char const*>, char const*) (/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang+0x4dbf2a8)
#14 0x0000000004dbecf0 main (/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang+0x4dbecf0)
#15 0x000040004beb4384 __libc_start_main (/lib64/libc.so.6+0x24384)
#16 0x0000000004dbd1bc _start (/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang+0x4dbd1bc)
flang-20: error: unable to execute command: Aborted (core dumped)
flang-20: error: flang frontend command failed due to signal (use -v to see invocation)
flang version 21.0.0git (https://github.com/llvm/llvm-project.git 8aebcebd90b79b90eca828762fd3b0637e430e51)
Target: aarch64-unknown-linux-gnu
Thread model: posix
InstalledDir: /work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin
Build config: +assertions
flang-20: note: diagnostic msg:
********************

PLEASE ATTACH THE FOLLOWING FILES TO THE BUG REPORT:
Preprocessed source(s) and associated run script(s) are located at:
flang-20: note: diagnostic msg: /tmp/snen_prc_err_038-0d317e
flang-20: note: diagnostic msg: /tmp/snen_prc_err_038-0d317e.sh
flang-20: note: diagnostic msg:

********************
$
```

```
$ cat /tmp/snen_prc_err_038-0d317e
#line "./snen_prc_err_038.f90" 1
 function s(x) result(i)
      integer::x
      procedure():: i
      end function
      end
$
```

```
$ cat /tmp/snen_prc_err_038-0d317e.sh
# Crash reproducer for clang version 21.0.0git (https://github.com/llvm/llvm-project.git 8aebcebd90b79b90eca828762fd3b0637e430e51)
# Driver args: "snen_prc_err_038.f90"
# Original command: "/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang" "-fc1" "-triple" "aarch64-unknown-linux-gnu" "-emit-obj" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-target-cpu" "generic" "-target-feature" "+outline-atomics" "-target-feature" "+v8a" "-target-featur
e" "+fp-armv8" "-target-feature" "+neon" "-resource-dir" "/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/lib/clang/21" "-mframe-pointer=non-leaf" "-o" "/tmp/snen_prc_err_038-dfd162.o" "-x" "f95-cpp-input" "snen_prc_err_038.f90"
 "/work/groups/ssoft/compiler/llvm/aarch64/main-20250203-8aebcebd90b7/bin/flang" "-fc1" "-triple" "aarch64-unknown-linux-gnu" "-emit-obj" "-mrelocation-model" "pic" "-pic-level" "2" "-pic-is-pie" "-target-cpu" "generic" "-target-feature" "+outline-atomics" "-target-feature" "+v8a" "-target-feature" "+fp-armv8" "-tar
get-feature" "+neon" "-mframe-pointer=non-leaf" "-x" "f95-cpp-input" "snen_prc_err_038-0d317e"
$
```

```
$ gfortran snen_prc_err_038.f90
snen_prc_err_038.f90:1:0:

    1 | function s(x) result(i)
      |
internal compiler error: in gfc_typenode_for_spec, at fortran/trans-types.cc:1149
Please submit a full bug report, with preprocessed source.
See <https://bugs.rockylinux.org/> for instructions.
$
```

```
$ ifx snen_prc_err_038.f90
snen_prc_err_038.f90(3): warning #6178: The return value of this FUNCTION has not been defined.   [I]
procedure():: i
--------------^
LLVM ERROR: unable to allocate function return #0
 #0 0x000000000312d792
          #1 0x0000000003191e57
          #2 0x0000000003191f80
          #3 0x00001528ddf3a6f0
          #4 0x00001528ddf8794c
          #5 0x00001528ddf3a646 raise + 22
          #6 0x00001528ddf247f3 abort + 211
          #7 0x00000000026b32dc
          #8 0x00000000022e6181
          #9 0x00000000022e4e52
         #10 0x0000000002a77f78
         #11 0x0000000002491b87
         #12 0x0000000002491a01
         #13 0x000000000249cd46
         #14 0x000000000279be67
         #15 0x000000000279b8ff
         #16 0x00000000022a56b8
         #17 0x000000000222eef5
         #18 0x000000000222e8df
         #19 0x0000000002229377
         #20 0x00000000030ce5b9
         #21 0x00000000030cc544
         #22 0x000000000307863e
         #23 0x000000000324df17
         #24 0x00001528ddf25590
         #25 0x00001528ddf25640 __libc_start_main + 128
 #26 0x0000000002eb06ea

snen_prc_err_038.f90: error #5629: **Internal compiler error: abort signal raised** Please report this error along with the circumstances in which it occurred in a Software Problem Report.  Note: File and line given may not be explicit cause of this error.
compilation aborted for snen_prc_err_038.f90 (code 3)
$
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzsWktz4zaQ_jXwpYsqEJRI6uCD7LGS2XIys7aTHFUg2JSQoQAuAPqxh_3tWwCoF_2cTTK5rGs8JsFGo7vR6EZ_ALdWrhXiOZldkNmnM967jTbneqN00vx5Vun66ZzkdPhHF7-jsVIr0A00LVdrINkCGJ3QCSWsLDlWAqt6TqtiXs0pCl6ysshZU2cVzbMCpxnFWUrYnLDlYmHEJp8SujgegdDFHxtUQHJq0PatSxTfIsmpH5Pk9Obq9rfrO_8uLSjtoMZGKqxBhj5Nr4TzEtq-6oxeG771fNklcBB628mWh88OzVYq7tACr5Q2W962TxMAQhc_6we8R-P7uA2C7ddrtLHThrsXBJMWerUXw4LQxqBw7RPo3nW986J5Tsfjb9FavsaJH_Bug8Arff8ixZ7Hnu8EjiSU4YtUwzdw-qDakWbAG4cmKDCJRvaDNrpt9YNUa-AGg4gOrYOd3dglLP0k-4efGm2c4Qq4qkE2j8eiErbERxR9EDvaZhjEKlSrzogVGrOiWTlp5pRki-MpH_gSujhMHWHlI2HzgRdhpfQeQxdSOVyj8RyyxSOhi85ogXVvkLDSk4QPIAldoKphxzC-Pvezk1c2HTz6RZnpYkLY8hV1Uv_rH-CBGyXV2j8ud9pEJYK3-knb-SuhC4D3VYbDD5ldeU2M0d4C0GoRtGaELR-0-UbYcqO3SNjSr17Clpd3hC39hBK29Fb2bx1hS4Od0XUv0PwHYctf0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