fpt and WinFPT Reference Manual - Command-line Commands

| SimCon Home | Ref Manual Home |

Implementation of BLOCK - END BLOCK

The Fortran 2008 BLOCK - END BLOCK construct is implemented in gfortran versions 7.1.0 and 8.0.1 and in ifort V18. The construct encloses a scope in which new variables may be declared. If the new variables have the same names as variable in the enclosing scope, they supersede the variables in the enclosing scope only within the block. For example:

SUBROUTINE s INTEGER :: i = 42 WRITE(*,'("i before BLOCK: ",I2)')i BLOCK INTEGER i = 1 WRITE(*,'("i within BLOCK: ",I2)')i END BLOCK WRITE(*,'("i after BLOCK: ",I2)')i END SUBROUTINE s

runs with the result:

i before BLOCK: 42 i within BLOCK: 1 i after BLOCK: 42

The Fortran 2008 standard requires that any implicitly declared variable either within or outside a BLOCK construct shall have the scope of the enclosing sub-program. However in both ifort and gfortran, implicitly declared variables which first occur within a BLOCK construct have the scope of the block, not of the enclosing sub-program. Thus, the code:

WRITE(*,'(//,"Test of export of a new implicitly declared variable")') j = 1 BLOCK i = 42 j = 42 WRITE(*,'("i inside BLOCK: ",I2)')i WRITE(*,'("j inside BLOCK: ",I2)')j END BLOCK WRITE(*,'("i after BLOCK: ",I2)')i WRITE(*,'("j after BLOCK: ",I2)')j WRITE(*,'("End of test of export of a new variable")')

runs under both comp[ilers with the result:

Test of export of a new implicitly declared variable i inside BLOCK: 42 j inside BLOCK: 42 i after BLOCK: ** j after BLOCK: 42 End of test of export of a new variable

This is incorrect, but it should be noted that this situation will be very rare.

fpt therefore generates a diagnostic whenever an implicitly declared variable first occurs within a BLOCK construct. The diagnostic is also generated for implicitly declared objects in DO CONCURRENT, FORALL and ASSOCIATE constructs which also have internal scope. The code above, for example, is processed by fpt:

WRITE (*,'(//,"Test of export of a new implicitly declared variable")') J=1 BLOCK I=42 !-----------^------------------------------------------------------------------ !!! FPT - 3957 Implicitly declared object in a BLOCK may compile incorrectly !------------------------------------------------------------------------------ J=42 WRITE (*,'("i inside BLOCK: ",I2)')I WRITE (*,'("j inside BLOCK: ",I2)')J END BLOCK WRITE (*,'("i after BLOCK: ",I2)')I WRITE (*,'("j after BLOCK: ",I2)')J WRITE (*,'("End of test of export of a new variable")')

Copyright ©1995 to 2025 Software Validation Ltd. All rights reserved.