Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29487 Discussions

Generic procedures: non-ambiguous at declaration or use?

antony
Beginner
1,262 Views

Consider this

 


    module generics

    Type T
    contains
    procedure :: A
    procedure :: B
    generic :: C=>A, B
    end type T
    contains

    subroutine A(X,Y,Z)
    class(T) X
    real Y
    real, optional :: Z
    end subroutine

    subroutine B(X,Y,Z)
    class(T) X
    class(*), optional :: Y, Z
    end subroutine

    end module generics 

The two subroutines are ambiguous (e.g. for C%(x,1.,2.)). But ifort accepts this when you compile the module, rejecting it only  if you try to use it in any way that is ambiugous. gfortran rejects it as soon as you compile the module. So when is non-ambiguity required? The standard doesn't seem to be very obviously clear on this.

For libraries it would be useful to have at least some compile-time warning that there may be ambiguous call cases.

0 Kudos
5 Replies
Steven_L_Intel1
Employee
1,262 Views

The standard makes no mention of "when" - it simply says you are not allowed to have an ambiguous generic. But I agree it would be nice if the compiler could alert you as soon as if the problem is seen. I'll suggest that to the developers.

There is a case where it's not possible to know until use - if modules mod1 and mod2 each add a routine to the same generic, either one by itself is fine but if a program then uses both modules, it's still not an issue unless the generic is used.

0 Kudos
Craig_Dedo
New Contributor I
1,262 Views

Strictly speaking, the Fortran standard only requires Fortran compilers to report violations of the numbered syntax rules and associated constraints.  However, Fortran compilers may report additional violations of the rules that are in the normative text but not in the numbered syntax rules and constraints.

0 Kudos
antony
Beginner
1,262 Views

Thanks.

0 Kudos
IanH
Honored Contributor III
1,262 Views

Doesn't this come down to what the scope of the generic identifier is?  Isn't it in scope inside the type definition?  Doesn't C1215 (in F2008) then require a diagnostic, even if the generic name isn't then used?

Isn't the "doesn't matter unless it is used" a separate aspect around conflicting "identifiers", rather than generic disambiguation?  Is a writing style that involves a whole heap of semi-rhetorical questions really annoying?

I'm not sure, but I'd guess "yes" to all the above.

0 Kudos
Steven_L_Intel1
Employee
1,262 Views

To that last question, definitely yes.

I'd have to think a bit more on the scope issue, but I now agree with you that C1215 requires the diagnostic when the ambiguity is visible. The matter is perhaps a bit different (and more restricted) when one is considering generic TBPs, but I think you could get into this situation with extending a type - not 100% sure of that.

0 Kudos
Reply