Allocation of type components using associate

Hi,

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
    endtype
    type(ta) :: a
    associate(a_x => a%x)
        allocate(a_x(1:10))
        print*,'in associate: allocated(x) : ',allocated(a_x),size(a_x,1)
    endassociate
    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?

Thanks
Francesco

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
test.F90:7:17:

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

    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.

-Mat

Ok, sorry. It seems forbidden if I well understand the standard 2018 (11.1.3.3) “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.

-Mat

Also see 19.5.1.6.2:

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.

Hi Francesco,

FYI, as of our 23.1 release, we produce the following error for this code:

% nvfortran test.f90 -V23.1
NVFORTRAN-S-0084-Illegal use of symbol a_x - must have a POINTER or ALLOCATABLE attribute (test.f90: 7)
NVFORTRAN-S-0074-Illegal number or type of arguments to allocated - keyword argument array (test.f90: 8)
  0 inform,   0 warnings,   2 severes, 0 fatal for test

-Mat