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

            Bug ID: 125534
           Summary: ALLOCATE of parameterized derived-type array
                    initializes only first element
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jvdelisle at gcc dot gnu.org
  Target Milestone: ---

When allocating an array of a parameterized derived type (PDT) that has
allocatable components, gfortran initializes only the first element.  Accessing
the allocatable components of any other element uses garbage pointers and
crashes.

  module m
    implicit none
    integer, parameter :: default_real = kind(0.)
    type :: tensor_t(k)
      integer, kind :: k = default_real
      real(k), allocatable :: values_(:)
    end type
  end module

  program test
    use m
    implicit none
    integer, parameter :: n = 5
    type(tensor_t), allocatable :: a(:)
    integer :: i

    allocate (a(n))
    do i = 1, n
      a(i)%values_ = [real(i), real(i*2)]
    end do

    if (any (a(3)%values_ /= [3., 6.])) stop 1
  end program

  $ gfortran pdt_array_alloc_1.f90 && ./a.out

  The program crashes because a(2) through a(n) have
  uninitialized allocatable component pointers.  The
  program should run and exit cleanly.

An ugly one for sure.

Reply via email to