Allocation of type components using associate


it seems that allocating a component of a type using the associated name does not produce an allocated array usable outside the associate region.

program test
    type :: ta
        real, allocatable, dimension(:) :: x
    type(ta) :: a
    associate(a_x => a%x)
        print*,'in associate: allocated(x) : ',allocated(a_x),size(a_x,1)
    print*,'out associate: allocated(x) : ',allocated(a%x),size(a%x,1)
endprogram test

(Tested using nvfortran 22.2-0 linuxpower. )

Is this the expected behavior or a bug?


I’m not sure it’s legal code and we’re not catching the error. Both gfortran (v11.2) and nagfor give the following error:

% nagfor test.F90
NAG Fortran Compiler Release 7.0(Yurakucho) Build 7048
Error: test.F90, line 7: Substring of non-CHARACTER item A_X
       detected at )@)
Error: test.F90, line 7: A_X is neither ALLOCATABLE nor a POINTER
       detected at )@)
[NAG Fortran Compiler pass 1 error termination, 2 errors]

% gfortran test.F90

    7 |         allocate(a_x(1:10))
      |                 1
Error: Allocate-object at (1) must be ALLOCATABLE or a POINTER

    8 |         print*,'in associate: allocated(x) : ',allocated(a_x),size(a_x,1)
      |                                                         1
Error: ‘array’ argument of ‘allocated’ intrinsic at (1) must be ALLOCATABLE

Though it may possibly a new feature in F2018 which isn’t supported as of yet.

If needed, I can ask our compiler folks for a more definitive answer.


Ok, sorry. It seems forbidden if I well understand the standard 2018 ( “The associating entity does not have the ALLOCATABLE or POINTER attributes”. If so, maybe it would be nice to have the compiler detecting the error to help debugging complex codes.

Thanks, Francesco

Sounds good. I submitted TPR #31620 to see if we can catch the error as well.


Also see

If a selector is allocatable, it shall be allocated; the associate name is associated with the data object and does not have the ALLOCATABLE attribute.