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

             Bug #: 53255
           Summary: [OOP] With TYPE, wrong type-bound operator used: of
                    parent instead of overridden one
    Classification: Unclassified
           Product: gcc
           Version: 4.8.0
            Status: UNCONFIRMED
          Keywords: wrong-code
          Severity: normal
          Priority: P3
         Component: fortran
        AssignedTo: unassig...@gcc.gnu.org
        ReportedBy: bur...@gcc.gnu.org


Reported by Reinhold Bader.

Expected: The following output:
 executing base
 executing override
 OK

De-facto output:
 executing base
 executing base
 FAIL


If one changes:
  type(ext) :: p
to
  class(ext), allocatable :: p
  allocate(p)
or
  class(base), allocatable :: p
  allocate(ext :: p)
it works.

Thus, it seems as if only the compile-time resolution of TYPE has the problem,
while the vtable run-time version is correct.



module mod_base
  implicit none
  private
  type, public :: base
     private
     real :: r(2,2) = reshape( (/ 1.0, 2.0, 3.0, 4.0 /), (/ 2, 2 /))
   contains
     procedure, private :: trace
     generic :: operator(.tr.) => trace
  end type base
contains
  complex function trace(this)
    class(base), intent(in) :: this
    trace = this%r(1,1) + this%r(2,2)
  end function trace
end module mod_base
module mod_ext
  use mod_base
  implicit none
  private
  public :: base
  type, public, extends(base) :: ext
     private
     real :: i(2,2) = reshape( (/ 1.0, 1.0, 1.0, 1.5 /), (/ 2, 2 /))
   contains
     procedure, private :: trace => trace_ext
  end type ext
contains
   complex function trace_ext(this)
    class(ext), intent(in) :: this

!   the following should be executed through invoking .tr. p below
    write(*,*) 'executing override'
    trace_ext = .tr. this%base + (0.0, 1.0) * ( this%i(1,1) + this%i(2,2) )
  end function trace_ext

end module mod_ext
program test_override
  use mod_ext
  implicit none
  type(base) :: o
  type(ext) :: p
!  write(*,*) .tr. o
!  write(*,*) .tr. p

  if (abs(.tr. o - 5.0 ) < 1.0e-6  .and. abs( .tr. p - (5.0,2.5)) < 1.0e-6)
then
     write(*,*) 'OK'
  else
     write(*,*) 'FAIL'
  end if
end program test_override

Reply via email to