Bug 51514

Summary: [OOP] Wrong code when passing a scalar CLASS to a TYPE
Product: gcc Reporter: Tobias Burnus <burnus>
Component: fortranAssignee: Not yet assigned to anyone <unassigned>
Status: RESOLVED FIXED    
Severity: normal CC: burnus
Priority: P3 Keywords: wrong-code
Version: 4.7.0   
Target Milestone: ---   
Host: Target:
Build: Known to work:
Known to fail: Last reconfirmed:
Attachments: Completely untested/not compiled draft patch

Description Tobias Burnus 2011-12-12 14:50:50 UTC
The following program (subprog_poly_nonpoly_01_pos.f90) of Reinhold Bader's test suite fails with at run time with:

==17368== Invalid read of size 4
==17368==    at 0x400AB4: MAIN__ (subprog_poly_nonpoly_01_pos.f90:21)
==17368==    by 0x400BCD: main (subprog_poly_nonpoly_01_pos.f90:16)

That's the line:
  if (xx%i == 3) then

The problem is that one passes the CLASS and not the TYPE to the subroutine:
      subpr (&xx);
      if (xx._data->i == 3)
The first line should have been  subpr(&xx._data)

It works with the Intel Compiler 12.1. (See also PR 46990.)


module mod_subpr
  implicit none
  type :: foo
    integer :: i = 2
  end type
  type, extends(foo) :: foo_1
    real :: r(2)
  end type
contains
  subroutine subpr(x)
    type(foo) :: x
    x%i = 3
  end subroutine
end module
program prog
  use mod_subpr
  implicit none
  class(foo), allocatable :: xx
  allocate(foo_1 :: xx)
  call subpr(xx)
  if (xx%i == 3) then
     write(*,*) 'OK'
  else
     write(*,*) 'FAIL'
  end if
end program
Comment 1 Tobias Burnus 2012-02-03 14:24:29 UTC
Ensure that passing CLASS to TYPE also works for polymorphic arrays - and with ELEMENTAL procedures, cf. also PR 51514.
Comment 2 Dominique d'Humieres 2012-02-03 14:28:56 UTC
> cf. also PR 51514.

? wrong pr?
Comment 3 Tobias Burnus 2012-02-03 15:09:16 UTC
(In reply to comment #2)
> > cf. also PR 51514.
> ? wrong pr?

No, why should it be wrong? - Clearly this PR is related to itself...

(I meant PR 50981 - and in particular the summary at PR 50981 comment 36.)
Comment 4 Tobias Burnus 2012-02-06 11:04:25 UTC
Actually, the issue seems to only affect scalar polymorphic variables.

If the actual argument is a polymorphic array, it works. Both as argument to an elemental function and to an array dummy argument.
Comment 5 Tobias Burnus 2012-02-06 14:04:29 UTC
Created attachment 26583 [details]
Completely untested/not compiled draft patch

The attached patch is completely untested. It additionally contains some - also untested - fixes for polymophic coarrays.

Variant:

  subroutine subpr2(x)
    type(foo) :: x
    print *,x%i
    if (x%i /= 55) call abort ()
  end subroutine
  function f()
    class(foo), allocatable :: f
    allocate (f)
    f%i = 55
  end function f
with
  call subpr(f())
Comment 6 Tobias Burnus 2012-02-07 08:15:28 UTC
Author: burnus
Date: Tue Feb  7 08:15:14 2012
New Revision: 183954

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=183954
Log:
2012-02-07  Tobias Burnus  <burnus@net-b.de>

        PR fortran/51514
        * trans-expr.c (gfc_conv_procedure_call): Add _data component
        for calls of scalar CLASS actuals to TYPE dummies.

2012-02-07  Tobias Burnus  <burnus@net-b.de>

        PR fortran/51514
        * gfortran.dg/class_to_type_2.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/class_to_type_2.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/trans-expr.c
    trunk/gcc/testsuite/ChangeLog
Comment 7 Tobias Burnus 2012-02-07 08:16:24 UTC
FIXED on the trunk (4.7).