https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125427
Bug ID: 125427
Summary: Finalization in an assignment causes double evaluation
of lhs
Product: gcc
Version: 15.2.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: mikael at gcc dot gnu.org
Target Milestone: ---
This is taken from PR125391 comment #9.
Double evaluation for finalization can be bogus in case of side-effects.
Here is an example. We call post_increment twice inside save_val.
Tested with gfortran-15.2.
$ gfortran -static x.f90 -o x
$ ./x
3
ERROR STOP 1
Error termination. Backtrace:
#0 0x4026fd in MAIN__
#1 0x402739 in main
$
$ cat x.f90
module m
implicit none
type :: inner
integer :: x = 0
contains
final :: inner_final
end type
type :: outer
integer :: first_available
type(inner), allocatable :: items(:)
contains
procedure, pass :: save_val
end type
interface outer
module procedure :: constructor_outer
end interface
contains
subroutine inner_final(this)
type(inner), intent(inout) :: this
end subroutine
subroutine save_val(o, i)
class(outer), intent(inout) :: o
type(inner), intent(in) :: i
o%items(post_increment(o%first_available)) = i
end subroutine
function post_increment(i) result(r)
integer :: i, r
r = i
i = i + 1
end function
function constructor_outer(n) result(r)
integer :: n
type(outer) :: r
allocate(r%items(n))
r%first_available = 1
end function
end module
program p
use m
type(outer) :: x
x = outer(5)
call x%save_val(inner(-1))
print *, x%first_available
if (x%first_available /= 2) error stop 1
end program