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

Error in overloading constructors of parametric types.

Trent_Hawkins
Novice
899 Views

When compiling with ifort 2017 update 1:

#     ifndef matrix_algebra_F90
#     define matrix_algebra_F90

      module matrix_algebra

            implicit none

            integer,parameter::x_=8
            type::vector(size)
               integer,len::size
               complex(kind=x_)::component(0:size)
            contains
               procedure,private::initialize_vector
        end type  vector

            interface vector
               module procedure vector_constructor
        end interface vector

      contains

            subroutine initialize_vector(this,source)

                  implicit none

                  class(vector(*))::this
                  complex(kind=x_),intent(in),optional::source(0:this%size)

                  if(present(source)) then
                     this%component=source
                  else
                     this%component=(0.0_x_,0.0_x_)
              end if!present(source)

        end subroutine initialize_vector!this,source

            function vector_constructor(size,source) result(that)

                  implicit none

                  integer,intent(in)::size
                  complex(kind=x_),intent(in),optional::source(0:size)
                  type(vector(size))::that

                  if(present(source)) then
                     call that%initialize_vector(source)
                  else
                     call that%initialize_vector()
              end if!present(source)

        end function vector_constructor!size,source

            function test_constructor(vector0) result(vector_)

                  implicit none

                  type(vector(*)),intent(in)::vector0
                  type(vector(vector0%size))::vector_

                  vector_=vector(vector0%size,(0.0_x_,0.0_x_))

        end function test_constructor

  end module matrix_algebra

#     endif

I get the following error:

matrix/snippet.F90(97): error #8725: If a type parameter does not have a default value, there must be a type parameter spec corresponding to that type parameter.   [VECTOR]
                  vector_=vector(vector0%size,(0.0_x_,0.0_x_))
^
matrix/snippet.F90(97): error #6593: The number of expressions in a structure constructor differs from the number of components of the derived type.   [VECTOR]
                  vector_=vector(vector0%size,(0.0_x_,0.0_x_))
--------------------------^

This disallows me from defining custom constructors, something I was aware of being able to do. I suspect that the class name is already bound by the compiler as a synthesized constructor that simply is called to at least initialize all members with no in-class initializers (default values). In that case, the compiler prevents me from creating additional constructors to the default ones (overloading the constructor). Could you please tell me what detail I am missing here? Thank you in advance for your time.

EDIT: When using constructors in the aforementioned manner in more complex code with more types and constructors, it generates a internal compiler error, but it is certain that the constructors are the culprits, as removing all constructor calls resolves the internal error. In an attempt to make the above snippet to reproduce the problem I get a normal compiler error instead, but I think it is related still.

3 Replies
FortranFan
Honored Contributor II
899 Views

Here's a modified version of code in the original post that addresses the 2 issues Intel Fortran compiler didn't like but it now generates an internal compiler error, at least with the Windows version.  Perhaps someone can Intel can follow up on the ICE.

module matrix_algebra

   implicit none

   integer, parameter :: x_ = 8

   type :: vector(size)
      integer, len :: size = 1  !.. Introduce a default length parameter
      complex(kind=x_) :: component(0:size)
   contains
      procedure, private :: initialize_vector
   end type  vector

   interface vector
      module procedure vector_constructor
   end interface vector

contains

   subroutine initialize_vector(this, source)

      class(vector(size=*))                  :: this
      complex(kind=x_), intent(in), optional :: source(0:this%size)

      if ( present(source) ) then
         this%component = source
      else
         this%component = (0.0_x_,0.0_x_)
      end if ! present(source)

   end subroutine initialize_vector ! this, source

   function vector_constructor( size, source ) result( that )

      integer, intent(in)                    :: size
      complex(kind=x_), intent(in), optional :: source(0:size)
      type(vector(size)) :: that

      if ( present(source) ) then
         call that%initialize_vector( source )
      else
         call that%initialize_vector()
      end if ! present(source)

   end function vector_constructor ! size, source

   function test_constructor( vector0 ) result( vector_ )

      type(vector(size=*)), intent(in) :: vector0
      type(vector(vector0%size)) :: vector_

      vector_ = vector( vector0%size, source=[ (0.0_x_,0.0_x_) ] )  !.. Introduce a rank one array as 2nd argument

   end function test_constructor

end module matrix_algebra
Compiling with Intel(R) Visual Fortran Compiler 17.0.1.143 [IA-32]...
m.f90
fortcom: Fatal: There has been an internal compiler error (C0000005).
ifort: error #10298: problem during post processing of parallel object compilation
compilation aborted for m.f90 (code 1)

 

Kevin_D_Intel
Employee
899 Views

Thank you FortranFan. I'll follow-up and report to Development.

(Internal tracking id: DPD200416899)

0 Kudos
Trent_Hawkins
Novice
899 Views

This is the internal compiler error I was trying to reproduce, thank you FortranFan. In my attempt to shorten the snippet, I forgot to make the proper constructor call, which would result in the internal compiler error I am also getting on Linux. Here is what I got with the fixed code (for completeness) on 2017v1 Linux version:

/tmp/ifortCXsmaT.i90: catastrophic error: **Internal compiler error: segmentation violation signal raised**
Please report this error along with the circumstances in which it occurred in a Software Problem Report. 
Note: File and line given may not be explicit cause of this error.
compilation aborted for snippet.F90 (code 1)

 

0 Kudos
Reply