public inbox for fortran@gcc.gnu.org
 help / color / mirror / Atom feed
From: Harald Anlauf <anlauf@gmx.de>
To: fortran <fortran@gcc.gnu.org>, gcc-patches <gcc-patches@gcc.gnu.org>
Subject: [PATCH] Fortran: a C interoperable function cannot have the CLASS attribute [PR95375]
Date: Tue, 20 Dec 2022 21:40:23 +0100	[thread overview]
Message-ID: <trinity-d347c3e7-b009-48e8-b790-9380995fd72a-1671568823481@3c-app-gmx-bs49> (raw)

[-- Attachment #1: Type: text/plain, Size: 280 bytes --]

Dear all,

we obviously forgot to extend the C interoperability check of the
type of function results to CLASS variables and thus did not reject
them.  Wrong code could lead to an ICE, see testcase by Gerhard.

Regtested on x86_64-pc-linux-gnu.  OK for mainline?

Thanks,
Harald


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: pr95375.diff --]
[-- Type: text/x-patch, Size: 2432 bytes --]

From dc22544c2412cf8810a4956f537a2f50e0711a05 Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Tue, 20 Dec 2022 21:17:08 +0100
Subject: [PATCH] Fortran: a C interoperable function cannot have the CLASS
 attribute [PR95375]

gcc/fortran/ChangeLog:

	PR fortran/95375
	* decl.cc (verify_bind_c_sym): Extend interoperability check to
	CLASS variables.

gcc/testsuite/ChangeLog:

	PR fortran/95375
	* gfortran.dg/bind_c_procs_4.f90: New test.
---
 gcc/fortran/decl.cc                          | 12 ++++++++----
 gcc/testsuite/gfortran.dg/bind_c_procs_4.f90 | 17 +++++++++++++++++
 2 files changed, 25 insertions(+), 4 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/bind_c_procs_4.f90

diff --git a/gcc/fortran/decl.cc b/gcc/fortran/decl.cc
index 1562dc22bc6..e593518a77e 100644
--- a/gcc/fortran/decl.cc
+++ b/gcc/fortran/decl.cc
@@ -5998,10 +5998,14 @@ verify_bind_c_sym (gfc_symbol *tmp_sym, gfc_typespec *ts,
 	    }
 	  else
 	    {
-              if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED)
-                gfc_error ("Type declaration %qs at %L is not C "
-                           "interoperable but it is BIND(C)",
-                           tmp_sym->name, &(tmp_sym->declared_at));
+	      if (tmp_sym->ts.type == BT_DERIVED || ts->type == BT_DERIVED
+		  || tmp_sym->ts.type == BT_CLASS || ts->type == BT_CLASS)
+		{
+		  gfc_error ("Type declaration %qs at %L is not C "
+			     "interoperable but it is BIND(C)",
+			     tmp_sym->name, &(tmp_sym->declared_at));
+		  retval = false;
+		}
               else if (warn_c_binding_type)
                 gfc_warning (OPT_Wc_binding_type, "Variable %qs at %L "
                              "may not be a C interoperable "
diff --git a/gcc/testsuite/gfortran.dg/bind_c_procs_4.f90 b/gcc/testsuite/gfortran.dg/bind_c_procs_4.f90
new file mode 100644
index 00000000000..407d8bb9afc
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/bind_c_procs_4.f90
@@ -0,0 +1,17 @@
+! { dg-do compile }
+! PR fortran/95375 - ICE in add_use_op
+! Contributed by G.Steinmetz
+
+function f() result(n) bind(c)      ! { dg-error "not C interoperable" }
+  class(*), allocatable :: n
+end
+program p
+  interface
+     function f() result(n) bind(c)
+       integer :: n
+     end
+  end interface
+  if ( f() /= 0 ) stop
+end
+
+! { dg-prune-output "Type mismatch" }
--
2.35.3


             reply	other threads:[~2022-12-20 20:40 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-12-20 20:40 Harald Anlauf [this message]
2022-12-20 21:10 ` Steve Kargl

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=trinity-d347c3e7-b009-48e8-b790-9380995fd72a-1671568823481@3c-app-gmx-bs49 \
    --to=anlauf@gmx.de \
    --cc=fortran@gcc.gnu.org \
    --cc=gcc-patches@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).