public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/99922] New: Bind(C) with assumed length character should work
@ 2021-04-05 23:18 everythingfunctional at protonmail dot com
  2021-04-06  2:04 ` [Bug fortran/99922] " kargl at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: everythingfunctional at protonmail dot com @ 2021-04-05 23:18 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 99922
           Summary: Bind(C) with assumed length character should work
           Product: gcc
           Version: 10.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: everythingfunctional at protonmail dot com
  Target Milestone: ---

As of (at least) Fortran 2018, bind(C) should be allowed for procedures with
assumed length character variables, as one can pass CFI_cdesc_t* from the C
side and it's supposed to work.

MWE below

! main.f90
program main
    implicit none

    interface
        subroutine say_hello_c() bind(C)
        end subroutine
    end interface

    call say_hello_c()
end program

! say_hello_fortran.f90
subroutine say_hello_fortran(name) bind(C)
    use iso_c_binding, only: c_char

    implicit none

    character(len=*, kind=c_char), intent(in) :: name

    print *, "Hello from Fortran, " // name // "!"
end subroutine

! say_hello_c.c
#include <string.h>
#include <ISO_Fortran_binding.h>

extern void say_hello_fortran(CFI_cdesc_t * name_descriptor);

void say_hello_c() {
    char * name = "World";
    CFI_cdesc_t name_descriptor;
    int error_code = CFI_establish(
            &name_descriptor,
            name,
            CFI_attribute_other,
            CFI_type_char,
            strlen(name),
            0,
            NULL);
    say_hello_fortran(&name_descriptor);
}

Compiling say_hello_fortran.f90 gives the error

say_hello_fortran.f90:1:33:

    1 | subroutine say_hello_fortran(name) bind(C)
      |                                 1
Error: Character argument ‘name’ at (1) must be length 1 because procedure
‘say_hello_fortran’ is BIND(C)

But the relevant text from the standard says (from section 18.3.6):

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 nonallocatable nonpointer variable of type CHARACTER
with assumed character length and the formal parameter is a pointer to
CFI_cdesc_t,

So this is supposed to be allowed, and I can confirm that I can compile and
execute the above example and obtain the expected result with Intel (ifort and
icc).

output of gfortran --version:
GNU Fortran (Ubuntu 10.2.0-13ubuntu1) 10.2.0
Copyright (C) 2020 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

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

* [Bug fortran/99922] Bind(C) with assumed length character should work
  2021-04-05 23:18 [Bug fortran/99922] New: Bind(C) with assumed length character should work everythingfunctional at protonmail dot com
@ 2021-04-06  2:04 ` kargl at gcc dot gnu.org
  2021-04-06 16:34 ` everythingfunctional at protonmail dot com
  2021-10-22 20:42 ` sandra at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: kargl at gcc dot gnu.org @ 2021-04-06  2:04 UTC (permalink / raw)
  To: gcc-bugs

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

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2021-04-06
             Status|UNCONFIRMED                 |NEW
                 CC|                            |kargl at gcc dot gnu.org
           Priority|P3                          |P4
     Ever confirmed|0                           |1

--- Comment #1 from kargl at gcc dot gnu.org ---
(In reply to Brad Richardson from comment #0)
> 
> So this is supposed to be allowed, and I can confirm that I can compile and
> execute the above example and obtain the expected result with Intel (ifort
> and icc).
> 

What result did Intel produce?

This patch suppresses the error message, which allows the code
to compile.  gfortran may produces wrong code.  Someone else can
investigate that issue

diff --git a/gcc/fortran/decl.c b/gcc/fortran/decl.c
index 9039c9dca2a..b10a92ca234 100644
--- a/gcc/fortran/decl.c
+++ b/gcc/fortran/decl.c
@@ -1549,19 +1549,21 @@ gfc_verify_c_interop_param (gfc_symbol *sym)
                             sym->ns->proc_name->name);
            }

-          /* Character strings are only C interoperable if they have a
-             length of 1.  */
-          if (sym->ts.type == BT_CHARACTER && !sym->attr.dimension)
+          /* In F2008 (and earlier?) a character string is only C
+            interoperable if it has a length of 1.  With F2018, if the
+            string is a dummy argument, it can have an assumed length if
+            the formal argument is CFI_cdesc_t.  */
+          if (sym->ts.type == BT_CHARACTER && !sym->attr.dimension
+             && !((gfc_option.allow_std & GFC_STD_F2018) && sym->attr.dummy))
            {
              gfc_charlen *cl = sym->ts.u.cl;
+
              if (!cl || !cl->length || cl->length->expr_type != EXPR_CONSTANT
                   || mpz_cmp_si (cl->length->value.integer, 1) != 0)
                {
-                 gfc_error ("Character argument %qs at %L "
-                            "must be length 1 because "
-                            "procedure %qs is BIND(C)",
-                            sym->name, &sym->declared_at,
-                            sym->ns->proc_name->name);
+                 gfc_error ("Character argument %qs at %L must be length 1 "
+                            "because procedure %qs is BIND(C)", sym->name, 
+                            &sym->declared_at, sym->ns->proc_name->name);
                  retval = false;
                }
            }

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

* [Bug fortran/99922] Bind(C) with assumed length character should work
  2021-04-05 23:18 [Bug fortran/99922] New: Bind(C) with assumed length character should work everythingfunctional at protonmail dot com
  2021-04-06  2:04 ` [Bug fortran/99922] " kargl at gcc dot gnu.org
@ 2021-04-06 16:34 ` everythingfunctional at protonmail dot com
  2021-10-22 20:42 ` sandra at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: everythingfunctional at protonmail dot com @ 2021-04-06 16:34 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Brad Richardson <everythingfunctional at protonmail dot com> ---
Output from compiling and running with Intel:

[pop-os:~/tmp/c_string_interop] ifort -V
Intel(R) Fortran Intel(R) 64 Compiler Classic for applications running on
Intel(R) 64, Version 2021.2.0 Build 20210228_000000
Copyright (C) 1985-2021 Intel Corporation.  All rights reserved.

[pop-os:~/tmp/c_string_interop] ifort -c say_hello_fortran.f90 -o
say_hello_fortran.f90.o
[pop-os:~/tmp/c_string_interop] icc -c say_hello_c.c -o say_hello_c.c.o
[pop-os:~/tmp/c_string_interop] ifort -c main.f90 -o main.f90.o
[pop-os:~/tmp/c_string_interop] ifort say_hello_fortran.f90.o say_hello_c.c.o
main.f90.o -o main.exe
[pop-os:~/tmp/c_string_interop] ./main.exe 
 Hello from Fortran, World!

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

* [Bug fortran/99922] Bind(C) with assumed length character should work
  2021-04-05 23:18 [Bug fortran/99922] New: Bind(C) with assumed length character should work everythingfunctional at protonmail dot com
  2021-04-06  2:04 ` [Bug fortran/99922] " kargl at gcc dot gnu.org
  2021-04-06 16:34 ` everythingfunctional at protonmail dot com
@ 2021-10-22 20:42 ` sandra at gcc dot gnu.org
  2 siblings, 0 replies; 4+ messages in thread
From: sandra at gcc dot gnu.org @ 2021-10-22 20:42 UTC (permalink / raw)
  To: gcc-bugs

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

sandra at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |sandra at gcc dot gnu.org
             Status|NEW                         |RESOLVED
         Resolution|---                         |DUPLICATE

--- Comment #3 from sandra at gcc dot gnu.org ---
This is a duplicate of now-fixed PR 92482.

*** This bug has been marked as a duplicate of bug 92482 ***

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

end of thread, other threads:[~2021-10-22 20:42 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-05 23:18 [Bug fortran/99922] New: Bind(C) with assumed length character should work everythingfunctional at protonmail dot com
2021-04-06  2:04 ` [Bug fortran/99922] " kargl at gcc dot gnu.org
2021-04-06 16:34 ` everythingfunctional at protonmail dot com
2021-10-22 20:42 ` sandra 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).