User account creation filtered due to spam.

Bug 51634 - [OOP] ICE with polymorphic operators
Summary: [OOP] ICE with polymorphic operators
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.7.0
: P3 normal
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: ice-on-valid-code
Depends on: 46328 51334 51733
Blocks:
  Show dependency treegraph
 
Reported: 2011-12-20 08:21 UTC by Tobias Burnus
Modified: 2012-01-19 08:13 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Tobias Burnus 2011-12-20 08:21:05 UTC
Related to PR 51334 and PR 46328, though the error message is slightly different.

foo.f90:37:0: internal compiler error: tree check: expected function_type or method_type, have pointer_type in gimplify_call_expr, at gimplify.c:2430


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
    lhs%f = rhs%f
  end subroutine

  function multiply(lhs,rhs)
    class(field) ,intent(in) :: lhs
    real ,intent(in)  :: rhs
    class(field) ,allocatable :: multiply
    allocate(multiply)
    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
  dt = 7
  half = 0.5
  f = g * dt * half
end program main
Comment 1 Paul Thomas 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
Comment 2 Tobias Burnus 2012-01-13 22:17:53 UTC
(In reply to comment #1)
> 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.

The test case now works since the commit of PR 48351 (and thus I closed PR 51733). The following allocates of comment 1 are still needed, though - otherwise the code is invalid:
 function multiply(lhs,rhs)
   allocate(multiply)
 program main
   allocate (g%f(2), source = [1.0, 2.0])

 * * *

Unfortunately, the original program still fails. New test case; produces with ifort 12.1:
   6.000000       12.00000       18.00000    
   24.00000       30.00000       36.00000    

Fails with gfortran at:
  Invalid free() / delete / delete[] / realloc()
    at 0x4C28C3E: free (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
    by 0x401BD0: MAIN__ (test.f90:34)

The "free" issue only occurs for:
  fireworks = fireworks + fireworks * dt
It works with
  fireworks = fireworks + fireworks
or with
  fireworks = fireworks * dt


I am not sure why it fails, but the following looks a bit odd:

        struct soop_stars D.1952;
        class.8._data = (struct soop_stars *) &D.1952;
        ...
        if (class.8.position.data != 0B)
          __builtin_free ((void *) class.8.position.data);

Shouldn't that be: "class.8._data.position.data" (not the "_data")?


module soop_stars_class
  implicit none
  type soop_stars
    real, dimension(:), allocatable :: position,velocity
  contains
    procedure :: total
    procedure :: product
    generic :: operator(+) => total
    generic :: operator(*) => product
  end type
contains
  type(soop_stars) function product(lhs,rhs)
    class(soop_stars) ,intent(in) :: lhs
    real ,intent(in) :: rhs
    product%position = lhs%position*rhs
    product%velocity = lhs%velocity*rhs
  end function

  type(soop_stars) function total(lhs,rhs)
    class(soop_stars) ,intent(in) :: lhs,rhs
    total%position = lhs%position + rhs%position
    total%velocity = lhs%velocity + rhs%velocity
  end function
end module

program main
  use soop_stars_class ,only : soop_stars
  implicit none
  type(soop_stars) :: fireworks
  real :: dt
  fireworks%position = [1,2,3]
  fireworks%velocity = [4,5,6]
  dt = 5
  fireworks = fireworks + fireworks*dt
  print *, fireworks%position
  print *, fireworks%velocity
end program
Comment 3 Paul Thomas 2012-01-18 20:52:51 UTC
Author: pault
Date: Wed Jan 18 20:52:48 2012
New Revision: 183287

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183287
Log:
2012-01-18  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/51634
	* trans-expr.c (gfc_conv_procedure_call): Deallocate allocatable
	components of temporary class arguments.

2012-01-18  Paul Thomas  <pault@gcc.gnu.org>

	PR fortran/51634
	* gfortran.dg/typebound_operator_12.f03: New.
	* gfortran.dg/typebound_operator_13.f03: New.

Added:
    trunk/gcc/testsuite/gfortran.dg/typebound_operator_12.f03
    trunk/gcc/testsuite/gfortran.dg/typebound_operator_13.f03
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog
Comment 4 Tobias Burnus 2012-01-19 08:13:13 UTC
FIXED on the trunk (4.7).

Thanks for the patch, Paul!