User account creation filtered due to spam.

Bug 53255 - [OOP] With TYPE, wrong type-bound operator used: of parent instead of overridden one
Summary: [OOP] With TYPE, wrong type-bound operator used: of parent instead of overrid...
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: fortran (show other bugs)
Version: 4.8.0
: P3 normal
Target Milestone: 4.7.1
Assignee: Not yet assigned to anyone
URL:
Keywords: wrong-code
Depends on:
Blocks:
 
Reported: 2012-05-06 08:09 UTC by Tobias Burnus
Modified: 2016-11-16 14:01 UTC (History)
3 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 2012-05-06 08:09:26 UTC
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
Comment 1 Tobias Burnus 2012-05-06 08:54:29 UTC
What happens is the call to resolve_compcall, which in turn calls resolve_typebound_generic_call. That procedure fails as e->value.compcall.tbp->is_generic == false. I think that's okay as only .tr. is generic, the "trace" is not.

However, somewhere in resolve_compcall or resolve_typebound_static it should search for the overridden procedure.

The following seems to work. Janus: Do you think that the patch makes sense?

--- a/gcc/fortran/resolve.c
+++ b/gcc/fortran/resolve.c
@@ -5671,9 +5702,8 @@ resolve_typebound_static (gfc_expr* e, gfc_symtree** target,
   e->value.compcall.actual = NULL;
 
   /* If we find a deferred typebound procedure, check for derived types
-     that an over-riding typebound procedure has not been missed.  */
-  if (e->value.compcall.tbp->deferred
-       && e->value.compcall.name
+     that an overriding typebound procedure has not been missed.  */
+  if (e->value.compcall.name
        && !e->value.compcall.tbp->non_overridable
        && e->value.compcall.base_object
        && e->value.compcall.base_object->ts.type == BT_DERIVED)
Comment 2 Tobias Burnus 2012-05-07 08:35:21 UTC
Author: burnus
Date: Mon May  7 08:35:17 2012
New Revision: 187226

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

        PR fortran/53255
        * resolve.c (resolve_typebound_static): Fix handling
        of overridden specific to generic operator.

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

        PR fortran/53255
        * gfortran.dg/typebound_operator_15.f90: New.


Added:
    trunk/gcc/testsuite/gfortran.dg/typebound_operator_15.f90
Modified:
    trunk/gcc/fortran/ChangeLog
    trunk/gcc/fortran/resolve.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 Tobias Burnus 2012-05-07 11:50:12 UTC
Author: burnus
Date: Mon May  7 11:50:04 2012
New Revision: 187232

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

        Backport from mainline:
        2012-05-07  Tobias Burnus  <burnus@net-b.de>

        PR fortran/53255
        * resolve.c (resolve_typebound_static): Fix handling
        of overridden specific to generic operator.

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

        Backport from mainline:
        2012-05-07  Tobias Burnus  <burnus@net-b.de>

        PR fortran/53255
        * gfortran.dg/typebound_operator_15.f90: New.


Added:
    branches/gcc-4_7-branch/gcc/testsuite/gfortran.dg/typebound_operator_15.f90
Modified:
    branches/gcc-4_7-branch/gcc/fortran/ChangeLog
    branches/gcc-4_7-branch/gcc/fortran/resolve.c
    branches/gcc-4_7-branch/gcc/testsuite/ChangeLog
Comment 4 Tobias Burnus 2012-05-07 13:33:08 UTC
FIXED on the trunk (4.8) and on the 4.7 branch.

4.5 and 4.6 are also affected, but already for 4.6 the code is too different for a simple back porting. As the OOP support in 4.7/4.8 is much better, 4.5/4.6 shouldn't be used for polymorphism application at all.