From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 48) id AC1BF3858401; Mon, 5 Dec 2022 07:16:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org AC1BF3858401 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1670224581; bh=tUCwt3QNy97qEG9DsLfIUcMFWSJFmndZH1Vku5AkbrY=; h=From:To:Subject:Date:From; b=ZDvD6XLL3WnGSOlwx2cCsPusMAV7UojKIbSDZQfG7Az6hE5/MWFBqtTmwKcyEP+gH 3b0oGA8sAsWtzuynlWFg84C41VGjrFuKmBweNBw/OcDnwIGCnyuE9e8oF/3rHSH/aU Hw/AIgDp360XT9+WMn1FNMIo9dufMtxMAs6PdBDU= From: "dreier at dkrz dot de" 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 X-Bugzilla-Reason: CC X-Bugzilla-Type: new X-Bugzilla-Watch-Reason: None X-Bugzilla-Product: gcc X-Bugzilla-Component: fortran X-Bugzilla-Version: 12.2.0 X-Bugzilla-Keywords: X-Bugzilla-Severity: normal X-Bugzilla-Who: dreier at dkrz dot de X-Bugzilla-Status: UNCONFIRMED X-Bugzilla-Resolution: X-Bugzilla-Priority: P3 X-Bugzilla-Assigned-To: unassigned at gcc dot gnu.org X-Bugzilla-Target-Milestone: --- X-Bugzilla-Flags: X-Bugzilla-Changed-Fields: bug_id short_desc product version bug_status bug_severity priority component assigned_to reporter target_milestone attachments.created Message-ID: Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Bugzilla-URL: http://gcc.gnu.org/bugzilla/ Auto-Submitted: auto-generated MIME-Version: 1.0 List-Id: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=3D107968 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=3D54010&action=3Dedit 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(:) =3D> null() REAL, POINTER :: vlat(:) =3D> 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 =3D 1 vertex%lat =3D 2 ! obtain pointer to (non-contiguous) field vertices_target%vlon =3D> 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 =3D> vertices_target CONTAINS SUBROUTINE set_vertices_pointer(vertices) TYPE(t_vertices), POINTER, INTENT(IN) :: vertices vertices_pointer =3D> vertices END SUBROUTINE set_vertices_pointer END PROGRAM foo ``` I expect the output to be: ``` 1.00000000 1.00000000=20=20=20=20 1.00000000 1.00000000 ``` Instead, the compiled program gives me ``` 1.00000000 1.00000000=20=20=20=20 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`.=