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: resolve correct generic with TYPE(C_PTR) arguments [PR61615]
Date: Mon, 10 Apr 2023 22:49:46 +0200	[thread overview]
Message-ID: <trinity-eca740fe-9b24-4f46-9744-67b7dff20908-1681159786501@3c-app-gmx-bs33> (raw)

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

Dear all,

when comparing formal and actual arguments of a procedure, there was no
check of rank for derived types from intrinsic module ISO_C_BINDING.
This could lead to a wrong resolution of generic procedures with dummy
argument of related types, see PR.  This was likely an oversight.

The attached fix is simple and regtests cleanly on x86_64-pc-linux-gnu.

OK for mainline?

Thanks,
Harald


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

From d41aa0f60b53799a5d28743f168fbf312461f51f Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Mon, 10 Apr 2023 22:39:52 +0200
Subject: [PATCH] Fortran: resolve correct generic with TYPE(C_PTR) arguments
 [PR61615]

gcc/fortran/ChangeLog:

	PR fortran/61615
	* interface.cc (compare_parameter): Enable rank check for arguments
	of derived type from the intrinsic module ISO_C_BINDING.

gcc/testsuite/ChangeLog:

	PR fortran/61615
	* gfortran.dg/interface_49.f90: New test.
---
 gcc/fortran/interface.cc                   | 14 ++++++-
 gcc/testsuite/gfortran.dg/interface_49.f90 | 43 ++++++++++++++++++++++
 2 files changed, 56 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/interface_49.f90

diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc
index db79b104dc2..8682dc999be 100644
--- a/gcc/fortran/interface.cc
+++ b/gcc/fortran/interface.cc
@@ -2361,7 +2361,19 @@ compare_parameter (gfc_symbol *formal, gfc_expr *actual,
       && formal->ts.u.derived && formal->ts.u.derived->ts.is_iso_c
       && actual->ts.type == BT_DERIVED
       && actual->ts.u.derived && actual->ts.u.derived->ts.is_iso_c)
-    return true;
+    {
+      if (ranks_must_agree
+	  && ((actual->rank == 0 && formal->attr.dimension)
+	      || (actual->rank != 0 && !formal->attr.dimension)))
+	{
+	  if (where)
+	    argument_rank_mismatch (formal->name, &actual->where,
+				    symbol_rank (formal), actual->rank,
+				    NULL);
+	  return false;
+	}
+      return true;
+    }

   if (formal->ts.type == BT_CLASS && actual->ts.type == BT_DERIVED)
     /* Make sure the vtab symbol is present when
diff --git a/gcc/testsuite/gfortran.dg/interface_49.f90 b/gcc/testsuite/gfortran.dg/interface_49.f90
new file mode 100644
index 00000000000..67d3e3f871b
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/interface_49.f90
@@ -0,0 +1,43 @@
+! { dg-do run }
+! PR fortran/61615 - resolve correct generic with TYPE(C_PTR) arguments
+! Contributed by Jacob Abel
+
+MODULE foo
+  USE iso_c_binding, only : c_ptr
+  IMPLICIT NONE
+  integer :: rank = -99
+  INTERFACE bar
+    MODULE PROCEDURE bar_s
+    MODULE PROCEDURE bar_a1d
+  END INTERFACE bar
+CONTAINS
+  SUBROUTINE bar_s(a)
+    TYPE(c_ptr) :: a
+    WRITE (0, *) 'in bar_s'
+    rank = 0
+  END SUBROUTINE bar_s
+
+  SUBROUTINE bar_a1d(a)
+    TYPE(c_ptr) :: a(:)
+    WRITE (0, *) 'in bar_a1d'
+    rank = 1
+  END SUBROUTINE bar_a1d
+END MODULE foo
+
+PROGRAM cptr_array_vs_scalar_arg
+  USE foo
+  USE iso_c_binding, only : c_ptr, c_loc
+  IMPLICIT NONE
+  INTEGER, TARGET :: i
+  TYPE(c_ptr)     :: a, b(1)
+  a    = C_LOC(i)
+  b(1) = C_LOC(i)
+  CALL bar(a)
+  if (rank /= 0) stop 1
+  CALL bar(b)
+  if (rank /= 1) stop 2
+  CALL bar((a))
+  if (rank /= 0) stop 3
+  CALL bar((b))
+  if (rank /= 1) stop 4
+END PROGRAM cptr_array_vs_scalar_arg
--
2.35.3


             reply	other threads:[~2023-04-10 20:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-04-10 20:49 Harald Anlauf [this message]
2023-04-11  0:43 ` Jerry D
2023-04-11 14:54   ` [PATCH, v2] " Harald Anlauf

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-eca740fe-9b24-4f46-9744-67b7dff20908-1681159786501@3c-app-gmx-bs33 \
    --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).