public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
From: "kargl at gcc dot gnu.org" <gcc-bugzilla@gcc.gnu.org>
To: gcc-bugs@gcc.gnu.org
Subject: [Bug fortran/107266] Reject kind=4 characters for BIND(C) – it invalid and generates wrong code
Date: Fri, 14 Oct 2022 20:08:46 +0000	[thread overview]
Message-ID: <bug-107266-4-CUeUHQ8cFx@http.gcc.gnu.org/bugzilla/> (raw)
In-Reply-To: <bug-107266-4@http.gcc.gnu.org/bugzilla/>

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

kargl at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
   Last reconfirmed|                            |2022-10-14
             Status|UNCONFIRMED                 |NEW
     Ever confirmed|0                           |1

--- Comment #4 from kargl at gcc dot gnu.org ---
This un-regression-tested patch

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index d133bc2d034..dfd59febf70 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -15509,6 +15509,33 @@ resolve_symbol (gfc_symbol *sym)
       return;
     }

+  /* A Function result with character(kind=4) cannot have the bind(c)
+     attribute, because c_char is assumed to match default character kind.  */
+  if (sym->attr.function && sym->attr.is_bind_c)
+    {
+      if (sym->ts.type == BT_CHARACTER
+         && sym->result->ts.kind != gfc_default_character_kind)
+       {
+         gfc_error ("Symbol %qs at %L with type CHARACTER(KIND=4) "
+                    "cannot have the BIND(C) attribute", sym->name,
+                    &sym->declared_at);
+         return;
+       }
+
+      if (sym->ts.type == BT_UNKNOWN
+         && sym->result
+         && sym->result->ts.type == BT_CHARACTER
+         && sym->result->ts.kind != gfc_default_character_kind)
+       {
+         gfc_error ("Function result variable %qs at %L with type "
+                    "CHARACTER(KIND=4) at %L cannot have the BIND(C) "
+                    "attribute",
+                    sym->result->name, &sym->result->declared_at,
+                    &sym->result->ts.u.cl->length->where);
+         return;
+       }
+    }
+
   if (sym->attr.artificial)
     return;

with this code

function f(x) bind(C) result(a)
   character(kind=4) a
   character(kind=4), value :: x
   a = x
end

character(kind=4) function g(x) bind(C)
   character(kind=4), value :: x
   g = x
end

yields
% gfcx -c  a.f90
a.f90:1:31:

    1 | function f(x) bind(C) result(a)
      |                               1
    2 |    character(kind=4) a
      |                    2           
Error: Function result variable 'a' at (1) with type CHARACTER(KIND=4) at (2)
cannot have the BIND(C) attribute
a.f90:7:28:

    7 | character(kind=4) function g(x) bind(C)
      |                            1
Error: Symbol 'g' at (1) with type CHARACTER(KIND=4) cannot have the BIND(C)
attribute


Tobias, once you regression test the patch, feel free to commit it.

  parent reply	other threads:[~2022-10-14 20:08 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-14 16:04 [Bug fortran/107266] New: " burnus at gcc dot gnu.org
2022-10-14 16:09 ` [Bug fortran/107266] " burnus at gcc dot gnu.org
2022-10-14 18:31 ` kargl at gcc dot gnu.org
2022-10-14 18:51 ` kargl at gcc dot gnu.org
2022-10-14 18:51 ` kargl at gcc dot gnu.org
2022-10-14 20:08 ` kargl at gcc dot gnu.org [this message]
2022-10-14 21:36 ` burnus at gcc dot gnu.org
2022-10-14 22:21 ` sgk at troutmask dot apl.washington.edu
2022-10-14 22:45 ` kargl at gcc dot gnu.org
2022-10-17 16:15 ` cvs-commit at gcc dot gnu.org
2022-10-17 16:56 ` kargl at gcc dot gnu.org
2022-10-18 10:40 ` burnus at gcc dot gnu.org
2022-10-18 17:29 ` sgk at troutmask dot apl.washington.edu
2022-10-18 22:43 ` sgk at troutmask dot apl.washington.edu
2022-10-18 23:18 ` sgk at troutmask dot apl.washington.edu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=bug-107266-4-CUeUHQ8cFx@http.gcc.gnu.org/bugzilla/ \
    --to=gcc-bugzilla@gcc.gnu.org \
    --cc=gcc-bugs@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).