[Bug fortran/51634] [OOP] ICE with polymorphic operators

pault at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Mon Jan 2 13:28:00 GMT 2012


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=51634

Paul Thomas <pault at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |pault at gcc dot gnu.org

--- Comment #1 from Paul Thomas <pault at gcc dot gnu.org> 2012-01-02 13:28:13 UTC ---
Fixed on trunk as long as explicit allocations are inserted, as below.

I will raise a separate PR for the lack of automatic allocate on assign for
class objects with derived type components.

Thanks for the report.

Paul

module field_module
  implicit none
  private
  public :: field
  type :: field
    real, allocatable :: f(:)
  contains
    procedure :: multiply_real  => multiply
    procedure :: copy           => copy_field
    generic   :: operator(*)    => multiply_real
    generic   :: assignment(=)  => copy
  end type

contains
  subroutine copy_field (lhs, rhs)
    class(field), intent(inout) :: lhs
    class(field), intent(in) :: rhs
    if (allocated (lhs%f)) deallocate (lhs%f)
    allocate (lhs%f(size (rhs%f, 1)))
    lhs%f = rhs%f
  end subroutine

  function multiply(lhs,rhs)
    class(field) ,intent(in) :: lhs
    real ,intent(in)  :: rhs
    class(field) ,allocatable :: multiply
    integer :: i
    allocate(multiply, source = field([(0.0, i = 1, size (lhs%f, 1))]))
    multiply%f = lhs%f * rhs
  end function
end module field_module

program main
  use field_module
  implicit none
  type(field) :: f, g
  real :: dt, half
  allocate (g%f(2), source = [1.0, 2.0])
  dt = 7
  half = 0.5
  f = g * dt * half
  print *, f%f
end program main



More information about the Gcc-bugs mailing list