public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "dreier at dkrz dot de" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/107968] New: array slicing gives wrong result for an array pointer defined in a subroutine
Date: Mon, 05 Dec 2022 07:16:21 +0000	[thread overview]
Message-ID: <bug-107968-4@http.gcc.gnu.org/bugzilla/> (raw)

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	other threads:[~2022-12-05  7:16 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-05  7:16 dreier at dkrz dot de [this message]
2022-12-05 19:12 ` [Bug fortran/107968] " anlauf at gcc dot gnu.org
2022-12-05 21:36 ` anlauf at gcc dot gnu.org

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107968-4@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).