Hi Andre,

Am 14.06.24 um 17:05 schrieb Andre Vehreschild:
Hi all,

I somehow got assigned to this PR so I fixed it. GFortran was ICEing because of
the ASSUME_RANK in a derived to class conversion. After fixing this, storage
association was producing segfaults. The "shape conversion" of the class array
as dummy argument was not initializing the dim 0 stride and with that grabbing
into the memory somewhere. This is now fixed and

regtests fine on x86_64 Fedora 39. Ok for mainline?

the patch fixes the testcase in your submission, but not the following
slight variation of the main program:

module foo_mod
  implicit none
  type foo
     integer :: i
  end type foo
contains
  subroutine d1(x,n)
    integer, intent(in) :: n
    integer :: i
    class (foo), intent(out) :: x(n)
    select type(x)
    class is(foo)
       x(:)%i = (/ (42 + i, i = 1, n ) /)
    class default
       stop 1
    end select
  end subroutine d1
  subroutine d2(x,n)
    integer, intent(in) :: n
    integer :: i
    class (foo), intent(in) :: x(n,n,n)
    select type (x)
    class is (foo)
       print *,x%i
if ( any( x%i /= reshape((/ (42 + i, i = 1, n ** 3 ) /), [n, n, n] ))) stop 2
    class default
       stop 3
    end select
  end subroutine d2
end module foo_mod
program main
  use foo_mod
  implicit none
  type (foo), dimension(:), allocatable :: f
  integer :: n
  n = 2
  allocate (f(n*n*n))
  ! Original testcase:
  call d1(f,n*n*n)
  call d2(f,n)                  ! OK
  call d1(f(1:n*n*n),n*n*n)
  print *, "After call d1(f(1:n*n*n:1),n*n*n):"
  print *, f%i
  call d2(f(1:n*n*n),n)         ! OK
  ! Using stride -1:
  call d1(f(n*n*n:1:-1),n*n*n)
  print *, "After call d1(f(n*n*n:1:-1),n*n*n):"
  print *, f%i
  call d2(f(n*n*n:1:-1),n)      ! not OK
  deallocate (f)
end program main

While this runs fine with the latest Intel compiler, gfortran including
your patch prints:

43 44 45 46 47 48 49 50
 After call d1(f(1:n*n*n:1),n*n*n):
43 44 45 46 47 48 49 50 43 44 45 46 47 48 49 50
 After call d1(f(n*n*n:1:-1),n*n*n):
50 49 48 47 46 45 44 43 43 0 0 49 0 34244976 0 34238480
STOP 2

So while the negative stride (-1) in the call to d1 appears to
work as it should, it does not work properly for the call to d2.
The first array element is fine in d2, but anything else isn't.

Do you see what goes wrong here?

(This may be a more general, pre-existing issue in a different place.)

Thanks,
Harald

P.S.: regarding your commit message, I think the reference to the pr
in brackets should be moved to the end of the summary line, i.e. for

Fortran: [PR96992] Fix rejecting class arrays of different ranks as storage association argument.

the "[PR96992" should be moved.  Makes it also easier to read.

I assume this patch could be fixing some other PRs with class array's parameter
passing, too. If that sounds familiar, feel free to point me to them.

Regards,
        Andre
--
Andre Vehreschild * Email: vehre ad gmx dot de


Reply via email to