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

            Bug ID: 128935
           Summary: IS_CONTIGUOUS .false. for a full-length substring with
                    a non-constant range
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jvdelisle at gcc dot gnu.org
  Target Milestone: ---

Created attachment 66249
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=66249&action=edit
Draft patch to fix this

A substring of a contiguous character array is contiguous when the
substring range covers the whole string (F2023 8.5.7 p2 item (7)(e)).
gfortran gets this right only when the range is a constant equal to
the length.  When the start, the end, or the length is not a constant
(a variable bound, a deferred-length array, an assumed-length dummy),
IS_CONTIGUOUS returns .false. even when the substring covers the whole
string:

program p
  implicit none
  character(len=4) :: c(10)
  character(len=:), allocatable :: dc(:)
  integer :: n = 4
  allocate (character(len=3) :: dc(10))
  print *, is_contiguous (c(:)(1:4))         ! T
  print *, is_contiguous (c(:)(1:n))         ! T expected
  print *, is_contiguous (dc(:)(:len(dc)))   ! T expected
  call sub (c, 4)
contains
  subroutine sub (ac, m)
    character(len=*) :: ac(:)
    integer :: m
    print *, is_contiguous (ac(:)(1:m))      ! T expected
  end subroutine
end program

Output:
 T
 F
 F
 F

Expected:
 T
 T
 T
 T

The same check rejects a valid pointer assignment.  10.2.2.3 p6
requires the target of a CONTIGUOUS pointer to be contiguous, which
is a condition at run time, but gfortran reports an error at compile
time:

program p
  implicit none
  character(len=4), target :: c(10)
  character(len=:), pointer, contiguous :: cp(:)
  integer :: n = 4
  cp => c(:)(1:n)
  print *, is_contiguous (cp)
end program

    6 |   cp => c(:)(1:n)
      |        1
Error: Assignment to contiguous pointer from non-contiguous target at
(1)

Cause: resolution removes a substring reference only when its range
is constant and covers the whole string.  For any other substring of
an array section, gfc_is_not_contiguous takes the reference that
follows the section as proof that the section is not contiguous, so
IS_CONTIGUOUS folds to .false. and the pointer assignment is
rejected.  A substring known to be partial, such as c(:)(1:3), is
correctly not contiguous.

Attached is a fix.
  • [Bug fortran/128935] New: IS_CON... jvdelisle at gcc dot gnu.org via Gcc-bugs

Reply via email to