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

Protected array variables and submodules

Johannes_R_
Beginner
218 Views

The following code is modified from https://software.intel.com/en-us/forums/intel-visual-fortran-compiler-for-windows/topic/610969.

module mymod
  implicit none
  integer, protected :: n
  integer, protected :: m(1)
  interface
     module subroutine set(i)
       integer, intent(in) :: i
     end subroutine set
  end interface
end module mymod

submodule(mymod) mymod_i
contains
  module subroutine set(i)
    integer, intent(in) :: i
    n = i          ! OK
    m = i          ! OK
    m(1) = i       ! ERROR #7986
    call foo(m(1)) ! ERROR #7993
  end subroutine set

  subroutine foo(x)
    integer, intent(out) :: x
    x = 10
  end subroutine foo
end submodule mymod_i

program test
  use mymod
  implicit none
  integer :: i

  i = 5
  call set_n(i)
  print *,n
end program test

This fails to compile:

$ ifort test.f90 -o test
test.f90(18): error #7986: A use associated object that has the PROTECTED attriute shall not appear in a variable definition context.  
    m(1) = i       ! ERROR #7986
----^
test.f90(19): error #7993: A use associated object that has the PROTECTED attriute shall not appear as an actual argument if the associated dummy argument hasthe INTENT(OUT) or INTENT(INOUT) attribute.  
    call foo(m(1)) ! ERROR #7993
-------------^
compilation aborted for test.f90 (code 1)
$ ifort --version
ifort (IFORT) 17.0.1 20161005
Copyright (C) 1985-2016 Intel Corporation.  All rights reserved.

There was previously a compiler bug, now fixed, which prevented the "n" protected variable from being set in the submodule. It seems this problem still occurs for arrays.

 

0 Kudos
2 Replies
FortranFan
Honored Contributor II
218 Views

" It seems this problem still occurs for arrays." - it's unfortunate, hopefully this will be fixed in the near future.

0 Kudos
Kevin_D_Intel
Employee
218 Views

Thank you for reporting this variant. I escalated it to our Developers.

(Internal tracking id: DPD200418117)

0 Kudos
Reply