Silverfrost Forums

Welcome to our forums

generic interface

27 May 2020 2:24 #25518

I reported the following code to NAG as a suspected error, since their compiler was complaining about the two subroutines s1 and s2 being indistinguishable.

Module m
 Interface s
   Module Procedure s1
   Module Procedure s2
 End Interface s
!
Contains
!
 Subroutine s1 (i, c)
   Integer, Intent(In) :: i
   Character(Len=*), Intent(In) :: c
   Return
 End Subroutine s1
!
 Subroutine s2 (c, i)
   Character(Len=*), Intent(In) :: c
   Integer, Intent(In) :: i
   Return
 End Subroutine s2
End Module m

The code compiles without an error in FTN95. However, NAG indicated that the code is incorrect because

Call s (i=i, c=c)

would be ambiguous since the ordering of the arguments does not matter even though the arguments are not optional. So perhaps FTN95 should issue a compilation error message?

28 May 2020 7:10 #25521

Thank you for the feedback. You are right. FTN95 does not fault this even when using /ISO.

8 Jun 2020 2:58 #25591

Here is a fleshed out version of the test code. Since the ambiguity is not detected and the violation of the standard is not detected, the fact that the program runs is, in itself, the error. If the program output is seen to be reasonable, the bug can go undetected for years.

Module m
 Interface s
   Module Procedure s1
   Module Procedure s2
 End Interface s
!
Contains
!
 Subroutine s1 (i, c)
   Integer, Intent(In) :: i
   Character(Len=*), Intent(In) :: c
   print *,'S1: ',c,i
   Return
 End Subroutine s1
!
 Subroutine s2 (c, i)
   Character(Len=*), Intent(In) :: c
   Integer, Intent(In) :: i
   print *,'S2: ',c,i
   Return
 End Subroutine s2
End Module m

Program Smn
use m
character(len=5) :: cc = 'ABCDE'
integer :: i = 123
! when args are passed without keywords, the order/type suffices to select the specific routine
call s(i,cc)
call s(cc,i)
! when keywords are used, both s1 and s2 would match; if s1 is tried first, it will match
! and the bug is not found unless s2 is also tested even after s1 succeeded.
call s(c=cc, i=i)
call s(i=i, c=cc)
end Program
4 Jul 2020 7:47 #25894

This issue has now been fixed for the next release of FTN95. This kind of ambiguous interface will now be reported as an error.

Please login to reply.