Reshaping arrays via explicit length arguments

According to my reading of the F2023 interpretation document J3/24-007, this case falls under sequence association (page 331, section 15.5.2.12, paragraph 1):

Sequence association only applies when the dummy argument is an explicit-shape or assumed-size array. The rest of this subclause only applies in that case

Paragraph 6 of the same section states,

An actual argument that represents an element sequence and corresponds to a dummy argument that is an array is sequence associated with the dummy argument. The rank and shape of the actual argument need not agree with the rank and shape of the dummy argument (emphasis added), but the number of elements in the dummy argument shall not exceed the number of elements in the element sequence of the actual argument. If the dummy argument is assumed-size, the number of elements in the dummy argument is exactly the number of elements in the element sequence.

The fact that this is an internal procedure appears irrelevant. It’s the explicit-shape dummy argument which permits sequence association. To get rank checking, rewrite the procedure as follows,

subroutine mustbe_1d(array)
    type(mytype), intent(inout) :: array(:)
    integer(int32):: j
    do j = 1, size(array)
      array(j)%k = j
    enddo
  end subroutine

In this case the rules state (page 237, paragraph 16),

If a dummy argument is an assumed-shape array, the rank of the actual argument shall be the same as the rank of the dummy argument, and the actual argument shall not be an assumed-size array.

So in this case the ranks must be the same.


The LAPACK procedures rely heavily on sequence association. For example if you look at the procedure DGESV, the B argument is 2-d array, however it is very common to solve a problem with a single right-hand side vector:

real(kind(1.0d0)) :: a(3,3), b(3)
integer :: ipiv(3), info
external :: dgesv
! ... fill a and b 
call dgesv(3,1,a,3,ipiv,b,3,info)

Before Fortran 90 introduced dynamic memory allocation, it was common to use fixed-size work buffers (i.e. memory pools) and designate areas for different purposes,

       integer ipiv(10), info
C work area for matrix A and vector b
       double precision work(10,10)
C solve a 3-by-3 system with 1 right-hand side
       call dgesv(3,1,work(1,1),10,ipiv,work(1,4),10,info)

LAPACK also uses storage sequence association heavily internally, for instance if you look at the DGETRF2 procedure.