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

--- Comment #3 from Steve Kargl <sgk at troutmask dot apl.washington.edu> ---
On Tue, May 15, 2018 at 04:50:41AM +0000, angus at agibson dot me wrote:
> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85786
> 
> --- Comment #2 from Angus Gibson <angus at agibson dot me> ---
> Changing the declaration of e to also be 'target', and associating the 
> pointer:
> 
>     CS%v(2)%p => e
>     if (query_ptr(e, CS)) then
>     ...
> 
> still causes the segfault.  Perhaps I disconnected this from the original code
> too much! Even if CS%v(2)%p wasn't associated, I don't see why there should be
> a segfault?

Here's a slight rewrite with debugging print statements.
Certainly, seems like you've found a bug :(


program test

   implicit none

   type :: p2d
      real, pointer :: p(:,:) => null()
   end type p2d

   type :: test_cs
      type(p2d), pointer :: v(:) => null()
   end type test_cs

   type(test_cs), pointer :: cs
   real, allocatable, target :: e(:,:)

   allocate(cs)
   print '(A,L1)', 'associated(cs) = ', associated(cs)

   allocate(cs%v(2))
   print '(A,L1)', 'associated(cs%v) = ', associated(cs%v)

   allocate(e(2,2))
   e = 42
   print '(A,I0)', 'loc(e) = ', loc(e)
   print '(A,4F6.1)', 'e = ', e

   if (query_ptr(e, cs)) then
      print *, 'associated'
   else
      print *, 'not associated'
   end if

   contains

      logical function query_ptr(f_ptr, cs)

         real, target, intent(in) :: f_ptr(:,:)
         type(test_cs), pointer, intent(inout) :: cs

         print '(A,I0)',    'loc(f_ptr) = ', loc(f_ptr)
         print '(A,4F6.1)', 'f_ptr = ', f_ptr

         if (associated(cs)) then
            print *, 'in query'
            print '(A,L1)', 'associated(cs%v) = ', associated(cs%v)
            cs%v(2)%p => f_ptr
            print '(A,L1)', 'associated(cs%v(2)%p) = ', associated(cs%v(2)%p)
            print '(A,I0)', 'loc(cs%v(2)%p) = ', loc(cs%v(2)%p)
            query_ptr = associated(cs%v(2)%p, f_ptr)
         else
            query_ptr = .false.
         end if
  end function query_ptr

end program test

Pretty good indication of why I don't use pointers.

Reply via email to