public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/113338] New: Valid Code Rejected, bind(C) procedure with pointer argument
@ 2024-01-11 18:48 everythingfunctional at protonmail dot com
  2024-01-11 19:56 ` [Bug fortran/113338] " anlauf at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: everythingfunctional at protonmail dot com @ 2024-01-11 18:48 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 113338
           Summary: Valid Code Rejected, bind(C) procedure with pointer
                    argument
           Product: gcc
           Version: 13.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: everythingfunctional at protonmail dot com
  Target Milestone: ---

The following example is rejected by gfortran, but should be valid.

Fortran Program:

program example
    use iso_c_binding

    implicit none

    type :: t
        integer :: i
    end type

    interface
        subroutine c_proc(x) bind(c)
            import t
            implicit none
            type(t), pointer, intent(in) :: x
        end subroutine
    end interface

    type(t), target :: x

    x%i = 42
    call c_proc(x)
contains
    subroutine f_proc(x) bind(c)
        type(t), pointer, intent(in) :: x

        print *, x%i
    end subroutine
end program

C code:

#include <ISO_Fortran_binding.h>

extern void f_proc(CFI_cdesc_t* x);

extern void c_proc(CFI_cdesc_t* x)
{
    f_proc(x);
}

Is rejected with the following messages:

main.f90:11:27:

   11 |         subroutine c_proc(x) bind(c)
      |                           1
Error: Variable ‘x’ at (1) is a dummy argument to the BIND(C) procedure
‘c_proc’ but is not C interoperable because derived type ‘t’ is not C
interoperable
main.f90:23:23:

   23 |     subroutine f_proc(x) bind(c)
      |                       1
Error: Variable ‘x’ at (1) is a dummy argument to the BIND(C) procedure
‘f_proc’ but is not C interoperable because derived type ‘t’ is not C
interoperable

But the standard says:

> 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 allocatable, assumed-shape, assumed-rank, or a pointer without the CONTIGUOUS attribute, and the formal parameter is a pointer to CFI_cdesc_t

I.e., that argument need not be interoperable, because it has the pointer
attribute.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/113338] Valid Code Rejected, bind(C) procedure with pointer argument
  2024-01-11 18:48 [Bug fortran/113338] New: Valid Code Rejected, bind(C) procedure with pointer argument everythingfunctional at protonmail dot com
@ 2024-01-11 19:56 ` anlauf at gcc dot gnu.org
  2024-01-12  1:51 ` everythingfunctional at protonmail dot com
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-01-11 19:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from anlauf at gcc dot gnu.org ---
NAG also rejects the code.

The code compiles with gfortran if one declares t interoperable:

  type, bind(c) :: t


Note that F2008 still had:

"(5) any dummy argument without the VALUE attribute corresponds to a formal
 parameter of the prototype that is of a pointer type, and the dummy argument
 is interoperable with an entity of the referenced type (ISO/IEC 9899:1999,
 6.2.5, 7.17, and 7.18.1) of the formal parameter, ..."

I wonder why the interoperability requirement was dropped.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/113338] Valid Code Rejected, bind(C) procedure with pointer argument
  2024-01-11 18:48 [Bug fortran/113338] New: Valid Code Rejected, bind(C) procedure with pointer argument everythingfunctional at protonmail dot com
  2024-01-11 19:56 ` [Bug fortran/113338] " anlauf at gcc dot gnu.org
@ 2024-01-12  1:51 ` everythingfunctional at protonmail dot com
  2024-01-12 20:23 ` [Bug fortran/113338] [F2018] " anlauf at gcc dot gnu.org
  2024-01-29 17:59 ` anlauf at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: everythingfunctional at protonmail dot com @ 2024-01-12  1:51 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Brad Richardson <everythingfunctional at protonmail dot com> ---
The addition of CFI_cdesc_t in 2018 means it is possible to pass
non-interoperable types to C so long as it doesn't need to know anything about
its type (i.e. doesn't try to modify or copy it). And yes, in this example it's
possible to add bind(C) to the type, but in the code I'm trying to write I
can't.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/113338] [F2018] Valid Code Rejected, bind(C) procedure with pointer argument
  2024-01-11 18:48 [Bug fortran/113338] New: Valid Code Rejected, bind(C) procedure with pointer argument everythingfunctional at protonmail dot com
  2024-01-11 19:56 ` [Bug fortran/113338] " anlauf at gcc dot gnu.org
  2024-01-12  1:51 ` everythingfunctional at protonmail dot com
@ 2024-01-12 20:23 ` anlauf at gcc dot gnu.org
  2024-01-29 17:59 ` anlauf at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-01-12 20:23 UTC (permalink / raw)
  To: gcc-bugs

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

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2024-01-12
           Keywords|                            |rejects-valid
     Ever confirmed|0                           |1
             Status|UNCONFIRMED                 |NEW
             Blocks|                            |85836
            Summary|Valid Code Rejected,        |[F2018] Valid Code
                   |bind(C) procedure with      |Rejected, bind(C) procedure
                   |pointer argument            |with pointer argument


Referenced Bugs:

https://gcc.gnu.org/bugzilla/show_bug.cgi?id=85836
[Bug 85836] [meta-bug] Fortran 2018 support

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug fortran/113338] [F2018] Valid Code Rejected, bind(C) procedure with pointer argument
  2024-01-11 18:48 [Bug fortran/113338] New: Valid Code Rejected, bind(C) procedure with pointer argument everythingfunctional at protonmail dot com
                   ` (2 preceding siblings ...)
  2024-01-12 20:23 ` [Bug fortran/113338] [F2018] " anlauf at gcc dot gnu.org
@ 2024-01-29 17:59 ` anlauf at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: anlauf at gcc dot gnu.org @ 2024-01-29 17:59 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from anlauf at gcc dot gnu.org ---
I just tried the example in comment#0 with ifort/ifx and noticed that it
may be over-simplified: the contained procedure is internal and thus not
visible to the external C code.  The BIND attribute does not change that.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-01-29 17:59 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-11 18:48 [Bug fortran/113338] New: Valid Code Rejected, bind(C) procedure with pointer argument everythingfunctional at protonmail dot com
2024-01-11 19:56 ` [Bug fortran/113338] " anlauf at gcc dot gnu.org
2024-01-12  1:51 ` everythingfunctional at protonmail dot com
2024-01-12 20:23 ` [Bug fortran/113338] [F2018] " anlauf at gcc dot gnu.org
2024-01-29 17:59 ` anlauf at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).