https://gcc.gnu.org/bugzilla/show_bug.cgi?id=125391

--- Comment #9 from Mikael Morin <mikael at gcc dot gnu.org> ---
(In reply to Mikael Morin from comment #8)
> 
> The third one is that we evaluate the lhs a second time for finalization,
> which can be problematic 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

Reply via email to