ATK
ATK

Reputation: 1526

calling a type bound procedure in a PURE procedure Fortran

If I have declared type like

 type test(NSIZE)
  integer, len :: NSIZE
  real :: dummy(NSIZE)
  contains 
  procedure, pass(this) :: proc 

  end test
  type(test(NSIZE=10)) :: test_type

my proc subroutine is PURE. My proc returns say one value and does not have any side effects.

 pure subroutine proc(this, n) 
  implicit none 
 class(test(NSIZE=*)), intent(inout) :: this 
 integer, intent(inout) :: n
    n = n +1 
 end subroutine proc

Now inside another subroutine also declared as PURE I call proc

  pure subroutine test2 

    integer :: n 

     call  test_type% proc(n)
  end subroutine test2 

I get the error on the call test% proc(n) saying following:

error #7140: This global use associated object appears in a 'defining' context in a PURE procedure or in an internal procedure contained in a PURE procedure.

A self contained example

module mod1
   implicit none

      type test (size)
         integer, len :: size
         real :: dum(size)
      contains
         procedure, pass(this) :: dum_proc
      end type

      type(test(size=10)) :: test1

   contains

      pure subroutine dum_proc(this,  n )
         implicit none
         class(test(size=*)), intent(inout) :: this
         integer, intent(out) :: n
         n =n +2
      end subroutine dum_proc
end module mod1


program SUPPORT


implicit none
integer :: n

n = 0

call caller(n)


contains
   pure subroutine caller( nk )
   use mod1, only : test1
   implicit none

   integer, intent(inout) :: nk

   call test1% dum_proc(nk)

   end subroutine

end program SUPPORT`

Upvotes: 2

Views: 326

Answers (1)

francescalus
francescalus

Reputation: 32366

Your problem comes from the call

call test1% dum_proc(nk)

Because test1 in the pure subroutine caller has been use associated it is not allowed to be an actual argument corresponding to a dummy argument with the intent(inout) attribute. In the call to the type-bound procedure test1 is associated with the passed-object dummy argument this (with that intent).

Being associated with an intent(inout) dummy argument counts as a variable definition context, and it is this that "'defining' context" means in the error message. It isn't necessary for the argument actually to be changed for it to be in a defining context.

If, instead, you have test1 as an intent(inout) dummy argument this restriction does not apply. Having test1 host associated has the same restriction as if use associated.

Upvotes: 2

Related Questions