[Bug fortran/126478] New: Bogus error for coarray ALLOCATE with MOLD= or SOURCE= specifier

dobonachea at lbl dot gov via Gcc-bugs <[email protected]>
Newsgroups gmane.comp.gcc.bugs
Message-ID <[email protected]/bugzilla/>
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=126478

            Bug ID: 126478
           Summary: Bogus error for coarray ALLOCATE with MOLD= or SOURCE=
                    specifier
           Product: gcc
           Version: 16.1.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: dobonachea at lbl dot gov
                CC: damian at archaeologic dot codes
  Target Milestone: ---

Created attachment 65164
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65164&action=edit
ALLOCATE statement for an allocatable array coarray with a SOURCE= specifier

The ALLOCATE statement for an allocatable array coarray is not correctly parsed
with a MOLD= or SOURCE= specifier by GFortran 16.1.0.

The two demonstration programs below both compile and execute correctly with
NAG Fortran 7.2:

---------------------------------------------------------
cgpu$ cat coarrays_31.f90

program coarrays_31
    implicit none

    ! Declare an allocatable array coarray
    integer, allocatable, save :: arr_coarray(:)[:]
    integer :: i

    ! Allocate the coarray using a SOURCE= specifier.
    ! The array dimensions and values are taken from the array constructor.
    ! The explicit [*] defines the co-dimensions.
    allocate(arr_coarray[*], source=[(i * 10, i = 1, 5)])

    ! Verify the allocation state
    if (.not. allocated(arr_coarray)) then
        error stop "Error: arr_coarray was not allocated."
    end if

    ! Verify the inherited size and values
    if (size(arr_coarray) /= 5) then
        error stop "Error: array size does not match SOURCE."
    end if

    if (any(arr_coarray /= [10, 20, 30, 40, 50])) then
        error stop "Error: array values do not match SOURCE."
    end if

end program coarrays_31

cgpu$ gfortran -fcoarray=lib -c coarrays_31.f90    

coarrays_31.f90:11:25:

   11 |     allocate(arr_coarray[*], source=[(i * 10, i = 1, 5)])
      |                         1
Error: Array section designator, e.g. '(:)', is required besides the coarray
designator '[...]' at (1)

cgpu$ cat coarrays_32.f90     

program coarrays_32
    implicit none

    real, allocatable :: prototype(:,:)
    real, allocatable :: target_arr(:,:)[:]

    allocate(prototype(10:12, 20:23))

    allocate(target_arr[*], mold=prototype)

    if (.not. allocated(target_arr)) error stop "Allocation failed"

    deallocate(target_arr)
    deallocate(prototype)

end program coarrays_32

cgpu$ gfortran -fcoarray=lib -c coarrays_32.f90 

coarrays_32.f90:9:24:

    9 |     allocate(target_arr[*], mold=prototype)
      |                        1
Error: Array section designator, e.g. '(:)', is required besides the coarray
designator '[...]' at (1)

cgpu$ gfortran --version

GNU Fortran (GCC) 16.1.0
Copyright (C) 2026 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

---------------------------------------------------------

The error messages asking for an "Array section designator" (grammatically, an
allocate-shape-spec-list) are incorrect. Redundantly providing the *optional*
explicit array bounds is an effective workaround to the defect, but that is
*not* required by Fortran specification.

The relevant specification text from Fortran 2023, subclause 9.7.1.1: (where
R933 is corrected as per J3/25-167)

R929 allocate-stmt is ALLOCATE ( [ type-spec :: ] allocation-list \
                                 [ , alloc-opt-list ] )

R930 alloc-opt is ERRMSG = errmsg-variable
               or MOLD = source-expr
               or SOURCE = source-expr
               or STAT = stat-variable

R933 allocation is allocate-object [ ( allocate-shape-spec-list ) ] \
                                   [ lbracket allocate-coarray-spec rbracket ]
            or allocate-object ( [ lower-bounds-expr : ] upper-bounds-expr ) \
                                   [ lbracket allocate-coarray-spec rbracket ]

C943 (R929) If an allocate-object is an array, either allocate-shape-spec-list
            or upper-bounds-expr shall appear in its allocation, 
            or source-expr shall appear in the ALLOCATE statement and 
            have the same rank as the allocate-object.

The final clause of C943 specifies that neither allocate-shape-spec-list nor
upper-bounds-expr is required when the SOURCE= or MOLD= specifiers are
provided. That is, in fact, the sole purpose of the MOLD= specifier.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.