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

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Crash when a function result is used for `merge` argument
        </td>
    </tr>

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

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

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

<pre>
    @jeanPerier #118070 has a regression.

A binary produced by Flang crashes.

Source program:

```fortran
program main
  implicit none
  type :: ty
 integer, allocatable :: val(:)
  end type
  type(ty) :: a1(1), a2(1), a3(1)
  allocate(a1(1)%val(1), source=1)
  allocate(a2(1)%val(1), source=2)
  allocate(a3(1)%val(1), source=3)
  a3 = merge(f(a1), a2, .true.)
  print *, a3(1)%val(1)
contains
  function f(d) result(r)
 implicit none
    type(ty), intent(in) :: d(:)
    type(ty) :: r(size(d))
 r = d
  end function
end
```

Run (on AArch64):

```console
$ flang test.f90
$ ./a.out
Segmentation fault (core dumped)
```

This is detected by [Fujitsu Compiler Test Suite run on Linaro CI](https://linaro.atlassian.net/browse/LLVM-1459). The soruce program above is a simplified version of [Fujitsu/Fortran/0173/Fujitsu-Fortran-0173_0248.test](https://github.com/fujitsu/compiler-test-suite/blob/main/Fortran/0173/0173_0248.f90). I confirmed the commit cd7e65398fbbd9642573013800dc3ae1e7307f82 (#118070) and eb49788bd935b2335f73fd2a4124c82e683bd9b0 (the latest latest `main` branch) have this problem but the commit before cd7e65398fbbd9642573013800dc3ae1e7307f82 does not have the problem.

This may be related to #113843, though #113843 is reported before #118070.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVU2L4zgT_jXKpYiRJduxDzlkujEMzAsvO8NeF1kq2-q1pSDJ3WR__VKOk07vNDsLgaByPU99V6kY7eAQj6z8wsrnnVrS6MPxT_Wm4mhnte9fdp03l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