Restricting access of contained procedure to variables in host

Fortran 2018 enhances IMPORT facility which helps precisely with situations mentioned here. The document proxy (18-007r1, section 8.8) for the standard provides details: Here’s an excerpt:

1 8.8 IMPORT statement
2 R867 import-stmt is IMPORT [[ :: ] import-name-list ]
3 or IMPORT, ONLY : import-name-list
4 or IMPORT, NONE
5 or IMPORT, ALL
6 C896 (R867) An IMPORT statement shall not appear in the scoping unit of a main-program, external
7 subprogram, module, or block-data.

Here’s a check with Intel oneAPI IFORT classic compiler (available to everyone for download) that works as expected:

module m
   implicit none
contains
   subroutine sub1( x1, x2 )
      real, intent(in) :: x1, x2
      call sub2()
   contains
      subroutine sub2()
         import, only : x1 !<-- only x1 is imported from containing scope
         print *, "x1 = ", x1, "; expected is 123.0"
         print *, "x2 = ", x2  !<-- error because x2 not imported nor declared 
      end subroutine 
   end subroutine 
end module
   use m, only : sub1
   real :: a, b
   a = 123.0 ; b = -1.0
   call sub1( a, b )
end

C:\Temp>ifort /c /standard-semantics /warn:all /stand:f18 p.f90
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on Intel(R) 64, Version 2021.1 Build 20201112_000000
Copyright (C) 1985-2020 Intel Corporation. All rights reserved.

p.f90(11): error #6404: This name does not have a type, and must have an explicit type. [X2]
print *, "x2 = ", x2 !<-- error because x2 not imported nor declared
---------------------------^

5 Likes