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

            Bug ID: 107968
           Summary: array slicing gives wrong result for an array pointer
                    defined in a subroutine
           Product: gcc
           Version: 12.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dreier at dkrz dot de
  Target Milestone: ---

Created attachment 54010
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=54010&action=edit
source code

The following code show some weird behavior to me:
```
PROGRAM foo
    implicit none

    TYPE t_geographical_coordinates
      REAL :: lon
      REAL :: lat
    END TYPE t_geographical_coordinates

    TYPE t_vertices
      REAL, POINTER         :: vlon(:) => null()
      REAL, POINTER         :: vlat(:) => null()
    END TYPE t_vertices

    TYPE(t_vertices), POINTER  :: vertices_pointer
    TYPE(t_vertices), TARGET  :: vertices_target
    TYPE(t_geographical_coordinates), ALLOCATABLE, TARGET :: vertex(:)

    ! initialization
    ALLOCATE(vertex(2))
    vertex%lon = 1
    vertex%lat = 2

    ! obtain pointer to (non-contiguous) field
    vertices_target%vlon => vertex%lon

    ! set pointer in a subroutine
    CALL set_vertices_pointer(vertices_target)

    WRITE (0,*) vertices_pointer%vlon
    WRITE (0,*) vertices_pointer%vlon(1:)

    ! It seems that setting the pointer in a subroutine does something else
than
    ! setting it directly. Furthermore uncommenting the following line fixes
the
    ! issue but changes the output of the lines above ?!

    ! vertices_pointer => vertices_target

    CONTAINS

    SUBROUTINE set_vertices_pointer(vertices)
      TYPE(t_vertices), POINTER, INTENT(IN) :: vertices
      vertices_pointer => vertices
    END SUBROUTINE set_vertices_pointer
END PROGRAM foo
```

I expect the output to be:
```
   1.00000000       1.00000000    
   1.00000000       1.00000000
```
Instead, the compiled program gives me
```
   1.00000000       1.00000000    
   1.00000000       2.00000000

```

Compiled with gfortran 12.2 on Ubuntu 22.10.
No warnings with `-Wall -Wextra`. Same behavior with `-fno-strict-aliasing
-fwrapv`.

Reply via email to