Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
Welcome to the Intel Community. If you get an answer you like, please mark it as an Accepted Solution to help others. Thank you!

Nested blocks mess up variables

Benjamin_D_2
Beginner
90 Views
According to the manual, a BLOCK construct is itself a scoping unit. At the same time, BLOCKs can be named, and the names must be unique within their scoping unit. This means that by putting blocks inside each other, they can have the same name, since they form new scoping unit. Variables inside a block are supposed to hide variables of the same name outside of the block. Hence, the following code should, and does, print 1, 2:
block
   integer :: a = 1

   block
      integer :: a = 2

      print *, a
   end block
   print *, a
end block
This is fine. Now I give a name to the blocks, and I choose the same name. For this, a third block is required which allows me to use the name again.
aName: block
   integer :: a = 1

   block
      aName: block
         integer :: a = 2

         print *, a
      end block aName
   end block
   print *, a
end block aName
This time, the output is 1, 1, and by looking at the assembler output, the compiler indeed assigns the very same position in memory to the two variables a. If, instead of using the initialization statement, I directly set the values to 1 and 2 after declaration, the output is 2, 2. The problem is not caused by the third block, but by the double names. Compiler: Visual Fortran Compiler 17.0.2.187
0 Kudos
2 Replies
Kevin_D_Intel
Employee
90 Views

I can reproduce this and reported it to Development. It is the use of the same name that leads to the incorrect results. Unique names avoids the issue.

(Internal tracking id: CMPLRS-42483)

Kevin_D_Intel
Employee
90 Views

Development informs me the program is invalid and that ifort should have issued an error regarding a duplicate construct name, ANAME.

They indicate that Construct names (which is what aName is in this routine) are local identifiers.

In section 19.1 of the draft F2015 standard, it states:

2  The scope of
6     • a local identifier is an inclusive scope,

and under the section "Terms and Definitions", 3.90, it defines inclusive scope as:

11 inclusive scope
12 nonblock scoping unit plus every block scoping unit whose host is that scoping unit or that is nested within such
13 a block scoping unit

    NOTE 3.5
    That is, inclusive scope is the scope as if BLOCK constructs were not scoping units.

Thus, 2 BLOCKs within the same routine cannot have the same construct name (aName).

ifort will be made to issue an appropriate error in the future.

Reply