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

Procedure inheritance rules in Fortran 2003/2008 for class(*)

antony
Beginner
4,550 Views

The code below compiles in ifort. Accoring to at least one interpretation on the Stackoverflow question it is invalid, but ifort does not produce F2008/F2003 compliance warnings. Is intel's opion that this is correct, that it is an extension, or is it a bug? (if it is deliberate it is useful, but would also be nice to be able to get a warning as it doesn't work in gfortran, and know what the exact rule being applied is).

module classes
Type AData
end Type

Type A
contains
procedure :: Work
end type

Type, extends(AData) :: BData
end Type

Type, extends(A) :: B
contains
procedure :: Work => Work2
end type

contains

subroutine Work(this, D)
class(A) :: this
class(*) :: D
end subroutine

subroutine Work2(this, D)
class(B) :: this
class(BData) :: D
end subroutine

end module classes
0 Kudos
23 Replies
antony
Beginner
735 Views

Also the reverse of the first case is rejected by gfortran, eg

 

    module classes
    implicit none

    Type AData
    end Type

    Type, extends(AData) :: BData
    end Type

    Type A
    contains
    procedure :: Work
    end type


    Type, extends(A) :: B
    contains
    procedure :: Work => Work2
    end type

    contains

    subroutine Work(this, D)
    class(A) :: this
    Type(AData), intent(in)  :: D

    end subroutine

    subroutine Work2(this, D)
    class(B) :: this
    Type(AData) :: D

    end subroutine

    end module classes

 

0 Kudos
FortranFan
Honored Contributor III
735 Views

antony wrote:

The code below compiles in ifort.

 ... would also be nice to be able to get a warning as it doesn't work in gfortran, ..

[fortran]

module classes
Type AData
end Type

Type A
contains
procedure :: Work
end type

Type, extends(AData) :: BData
end Type

Type, extends(A) :: B
contains
procedure :: Work => Work2
end type

contains

subroutine Work(this, D)
class(A) :: this
class(*) :: D
end subroutine

subroutine Work2(this, D)
class(B) :: this
class(BData) :: D
end subroutine

end module classes

[/fortran]

I am curious about recent developments with gfortran, so finally got around to downloading and setting it up for Windows.  The latest, convenient version I could find was MinGW 4.8.1 build.

NOTE: the above code builds without any errors/warnings in gfortran, MinGW 4.8.1 build:

[plain]

mingw32-gfortran.exe -JDebug\\GNU\\ -Wall -g -std=f2008 -Wrealloc-lhs-all -Wrealloc-lhs

-Wimplicit-interface -Wconversion -Warray-temporaries -Wall

-c C:\\dev\\Fortran\\TypeExtension\\sor\\ProcOverride.f90

-o Debug\\GNU\\sor\\ProcOverride.o

Process terminated with status 0 (0 minute(s), 0 second(s))

0 error(s), 0 warning(s) (0 minute(s), 0 second(s))

[/plain]

So perhaps a newer gfortran version (4.9+) has been improved on this procedure overriding aspect, but I can't find the newer version for Windows.  4.8.1 version also doesn't complain if the dummy argument in the overriding procedure uses an extended type.   However 4.8.1 version does catch mismatches in the dummy argument INTENT.

My (obvious) take-away:

  1. Fortran 2008 (and some 2003) implementations are still "in the works" in most compilers,
  2. Coders need to think through and use new features with appropriate caution,
  3. Cross-checking using different compilers can help, especially with the newer features,
  4. Test, test, test.. report issues so everyone can benefit

Thanks to Antony for this bringing this up,

0 Kudos
Steven_L_Intel1
Employee
735 Views

We have fixed the issues discussed in this thread - I expect the fixes to appear in the 15.0 release later this year.

0 Kudos
Reply