Ambiguity in Fortran 2018?

  n = 10
  call foo
 contains
  subroutine foo
    real :: a(n)
    common /blk/ n
    print *, size(a)
  end subroutine
end

block data
  common /blk/ n
  data n/20/
end

I have two Fortran compilers that print 10 and three that print 20. The specific text in the Fortran 2018 standard allows for both host-associated objects in specification expressions (10.1.11) as well as for forward-referenced implicitly-typed objects in COMMON. Is this case truly ambiguous, or is there text that I missed that resolves the problem?

1 Like

I’m curious as well. Which compilers did you try? Results from my end:

  • gfortran 4.8.5, 8.3.0, 9.2.0: 10
  • ifort 2021.1 Beta: 20
  • xlf 16.1.1: 20

For whatever it’s worth, I too think it’s section 19.5.1.4 on Host Association that governs the outcome, specifically paragraph 2 as indicated by

17 A name
18 that appears in the scoping unit as

27 (7) a variable-name in a common-block-object in a common-stmt,

38 is a local identifier in the scoping unit and any entity of the host that has this as its nongeneric name is inaccessible
39 by that name by host association.

Going by this verbiage, it makes sense the program outputs 20.

1 Like

NAG 8.0.0.3(7024) also prints 20.

Do either of these variants remove the disagreements among compilers?

cat test1.f90
n = 10
call foo
contains
subroutine foo
common /blk/ n
real :: a(n)
print *, size(a)
end subroutine
end

block data
common /blk/ n
data n/20/
end

cat test2.f90
n = 10
call foo
contains
subroutine foo
integer :: n
real :: a(n)
common /blk/ n
print *, size(a)
end subroutine
end

block data
common /blk/ n
data n/20/
end

1 Like