Class inheritance: Is this legal?

Cross-posting with r/fortran. I hope that this is OK.

I am trying to better understand OO fortran and came up with the following example:

module Vect_class
    type, abstract :: Vect_Type
    end type
end module Vect_class

module Elem_class
    use Vect_class
    type, abstract :: Elem_Type
        integer                      :: numDof
        class(Vect_type),allocatable :: v
    end type
end module Elem_class

module Vect
    use Vect_class
 
    type, extends(Vect_Type) :: Vect2D 
       real :: X,Y
    end type Vect2D   
 
    type, extends(Vect_Type) :: Vect3D 
       real :: X,Y,Z
    end type Vect3D   
end module Vect

module Elem
    use Elem_class

    type, extends(Elem_type)  :: elem2D
    end type
end module Elem
 
program testElem
    use Vect_class
    use Vect
    use Elem_class
    use Elem
 
    type(Vect2D) :: a
    type(Vect3D) :: b
    class(Vect_type),allocatable :: c,d
    class(Elem_type),allocatable :: e
    real       :: x,y,z
 
    x = 1.0
    y =-2.0
    z = 3.0
 
    a = Vect2D(x,y)
    b = Vect3D(x,y,z)
    c = Vect2D(x,y)
    d = Vect3D(x,y,z)
    e = Elem2D(10,b)
    write(*,*) e%numDof
end program testElem

It compiles and runs fine using intel 2022.1, but I get the following error message when using gfortran 11 (from RHEL devtoolset 11):

TestNestedClass2.f90:55:18:

   55 |     e = Elem2D(10,b)
      |                  1
Error: Cannot convert TYPE(vect3d) to CLASS(__class_vect_class_Vect_type_a) at (1)

Under macOS, I get a compiler internal error:

TestNestedClass2.f90:55:20:

   55 |     e = Elem2D(10,b)
      |                    1
internal compiler error: in fold_convert_loc, at fold-const.c:2563

Am I seeing a fortran bug / unimplemented fortran feature or is my code non-legal?

Gfortran 12.1 (compiled from source) on Ubuntu 20.04 shows, beside the *internal compiler error message, a long backtrace showing line numbers in several source files (of the compiler suite) and an invitation to submit a bug report.
So I guess it is gfortran problem rather than Fortran. And ifort dealing well with the snippet seems to confirm that.

This bug seems to be related to one I reported in 2011 (and still open).

2011, Yikes…

1 Like