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

            Bug ID: 78290
           Summary: Gfortran incorrectly creates a copy of an array passed
                    to an array pointer dummy argument
           Product: gcc
           Version: 6.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: andybugreports at gmail dot com
  Target Milestone: ---

The following code demonstrates incorrect behaviour of a array pointer being
passed to an array pointer dummy argument. As highlighted in the comment, the
compiler seems to generate a copy of the array rather than passing a pointer to
it. "sub1" seems to be doing the right thing, but due to the copy and what
looks like incorrect behaviour to attempt to copy some of it back the result is
wrong. Removing the line shown later in the source stops the copy occurring and
fixes the problem. Bug 53800 looks potentially similar:

PROGRAM main
  IMPLICIT NONE 
  INTEGER,PARAMETER::KI=4,KF=8,KL=4

  TYPE mytype
    INTEGER(KIND=KI)::i=1_KI
    REAL(KIND=KF)::r=2.0_KF
    LOGICAL(KIND=KL)::z=.TRUE._KL
  END TYPE mytype

  TYPE(mytype),DIMENSION(50),SAVE,TARGET::ta

  INTEGER(KIND=KI),DIMENSION(3),SAVE,TARGET::ia=1_KI
  INTEGER(KIND=KI),DIMENSION(:),POINTER::ia2=>NULL()
  INTEGER(KIND=KI),DIMENSION(:),POINTER,SAVE::ipsave=>NULL()
  INTEGER(KIND=KI),DIMENSION(:),POINTER::ip=>NULL()

  ALLOCATE(ia2(5)); ia2=2_KI

  ip=>ia; NULLIFY(ipsave)
  CALL sub1(ip)
  WRITE(*,*)'ia    ',ia
  WRITE(*,*)'ip    ',ip
  WRITE(*,*)'ipsave',ipsave

  ip=>ta%i !!! Works if you comment this line out !!!

! At writes: isave should point to ia which should be changed to all 3s
! ip should point to ip2
! Produces:
! ia               2           2           2
! ip               2           2           2
! ipsave           3           3           3
!
! Should produce:
! ia               3           3           3
! ip               2           2           2           2           2
! ipsave           3           3           3
!
! Seems to be creating a copy of ia and setting argument ipa as a pointer this,
and 
! changes to this never get transferred back to ia
! Also does not transfer back change of target or INOUT argugment
! Works if you comment out the line "ip=>ta%i" 
!
CONTAINS

   SUBROUTINE sub1(ipa)
     INTEGER(KIND=KI),DIMENSION(:),POINTER,INTENT(INOUT)::ipa ! Fortran 2003
     ! INTEGER(KIND=KI),DIMENSION(:),POINTER::ipa ! Fortan 95
     ipsave=>ipa 
     ipsave=3_KI
     ipa=>ia2
   END SUBROUTINE sub1

END PROGRAM main

Reply via email to