Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.

Derived type component as do-variable

John4
Valued Contributor I
574 Views

Hi,

The following code is rejected by the ifort compiler.

implicit none

type t
    integer :: i = 0
end type

type(t) :: a

do a%i = 1, 10
    print *,'something'
enddo

end

Looking at the standard, I saw no constraint about the do-variable (other than being of integer type), but gfortran also rejects the code. Am I missing something?



 

0 Kudos
3 Replies
mecej4
Honored Contributor III
574 Views

The 2008 standard says "C812 (R819) The do-variable shall be a variable of type integer." and "R819 do-variable is scalar-int-variable-name", which may be interpreted to mean that an element of an array of integers or an integer component of a derived type may not be accepted. The similar program

implicit none

integer i(2)

do i(2) = 1, 10
    print *,'something'
enddo

end

causes the Intel compiler to emit the following erudite messages:

cdo.f90(5): error #5082: Syntax error, found IDENTIFIER 'I' when expecting one of: ( % [ : . = =>
do i(2) = 1, 10
---^
cdo.f90(5): error #5082: Syntax error, found ',' when expecting one of: <END-OF-STATEMENT> ;
do i(2) = 1, 10
-----------^
cdo.f90(5): error #6404: This name does not have a type, and must have an explicit type.   [DO]
do i(2) = 1, 10
^
cdo.f90(5): error #6460: This is not a field name that is defined in the encompassing structure.   
do i(2) = 1, 10
---^
cdo.f90(7): error #6099: An ENDDO statement occurred without a corresponding DO or DO WHILE statement.
enddo
^

 

0 Kudos
Thomas_F_1
Beginner
574 Views

Oddly, the following will work, and a%i takes on the correct values:

implicit none

type t
    integer :: i = 0
end type

type(t) :: a

associate (i => a%i)
do i = 1, 10
    print *,'something'
enddo
end associate

end

 

0 Kudos
Steven_L_Intel1
Employee
574 Views

Not odd at all. With ASSOCIATE, i is a named scalar-integer-variable which is "construct associated" with a%i. It's similar to passing a%i as an argument to a subroutine that uses the corresponding dummy argument as a DO variable.

0 Kudos
Reply