public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug fortran/102368] New: Failure to compile program using the C_SIZEOF function in ISO_C_BINDING
@ 2021-09-16 14:49 longb at cray dot com
  2021-11-11 22:21 ` [Bug fortran/102368] " anlauf at gcc dot gnu.org
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: longb at cray dot com @ 2021-09-16 14:49 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 102368
           Summary: Failure to compile program using the C_SIZEOF function
                    in ISO_C_BINDING
           Product: gcc
           Version: 11.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: fortran
          Assignee: unassigned at gcc dot gnu.org
          Reporter: longb at cray dot com
  Target Milestone: ---

> cat test.f90
program main
use,intrinsic ::  iso_c_binding
implicit none

character(kind=c_char, len=*), parameter :: blergh = 'abc'

print *, c_sizeof(blergh)
print *, c_sizeof(.true.)
print *, c_sizeof(5)
print *, c_sizeof(5.0)
print *, c_sizeof(5.0d0)
end program main

> gfortran test.f90
test.f90:7:18:

    7 | print *, c_sizeof(blergh)
      |                  1
Error: 'x' argument of 'c_sizeof' intrinsic at (1) must be an interoperable
data entity: Type shall have a character length of 1

> ifort test.f90
> ./a.out
                     3
                     4
                     4
                     4
                     8

gfortran appears to be using pre-F08 rules for C_SIZEOF.

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

* [Bug fortran/102368] Failure to compile program using the C_SIZEOF function in ISO_C_BINDING
  2021-09-16 14:49 [Bug fortran/102368] New: Failure to compile program using the C_SIZEOF function in ISO_C_BINDING longb at cray dot com
@ 2021-11-11 22:21 ` anlauf at gcc dot gnu.org
  2021-11-11 22:53 ` anlauf at gcc dot gnu.org
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: anlauf at gcc dot gnu.org @ 2021-11-11 22:21 UTC (permalink / raw)
  To: gcc-bugs

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

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |anlauf at gcc dot gnu.org
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1
   Last reconfirmed|                            |2021-11-11

--- Comment #1 from anlauf at gcc dot gnu.org ---
Confirmed.

character(kind=c_char, len=*), parameter :: blergh = 'abc'

is interoperable according to e.g. F2018:18.3.4.

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

* [Bug fortran/102368] Failure to compile program using the C_SIZEOF function in ISO_C_BINDING
  2021-09-16 14:49 [Bug fortran/102368] New: Failure to compile program using the C_SIZEOF function in ISO_C_BINDING longb at cray dot com
  2021-11-11 22:21 ` [Bug fortran/102368] " anlauf at gcc dot gnu.org
@ 2021-11-11 22:53 ` anlauf at gcc dot gnu.org
  2021-11-12 17:41 ` anlauf at gcc dot gnu.org
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: anlauf at gcc dot gnu.org @ 2021-11-11 22:53 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from anlauf at gcc dot gnu.org ---
Testing the following patch:

diff --git a/gcc/fortran/check.c b/gcc/fortran/check.c
index ffa07b510cd..f325e5e4d5f 100644
--- a/gcc/fortran/check.c
+++ b/gcc/fortran/check.c
@@ -5272,13 +5272,18 @@ is_c_interoperable (gfc_expr *expr, const char **msg,
bool c_loc, bool c_f_ptr)
        && !gfc_simplify_expr (expr->ts.u.cl->length, 0))
       gfc_internal_error ("is_c_interoperable(): gfc_simplify_expr failed");

-    if (!c_loc && expr->ts.u.cl
-       && (!expr->ts.u.cl->length
-           || expr->ts.u.cl->length->expr_type != EXPR_CONSTANT
-           || mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0))
+    if (!c_loc && expr->ts.u.cl)
       {
-       *msg = "Type shall have a character length of 1";
-       return false;
+       bool len_ok = (expr->ts.u.cl->length
+                      && expr->ts.u.cl->length->expr_type == EXPR_CONSTANT);
+       /* F2008: character variable of constant length is interoperable.  */
+       if (len_ok && !(gfc_option.allow_std & GFC_STD_F2008))
+         len_ok = mpz_cmp_si (expr->ts.u.cl->length->value.integer, 1) != 0;
+       if (!len_ok)
+         {
+           *msg = "Type shall have a character length of 1";
+           return false;
+         }
       }
     }

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

* [Bug fortran/102368] Failure to compile program using the C_SIZEOF function in ISO_C_BINDING
  2021-09-16 14:49 [Bug fortran/102368] New: Failure to compile program using the C_SIZEOF function in ISO_C_BINDING longb at cray dot com
  2021-11-11 22:21 ` [Bug fortran/102368] " anlauf at gcc dot gnu.org
  2021-11-11 22:53 ` anlauf at gcc dot gnu.org
@ 2021-11-12 17:41 ` anlauf at gcc dot gnu.org
  2021-11-12 21:36 ` anlauf at gcc dot gnu.org
  2021-11-13 21:15 ` anlauf at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: anlauf at gcc dot gnu.org @ 2021-11-12 17:41 UTC (permalink / raw)
  To: gcc-bugs

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

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Assignee|unassigned at gcc dot gnu.org      |anlauf at gcc dot gnu.org
             Status|NEW                         |ASSIGNED

--- Comment #3 from anlauf at gcc dot gnu.org ---
Corrected the logic and submitted:

https://gcc.gnu.org/pipermail/fortran/2021-November/057007.html

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

* [Bug fortran/102368] Failure to compile program using the C_SIZEOF function in ISO_C_BINDING
  2021-09-16 14:49 [Bug fortran/102368] New: Failure to compile program using the C_SIZEOF function in ISO_C_BINDING longb at cray dot com
                   ` (2 preceding siblings ...)
  2021-11-12 17:41 ` anlauf at gcc dot gnu.org
@ 2021-11-12 21:36 ` anlauf at gcc dot gnu.org
  2021-11-13 21:15 ` anlauf at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: anlauf at gcc dot gnu.org @ 2021-11-12 21:36 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from anlauf at gcc dot gnu.org ---
Note: Steve Lionel of Intel thinks the code in comment#0 is invalid.

But nvfortran, flang and crayftn all accept it without complaining.

@Bill: any more detailed thoughts how to resolve this?

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

* [Bug fortran/102368] Failure to compile program using the C_SIZEOF function in ISO_C_BINDING
  2021-09-16 14:49 [Bug fortran/102368] New: Failure to compile program using the C_SIZEOF function in ISO_C_BINDING longb at cray dot com
                   ` (3 preceding siblings ...)
  2021-11-12 21:36 ` anlauf at gcc dot gnu.org
@ 2021-11-13 21:15 ` anlauf at gcc dot gnu.org
  4 siblings, 0 replies; 6+ messages in thread
From: anlauf at gcc dot gnu.org @ 2021-11-13 21:15 UTC (permalink / raw)
  To: gcc-bugs

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

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|ASSIGNED                    |WAITING

--- Comment #5 from anlauf at gcc dot gnu.org ---
The relevant F2018 standard excerpts are:

18.2.3.7  C_SIZEOF (X)
[...]
Argument. X shall be an interoperable data entity ...

3.45  data entity
data object, result of the evaluation of an expression, or the result of
the execution of a function reference

3.46  data object
constant (7.1.4), variable (9), or subobject of a constant (5.4.3.2.4)

3.92  interoperable
⟨Fortran entity⟩ equivalent to an entity defined by or definable by the
companion processor (18.3)

18.3.1  Interoperability of intrinsic types

Table 18.2 shows the interoperability between Fortran intrinsic types and C
types.  A Fortran intrinsic type with particular type parameter values is
interoperable with a C type if the type and kind type parameter value are
listed in the table on the same row as that C type.  If the type is
character, the length type parameter is interoperable if and only if its
value is one. ...

18.3.4  Interoperability of scalar variables

A named scalar Fortran variable is interoperable if and only if its type
and type parameters are interoperable, [...], and if it is of type
character its length is not assumed or declared by an expression that is
not a constant expression.

An interoperable scalar Fortran variable is interoperable with a scalar C
entity if their types and type parameters are interoperable.


I understand Steve's comment that the length has to be constant and one.
If this is the common understanding, the current PR would be invalid.

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

end of thread, other threads:[~2021-11-13 21:15 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-09-16 14:49 [Bug fortran/102368] New: Failure to compile program using the C_SIZEOF function in ISO_C_BINDING longb at cray dot com
2021-11-11 22:21 ` [Bug fortran/102368] " anlauf at gcc dot gnu.org
2021-11-11 22:53 ` anlauf at gcc dot gnu.org
2021-11-12 17:41 ` anlauf at gcc dot gnu.org
2021-11-12 21:36 ` anlauf at gcc dot gnu.org
2021-11-13 21:15 ` 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).