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

            Bug ID: 129168
           Summary: type(c_ptr) assumed-rank dummy dereferences scalar
                    actual argument
           Product: gcc
           Version: 16.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: gaetanjalin at gmail dot com
  Target Milestone: ---

Created attachment 66345
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66345&action=edit
Reproducible example

When passing a scalar type(c_ptr) (or type(c_funptr)) actual argument to an
assumed-rank dummy, the C pointer is dereferenced and the dummy gets the data
pointed to by the C pointer instead of the address the C pointer holds.

This does not happen if the type(c_ptr) is passed as a rank-1 actual argument,
or to a plain scalar dummy.

The test case (see attached) was compiled with:
gfortran -v -Wall -Wextra -fno-strict-aliasing -fwrapv
-fno-aggressive-loop-optimizations -fsanitize=address,undefined -std=f2018
gfortran_assumed_rank_bug.f90

This was reproduced with GCC 16.2.0 (homebrew, aarch64-apple-darwin), and GCC
11.4.1 (Red Hat, x86_64-linux-gnu).
Intel oneAPI (ifx) 2023.0.0 gave the expected result and passed the test (Red
Hat, x86_64-linux-gnu).

-fdump-tree-original shows that the type(c_ptr) scalar is dereferenced when
passed to the assumed-rank dummy:
desc.17.data = c_data
take_cptr(&desc.17, &D.3232, &buffer[0])

while the rank-1 array is properly passed by reference:
atmp.22.data = (void * restrict) &A.23;
take_cptr (&atmp.22, &D.3259, &buffer[0]);

Reply via email to