fortrangfortran

Polymorphic assumed-rank entity used in SELECT TYPE


After using Fortran for 3+ decades, I've hit a problem where polymorphism and an assumed-rank entity would seemingly provide a concise algorithm. Using an assumed-rank entity in a SELECT TYPE leads to an odd error message with gfortran.

gfortran -o z a.f90
a.f90:78:22:

78 |          select type (a)
   |                      1
Error: Assumed-rank variable a at (1) may only be used as actual argument

Is the following code standard conforming? I've read parts of the F2018 and F2023 standards, but have been unable to divine an answer. The shortest MWE of what I'm trying to do is

program foo

   implicit none

   integer i
   real(4) x

   i = 1
   x = 42

   call bar(i)
   call bar(x)

   contains

      subroutine bar(a)
         class(*), intent(in) :: a(..)
         integer rnk, typ

         select rank (a)
         rank (0)
             rnk = 0
         rank (1)
             rnk = 1
         rank default
            stop 'bad rank'
         end select

         select type (a)
         type is (integer)
            typ = 0
         type is (real(4))
            typ = 1
         class default
            print *, 'bad type'
         end select

!         call something(rnk, typ)

      end subroutine bar

end program foo


Solution

  • Well, I finally found the answer. gfortran implemented Fortran TS 29113 C535b, which appeared prior to assumed-rank. Fortran 2003 contains

    C840  An assumed-rank variable name shall not appear in a designator
    or expression except as an actual argument that corresponds to a dummy
    argument that is assumed-rank, the argument of the function C_LOC or
    C_SIZEOF from the intrinsic module ISO_C_BINDING (18.2), the first
    dummy argument of an intrinsic inquiry function, or the selector of
    a SELECT RANK statement.
    

    So not a bug in gfortran.

    Edit: It seems that J3 has recently passed https://j3-fortran.org/doc/year/24/24-171.txt, which removes the restrict in C840 for assumed-rank entities.