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

    <tr>
        <th>Summary</th>
        <td>
            [Flang] Missing finalization on `LBOUND` when the argument is a reference to a function that has finalizable result.
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          DanielCChen
      </td>
    </tr>
</table>

<pre>
    Consider the following code
```
module m
    type base
        integer*4 :: id = 1
        contains
        final :: finalizeBaseRank1
    end type

 contains
    subroutine finalizeBaseRank1 (b)
        type (base), intent(in) :: b(:)

        print *, 'finalizeBaseRank1'
    end subroutine

    type (base) function func (i)
        integer*4, intent(in) :: i
        dimension func(10:i+10)
        func%id = 0
 end function

end module

program main
use m
integer :: res
res = lbound(func(4), 1)       ! Missing finalization call
res = ubound(func(4), 1)       ! Has the finalization call as expected
end

```

Flang seems missing the call to the finalization routine for `lbound` because likely the call is folded at compiler time because the result is 1. However, the standard requires the finalization for the result of reference such a function.
```
If an executable construct references a nonpointer function, the result is finalized after execution of the innermost executable construct containing the reference.
```

</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMlNuOozgQhp_G3JQmMiYkcMEFnSialfYgrbQPYHAB3jF21ofu6X36lQ1JmHSvNChSOFT9_lz1l7lzctSIDSlfSHnOePCTsc2Za4nqdJpQZ50R783JaCcFWvATwmCUMm9Sj9AbgYS25EDXH21nI4JCmAltAQD8-xWh4w7X53hJ7XFES1i7B1K0pGhBCiDFGfJNVG-051K7zatBaq5uKelB_osv3OGfXH-75aIWadXIRdtnGRc6a4KXGj8KAGFVR1i9WTHhx9dxB6wm7JTotSeskpqw-kbTEVbF25S9EbhaqT0Q1sZUwo4fqdlxA_7Ae8g8McAQdO-l0ekmfpA_Mm_K-_-8cpMg5Iza3RQJq3JKilYS9pLTH6WX7-XardjuRH0jWpjjm8UFy_PVmtHyGWYuY0RwiztWzBuQxdgiiy5Jq84ELQirVqL9Wvw8bmG5CMvhN-lc9OFaVZ7K0nOlNlLh56S-creY-1kKuAP8fsXeo1h2t2xra3pC24viegSHODuYV6yolyS8-ah9t6GxQA503fGBQoc9j0VS8huq94eIdHHyBArgHnozX6WKAylnvKfEWIsuKB-j8x18NW_4Gr1wSt-c51pwK8DiP0HGCn3AijgbGTOAxQEt6h7BhX4Cfm_37qkKvwzANeB37IPnncI4e87b0PuHhgMO2uirie23D-esgA_426AI4EOMXGQjoRlSqNQa7Wyc_3zFde5vbbgDPENnoilEXdQ8wyY_FjXLywM7ZlOT78s6Z33NhhKLrmTHivP9nhbHgiPtBpHJhlFWUpbTvGZHut8VeVcNJT9WSGmF4kD2FGcu1U6p13ln7JhJ5wI2OTuUFcsU71C5dPIyNkT7kKIdrInzKghj8Ty2Tcz90oXRkT1V0nn3UPPSq3RyJ--R8vz5PBgd_fXryx9__X6O_nqbUKeacDuGGXUqN9-02ZtNk8FP3MPE7x1JdV76tMuCVc3k_dWls-9C2GWUfgrdrjczYZeIuv59uVrzN_aesEuqgiPsshbitWH_BQAA__8luA7c">