llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
28.96k stars 11.94k forks source link

[flang] Incorrect Error for `bind(c)` procedure #115010

Open everythingfunctional opened 2 days ago

everythingfunctional commented 2 days ago

When compiling something like the following example code:

program main
    implicit none

    type :: a
        integer, pointer :: b
    end type
contains
    subroutine sub(x) bind(c)
        type(a), pointer, intent(in) :: x
    end subroutine
end program

flang reports an error like the following:

error: Semantic errors in example.f90
./example.f90:9:41: error: The derived type of an interoperable object must be interoperable, but is not
          type(a), pointer, intent(in) :: x
                                          ^
./example.f90:4:13: Non-interoperable type
      type :: a
              ^
./example.f90:5:29: An interoperable derived type cannot have a pointer or allocatable component
          integer, pointer :: b

However this is incorrect based on the following excerpt from the standard:

18.3.7 Interoperability of procedures and procedure interfaces ... A Fortran procedure interface is interoperable with a C function prototype if ... (5) any dummy argument without the VALUE attribute corresponds to a formal parameter of the prototype that is of a pointer type, and either ... * the dummy argument is ... a pointer without the CONTIGUOUS attribute, and the formal parameter is a pointer to CFI_cdesc_t

klausler commented 2 days ago

Your example is not portable; all compilers except IFX and Cray reject it due to the non-interoperable type of the dummy argument.

I will adjust f18 to accept it with a portability warning.