[Flang] Spurious error for COMPILER_VERSION() reference in context where disambiguation is possible
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Flang incorrectly rejects valid Fortran code by reporting ambiguity when referencing COMPILER_VERSION from the intrinsic module ISO_FORTRAN_ENV, even in contexts where the correct procedure can be unambiguously determined. It incorrectly applies the restriction that local identifiers of the same class cannot have the same name, without recognizing the exception for generic names. The compiler should be able to disambiguate based on the number and type of arguments.
Expected Behavior: COMPILER_VERSION and COMPILER_OPTIONS are transformational functions from ISO_FORTRAN_ENV that are specified as generic (Fortran standard 16.10.2.1p2). According to the standard (19.3.1p3), a generic name may be the same as the name of a procedure. The compiler should recognize that there is no ambiguity when the procedure is referenced with no dummy argument or one dummy argument, and should resolve to the correct specific procedure. Additionally, COMPILER_VERSION is explicitly specified as a "specification inquiry" and should be allowed in constant expressions.
Actual Behavior: Flang produces the following error:
```
error: Reference to 'compiler_version' is ambiguous
character(*),parameter :: buf = compiler_version()
error: Reference to 'compiler_version' is ambiguous
print *, compiler_version(.true.), " or ",compiler_version(.false.) ! unambiguous
^^^^^^^^^^^^^^^^
error: Reference to 'compiler_version' is ambiguous
print *, compiler_version(.true.), " or ",compiler_version(.false.) ! unambiguous
^^^^^^^^^^^^^^^^
```
Reproducer:
```fortran
module user
! generic interface for COMPILER_VERSION
interface compiler_version
module procedure okp
end interface
contains
! specific has one dummy
function okp(f)
character(:),allocatable :: okp
logical,intent(in) :: f ! non-optional, can't be confused
okp = Merge('user:ok ','user:fail',f)
end function
end module
program test
! bring in generic COMPILER_VERSION from ISO_FORTRAN_ENV
use iso_fortran_env, only: compiler_version
! bring in generic COMPILER_VERSION from USER, disambiguating check happens here
! Class 1 local identifiers, can be the same if generic, which they are
use user
! only the intrinsic-module one can be in a constant expression, because
! COMPILER_VERSION() reference is explicitly specified to be a "specification inquiry"
character(*),parameter :: buf = compiler_version() ! unambiguous
print *, buf
print *, compiler_version(.true.), " or ",compiler_version(.false.) ! unambiguous
end program
```
Contributor guide
Research direction
Start by running the supplied Fortran reproducer with Flang and confirm the ambiguity diagnostics for COMPILER_VERSION. Trace generic-name resolution for the ISO_FORTRAN_ENV and user-defined interfaces, including the zero- and one-argument calls and constant expression, and finish when valid calls resolve without errors while genuinely ambiguous cases remain diagnosed.
Written by the indexing model from the issue text.
Assessment
- Domain
- compilers
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100