I’ve asked the OP’s question almost verbatim to ChatGPT (Plus), and it seems to support the interpretation by gfortran, flang, and Lfortran (even when use, only: foo is used). It suggests that “Annex A” was useful for interpretation, so the latter section might contain some more info… (But please keep in mind that answers from LLMs may be wrong, although it seems to have used the Standards PDF in the OP’s post for analyses.)
(Below is some of the replies. I’ve asked also about Annex A, but the answer was not very clear about the location of the original texts.)
The standard-conforming reading is:
your program is allowed, because foo is not actually used in the program body. So in this case, the behavior of gfortran, flang, and LFortran matches the wording of the Fortran 2023 draft more closely than Intel’s rejection does.
The key point is the sentence you quoted from 14.2.2 p8:
an accessed entity may be associated with two or more ultimate entities unless its identifier is not used.
That “used” means used as a name in the scoping unit for semantic purposes—for example in a reference like
call foo()
x = foo(...)
print *, foo
It does not mean “mentioned syntactically inside a USE, ONLY: list”. The ONLY list controls which names become accessible; it is not itself a reference to the entity in the sense relevant to the ambiguity rule. This reading is reinforced by the informative Annex text, which explicitly says that if two modules contain entities with the same name, and none of those entities are to be used, they can simply be ignored.
So your example
program main
use A, only: foo
use B, only: foo
implicit none
end program main
is valid under that reading, because:
foofromAandfoofromBare both accessible,- but the identifier
foois never actually referenced inmain, - therefore the ambiguity never becomes semantically relevant.
By contrast, this version is clearly ambiguous and should be rejected:
program main
use A
use B
implicit none
call foo()
end program main
and current compilers generally do reject that.
So to your direct question:
No, use A, only: foo should not itself count as “using the identifier foo” in the sense of 14.2.2 p8. The ONLYclause makes foo accessible; it does not by itself constitute a semantic reference that triggers the ambiguity prohibition.
EDIT: Here is the link for the full output of ChatGPT (please note, however, that it is very long because I made several subsequent questions like the wording in the Standards and Annex A.)