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

            Bug ID: 92703
           Summary: VALUE attribute: CLASS and derived-type with
                    allocatable components mishandled
           Product: gcc
           Version: 10.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: burnus at gcc dot gnu.org
            Blocks: 92702
  Target Milestone: ---

The test case shows that allocatable components of derived types are not copied
– just the derived type is (automatically) passed by value (according to the
platform ABI).

Hence, it works fine for data which is directly in the derived type – but not
for allocatable components.

With CLASS, I am not quite sure what goes wrong. I initially thought it is just
a missing copy – as the dump shows:

      class.20._data = &var;
      classy (&class.20);

But if I print x%A(:), it looks complete random and uninitialized.


Xref: PR 92702 – As it is also about copy-in of data (and later
finalizing/deallocation, if needed), I cross reference PR fortran/92702 which
is about VALUE + DIMENSION (permitted since F2008).


Testcase:

! { dg-do run }
!
! VALUE mishandled for CLASS and with ALLOCATABLE components
!
program main
  implicit none (type, external)
  type t
    integer :: A(100)
    complex :: B(1000,1000)
  end type t
  type t2
    integer, allocatable :: C(:)
  end type t2
  type(t) :: var
  type(t2) :: var2
  integer :: i

  allocate(var2%C(5))
  var2%C = [1,2,3,4,5]
  call foo(var2) ! var passed by value but var2%C not copied, hence:
  if (any(var2%C /= [1,2,3,4,5])) stop 2 ! FAILS HERE

  var%A = [((21*i), i = 1,100)]
  call classy(var)
  if (any(var%A /= [((21*i), i = 1, 100)])) stop 1

contains
  subroutine foo(y)
    type(t2), value :: y

    if (any(y%C /= [1,2,3,4,5])) stop 11
    y%C = [99,98,97,96,95]
    if (any(y%C /= [99,98,97,96,95])) stop 13
  end subroutine foo

  subroutine classy(y)
    class(t), value :: y

    if (any(y%A /= [((21*i), i = 1, 100)])) stop 1  ! FAILS HERE
    y%A(1) = 4
    if (y%A(1) /= 4) stop 22
  end subroutine classy
end program main


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=92702
[Bug 92702] [F2008] (and hence [F2018]) Implement VALUE support for arrays

Reply via email to