Skip to content

[flang] Default initialization of derived type procedure pointer component fails across file boundaries #170331

@edvinolo

Description

@edvinolo

When compiled with flang, the following module and program combination leads to a segmentation fault due to a null pointer. The problem seems to be that the procedure pointer component bar of the type typ is not properly initialized when the type definition is in a separate source file from where the initialization happens. If I put the module and program in the same source file, there are no issues.

Reproducer:

https://gcc.godbolt.org/z/s8rzoroq7

  • m_1.f90:
module m_1
implicit none

type, public :: typ
    integer :: i = 11
    procedure(foo), pointer :: bar => goo
end type typ

abstract interface
function foo(this,x) result(res)
    import :: typ
    implicit none
    class(typ), intent(in) :: this
    integer, intent(in) :: x
    integer :: res
end function foo
end interface

contains

function goo(this,x) result(res)
    class(typ), intent(in) :: this
    integer, intent(in) :: x
    integer :: res

    res = x + this%i
end function goo

end module m_1
  • example.f90:
program example
use m_1, only: typ
use iso_c_binding, only: c_funloc, c_intptr_t
use iso_fortran_env, only: output_unit
implicit none

integer :: x
type(typ) :: a

x = 1

print *,'a%i:', a%i
print *, 'c_funloc(a%bar):', transfer(c_funloc(a%bar),0_c_intptr_t)
print *, 'associated(a%bar):', associated(a%bar)
flush(output_unit)
print *, a%bar(x)

end program example

Compiling with flang m_1.f90 example.f90 and then running the program prints

a%i: 11
c_funloc(a%bar): 0
associated(a%bar): F

before terminating with a segmentation fault. Compiling with gfortran instead leads to normal execution and the following example output

a%i:          11
c_funloc(a%bar):              4198842
associated(a%bar): T
         12

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions