Overriding private function in different module

Viewed 111

If a type-bound procedure which is private in a module foo is overridden (or attempted to be overridden) by a type in a second module bar, how is this resolved? Is there any specification in the standard? Considering that the following example code, depending on the compiler, either prints FOO (intel fortan 19.1.1) or BAR (gfortran 7.5, maybe a newer version gives different results?), I wonder which is correct.

module foo

type :: foo_t
contains
    procedure, private :: foobar
    procedure :: exec
end type foo_t

contains

    subroutine exec(obj)
        class(foo_t) :: obj
        call obj%foobar()
    end subroutine exec

    subroutine foobar(this)
        class(foo_t) :: this
        print *, "FOO"
    end subroutine foobar
end module foo

module bar
    use foo

    type, extends(foo_t) :: bar_t
    contains
        procedure :: foobar => impl
    end type bar_t

contains

    subroutine impl(this)
        class(bar_t) :: this
        print *, "BAR"
    end subroutine impl
end module bar

program test
    use foo
    use bar

    class(foo_t), allocatable :: inst
    allocate( bar_t :: inst)
    call inst%exec()
end program test

Furthermore, is it possible to extend an abstract type with a private deferred method in a different module, as it would be the case in the example if foobar was deferred (which only compiles with the aforementioned gfortran compiler and then yields the expected result, but the question is if this is also the correct behavior)?

1 Answers

The correct result is FOO.

This is a known bug in gfortran (https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47805).

A private binding is not accessible in another module. Because the private binding is not accessible in module bar, it is not overridden (F2018 7.5.7.3p1). The binding definition in bar_t is unrelated to the binding in foo_t.

(An abstract type with a private deferred binding is not usable outside the module that defines it. See interpretation F08/0052 for more details.)

Related