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

Andrew Benson <abensonca at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |abensonca at gcc dot gnu.org

--- Comment #8 from Andrew Benson <abensonca at gcc dot gnu.org> ---
The fix for this PR 0626  handles a scalar lhs whose type has no pointer or
allocatable components. Its commit message says that "The allocatable-component
case remains PR fortran/57696". But PR 57696 is a different bug: there, the
component that has the defined assignment is itself allocatable. If the type
instead has any *other* pointer or allocatable component, the duplicated
finalization from this PR remains:

module m
   implicit none
   type :: foo
      integer :: x = 0
   contains
      generic   :: assignment(=) => assign
      final     :: finalize
      procedure :: assign
   end type
   type :: bar               ! as in PR 110626
      type(foo) :: f
   end type
   type :: barAlloc          ! as bar, plus an allocatable component
      type(foo)            :: f
      integer, allocatable :: a
   end type
   type :: barPtr            ! as bar, plus a pointer component
      type(foo)            :: f
      integer, pointer     :: p => null()
   end type
contains
   subroutine assign(self, src)
      class(foo), intent(out) :: self
      type(foo),  intent(in)  :: src
      write(*,'(a,i0,a,i0)') "  assign ", self%x, " <- ", src%x
      self%x = src%x
   end subroutine
   subroutine finalize(self)
      type(foo), intent(inout) :: self
      write(*,'(a,i0)') "  finalize ", self%x
      self%x = -1
   end subroutine
end module
program p
   use m
   implicit none
   type(bar)      :: x , y
   type(barAlloc) :: xa, ya
   type(barPtr)   :: xp, yp
   x %f%x = 7; y %f%x = 42
   xa%f%x = 7; ya%f%x = 42
   xp%f%x = 7; yp%f%x = 42
   write(*,*) 'bar:'
   y  = x
   write(*,*) 'barAlloc:'
   ya = xa
   write(*,*) 'barPtr:'
   yp = xp
end program

With 16.2.1 20261115 and trunk (17.0.0 20261115):

 bar:
  finalize 42
  finalize -1
  assign 0 <- 7
 barAlloc:
  finalize 42
  finalize 42
  assign 0 <- 7
 barPtr:
  finalize 42
  finalize 42
  assign 0 <- 7

For bar, the second finalization (of the INTENT(OUT) argument of assign) sees
the value left by the first, as it should after the PR 110626 fix. For
barAlloc and barPtr, both finalizations see the old value 42: as the PR 110626
commit message puts it, the INTENT(OUT) argument finalizes "the stale temporary
instead of the real component". This is because generate_component_assignments
still takes the old path (copying the lhs to the temporary t1) whenever the lhs
has pointer or allocatable components.

(For comparison, 16.0.1 prints "finalize 42, finalize 42" for all three, and
11.2.0 finalizes the rhs value for bar and barPtr.)

Reply via email to