Issue 241884
Summary [Flang] Matching BIND(C) types rejected in intrinsic assignment after separate module compilation
Labels flang
Assignees
Reporter hartsw
    Flang rejects intrinsic assignment between matching BIND(C) derived types
when the source type is read from a separately compiled module. Compiling
the equivalent single-file reproducer succeeds on the same machine.

Compiler:
flang version 23.1.5 (Fedora 23.1.5-2.fc45)
Target: x86_64-redhat-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
Configuration file: /etc/clang/x86_64-redhat-linux-gnu-flang.cfg

I noticed this when I was trying to compile some SWIG related stuff (flibcpp, repo is https://github.com/swig-fortran/flibcpp).  

Reproducer (no C/C++ dependencies or linking required):

random_wrapper.f90:
```fortran
module random_wrapper
  use, intrinsic :: iso_c_binding
  implicit none
 private
  type, bind(c) :: SwigClassWrapper
    type(c_ptr), public :: cptr = c_null_ptr
    integer(c_int), public :: cmemflags = 0
  end type
 type, public :: Engine
    type(SwigClassWrapper), public :: swigdata
  end type
end module
```

algorithm_wrapper.f90:
```fortran
module algorithm_wrapper
  use, intrinsic :: iso_c_binding
  use random_wrapper
 implicit none
  private
  public :: copy_wrapper
  type, bind(c) :: SwigClassWrapper
    type(c_ptr), public :: cptr = c_null_ptr
 integer(c_int), public :: cmemflags = 0
  end type
contains
  subroutine copy_wrapper(g)
    class(Engine), intent(in) :: g
 type(SwigClassWrapper) :: farg1
    farg1 = g%swigdata
    if (farg1%cmemflags /= 42_c_int) stop 1
    if (c_associated(farg1%cptr)) stop 2
  end subroutine
end module
```

Compile in a fresh directory, using two separate invocations:
```sh
flang -c random_wrapper.f90
flang -c algorithm_wrapper.f90
```

Expected: both commands succeed.
Actual: first succeeds, second reports:
```text
error: Semantic errors in algorithm_wrapper.f90
algorithm_wrapper.f90:15:5: error: No intrinsic or user-defined ASSIGNMENT(=) matches operand types TYPE(swigclasswrapper) and TYPE(swigclasswrapper)
      farg1 = g%swigdata
 ^^^^^^^^^^^^^^^^^^
```

If you put everything in one file it compiles fine.  It also compiles with gfortran and ifx.  For flang a component-by-component
copy works around the original failure.
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to