Follow up to PR 43388 (ALLOCATE with MOLD, a F2008 feature), but it also
applies to polymorphic variables with INTENT(OUT) (i.e. to F2003).

In either cases, a potentially existing default initializer of the *effective
type* needs to be applied. That means that this data has to be available in the
vtable.

See also: http://j3-fortran.org/pipermail/j3/2010-June/003621.html

(Thanks to Janus for spotting the problem (for MOLD) at the first place; thanks
to Bill Long for confirmation and mentioning INTENT(OUT); and to Aleksandar
Donev for the comments.)

Testing shows that other vendors have also overlooked these cases.
(For intent out, there seem to be compiler which do not set the variable at all
(e.g. gfortran) or those which only use the base type for the initialization;
similarly, for MOLD where unspecified initialization happens (via malloc),
possibly with base-type initialization on top.)

---------- INTENT(OUT) test case -----------------
! Expected: a= 1  b= 3
  implicit none
  type t
    integer :: a = 1
  end type t
  type, extends(t) :: t2
    integer :: b = 3
  end type t2

  type(t2) :: y
  y%a = 44
  y%b = 55
  call intent_out (y)
  print *, 'a=', y%a, ' b=', y%b
contains
  subroutine intent_out(x)
    class(t), intent(out) :: x
    select type (x)
      type is (t2)
      print *, 'a=', x%a, ' b=', x%b
    end select
  end subroutine
end

---------- MOLD test case ------------------------
! Expected:
!  a= 1  b= 3
! (Wrong is "a= 1  b= 0" or "a= 0  b= 0" or garbage)
implicit none
type t
  integer :: a = 1
end type t

type, extends(t) :: t2
  integer :: b = 3
end type t2

class(t), allocatable :: x, y

allocate (t2 :: y)
select type (y)
  type is (t2)
    y%a = 44
    y%b = 55
end select

allocate ( x, mold=y)
select type (x)
  type is (t2)
   print *, 'a=', x%a, ' b=', x%b
end select
end


-- 
           Summary: [OOP] wrong code for polymorphic variable with
                    INTENT(OUT)/Alloc w/ MOLD
           Product: gcc
           Version: 4.6.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: burnus at gcc dot gnu dot org
 BugsThisDependsOn: 43388


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

Reply via email to