https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128669

            Bug ID: 128669
           Summary: [OOP][F2003] Parent's procedure called instead of
                    NON_OVERRIDABLE override in a type with a FINAL
                    procedure
           Product: gcc
           Version: 16.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: markus.wallerberger at tuwien dot ac.at
  Target Milestone: ---

A derived type overrides a type-bound procedure of its parent and marks the
override NON_OVERRIDABLE.  If the derived type also has a FINAL procedure,
calling the procedure on a CLASS(parent) variable that holds the derived
type runs the parent's version instead of the override.  There is no error
or warning: the program silently calls the wrong procedure.

To reproduce the issue, both conditions have to be satisfied:

  1. the derived type is finalizable: it has a FINAL procedure, or a
     nonallocatable component whose type has a FINAL, and

  2. the override (overriding function in the derived type) is itself declared
     NON_OVERRIDABLE.

Without either, the program works.  Calls on a variable declared as the
derived type itself, TYPE(derived) or CLASS(derived), are not affected.

The result is the same at -O0 and -O2, and with -std=f2003, -std=f2018
and -pedantic; no diagnostics are issued at any warning level.


Workaround
----------

Drop NON_OVERRIDABLE from the overriding procedure.


Affected versions
-----------------

Anything I've tested from 7.5 onwards:  7.5.0, 8.4.0, 8.5.0, 9.4.0, 10.5.0,
11.4.0, 12.3.0, 13.3.0, 14.2.0, 15.2.0, 16.0.1 (r16-8246), 16.2.1


Related issue
-------------

PR 78443 fixed the same symptom for types without finalization:
add_proc_comp skips NON_OVERRIDABLE bindings unless tb->overridden is set.


Details
-------

A call on a CLASS variable goes through a table of procedure pointers that
gfortran emits for each type (the vtab).  In the table for derived, the
entry for name still points to the parent's procedure.  From
gfortran -O0 -S pr.f90, where the last entry is the one for name:

  __m_MOD___vtab_m_Derived:
          .long   41666283
          .zero   4
          .quad   0
          .quad   __m_MOD___vtab_m_Base
          .quad   __m_MOD___def_init_m_Derived
          .quad   __m_MOD___copy_m_Derived
          .quad   __m_MOD___final_m_Derived
          .quad   0
          .quad   __m_MOD_base_name       <-- should be __m_MOD_derived_name

With "procedure :: name => derived_name" (no NON_OVERRIDABLE) the slot holds
__m_MOD_derived_name, as expected.


Testcase I
----------

! { dg-do run }
!
! A NON_OVERRIDABLE binding that overrides a parent binding is called
! through the parent's procedure if the extended type is finalizable.

module m
  implicit none

  type :: base
  contains
    procedure :: name => base_name
  end type

  type, extends(base) :: derived
  contains
    procedure, non_overridable :: name => derived_name
    final :: derived_final
  end type

contains
  integer function base_name(this)
    class(base), intent(in) :: this
    base_name = 1
  end function

  integer function derived_name(this)
    class(derived), intent(in) :: this
    derived_name = 2
  end function

  subroutine derived_final(this)
    type(derived), intent(inout) :: this
  end subroutine
end module

program p
  use m
  implicit none
  class(base), allocatable :: x

  allocate(derived :: x)
  ! Checks that the overriding derived_name is called rather than base_name
  if (x%name() /= 2) stop 1
end program


Testcase II
-----------

! { dg-do run }
!
! A NON_OVERRIDABLE binding that overrides a parent binding is called
! through the parent's procedure if the extended type has a nonallocatable
! component of finalizable type.  An allocatable component does not trigger
! it.

module m
  implicit none

  type :: finalizable
    integer :: i = 0
  contains
    final :: finalizable_final
  end type

  type :: base
  contains
    procedure :: name => base_name
  end type

  type, extends(base) :: container
    type(finalizable) :: c
  contains
    procedure, non_overridable :: name => container_name
  end type

  type, extends(base) :: alloc_container
    type(finalizable), allocatable :: c
  contains
    procedure, non_overridable :: name => alloc_container_name
  end type

contains
  subroutine finalizable_final(this)
    type(finalizable), intent(inout) :: this
  end subroutine

  integer function base_name(this)
    class(base), intent(in) :: this
    base_name = 1
  end function

  integer function container_name(this)
    class(container), intent(in) :: this
    container_name = 2
  end function

  integer function alloc_container_name(this)
    class(alloc_container), intent(in) :: this
    alloc_container_name = 3
  end function
end module

program p
  use m
  implicit none
  class(base), allocatable :: x

  allocate(alloc_container :: x)
  ! Checks that the overriding alloc_container_name is called (works)
  if (x%name() /= 3) stop 2
  deallocate(x)

  allocate(container :: x)
  ! Checks that the overriding container_name is called rather than base_name
  if (x%name() /= 2) stop 1
end program


gfortran -v
-----------

Using built-in specs.
COLLECT_GCC=gfortran
COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-pc-linux-gnu/16/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: ../gcc/configure
--enable-languages=ada,c,c++,d,fortran,go,lto,m2,objc,obj-c++,rust,cobol
--enable-bootstrap --prefix=/usr --libdir=/usr/lib --libexecdir=/usr/lib
--mandir=/usr/share/man --infodir=/usr/share/info
--with-bugurl=https://gitlab.archlinux.org/archlinux/packaging/packages/gcc/-/issues
--with-build-config=bootstrap-lto --with-gcc-major-version-only
--with-linker-hash-style=gnu --with-system-zlib --enable-cet=auto
--enable-checking=release --enable-clocale=gnu --enable-default-pie
--enable-default-ssp --enable-gnu-indirect-function --enable-gnu-unique-object
--enable-libstdcxx-backtrace --enable-link-serialization=1
--enable-linker-build-id --enable-lto --enable-multilib --enable-plugin
--enable-shared --enable-threads=posix --disable-fixincludes --disable-libssp
--disable-libstdcxx-pch --disable-werror
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 16.2.1 20260810 (GCC)
  • [Bug fortran/128669] ... markus.wallerberger at tuwien dot ac.at via Gcc-bugs

Reply via email to