public inbox for gcc-patches@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: Proxy ping [PATCH] Fortran: diagnostics for actual arguments to pointer dummy arguments [PR94104]
Date: Wed, 9 Nov 2022 21:50:22 +0100	[thread overview]
Message-ID: <trinity-17c8fc65-adb9-4e07-a987-865911332259-1668027022980@3c-app-gmx-bap34> (raw)

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

Dear all,

Jose posted a patch here that was never reviewed:

  https://gcc.gnu.org/pipermail/fortran/2021-June/056162.html

I think the diagnostics improvement is helpful, as it adjusts
to the changes from F2003 to F2008.

The patch suffered a little from bitrot, but was otherwise
straightforward to apply.  I slightly edited the commit
message, as I found the original one difficult to parse.

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

Thanks,
Harald


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

From 46957184b74af8d5a3b41704f5ef48a12f37fe33 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jos=C3=A9=20Rui=20Faustino=20de=20Sousa?=
 <jrfsousa@gmail.com>
Date: Wed, 9 Nov 2022 21:30:25 +0100
Subject: [PATCH] Fortran: diagnostics for actual arguments to pointer dummy
 arguments [PR94104]

Error message improvement.  In Fortran 2008 actual procedure arguments
associated with a pointer, intent(in) attribute, dummy argument
can also have the target attribute, not just pointer.

gcc/fortran/ChangeLog:

	PR fortran/94104
	* interface.cc (gfc_compare_actual_formal): Improve error message
	dependent on Fortran standard level.

gcc/testsuite/ChangeLog:

	PR fortran/94104
	* gfortran.dg/parens_2.f90: Adjust to improved error message.
	* gfortran.dg/PR94104a.f90: New test.
	* gfortran.dg/PR94104b.f90: New test.
---
 gcc/fortran/interface.cc               | 48 +++++++++++++++++---------
 gcc/testsuite/gfortran.dg/PR94104a.f90 | 29 ++++++++++++++++
 gcc/testsuite/gfortran.dg/PR94104b.f90 | 29 ++++++++++++++++
 gcc/testsuite/gfortran.dg/parens_2.f90 |  2 +-
 4 files changed, 90 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/gfortran.dg/PR94104a.f90
 create mode 100644 gcc/testsuite/gfortran.dg/PR94104b.f90

diff --git a/gcc/fortran/interface.cc b/gcc/fortran/interface.cc
index d3e199535b3..49dbd1d886c 100644
--- a/gcc/fortran/interface.cc
+++ b/gcc/fortran/interface.cc
@@ -3477,25 +3477,39 @@ gfc_compare_actual_formal (gfc_actual_arglist **ap, gfc_formal_arglist *formal,
 	  goto match;
 	}

-      if (a->expr->expr_type != EXPR_NULL
-	  && compare_pointer (f->sym, a->expr) == 0)
+      if (a->expr->expr_type != EXPR_NULL)
 	{
-	  if (where)
-	    gfc_error ("Actual argument for %qs must be a pointer at %L",
-		       f->sym->name, &a->expr->where);
-	  ok = false;
-	  goto match;
-	}
+	  int cmp = compare_pointer (f->sym, a->expr);
+	  bool pre2008 = ((gfc_option.allow_std & GFC_STD_F2008) == 0);

-      if (a->expr->expr_type != EXPR_NULL
-	  && (gfc_option.allow_std & GFC_STD_F2008) == 0
-	  && compare_pointer (f->sym, a->expr) == 2)
-	{
-	  if (where)
-	    gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
-		       "pointer dummy %qs", &a->expr->where,f->sym->name);
-	  ok = false;
-	  goto match;
+	  if (pre2008 && cmp == 0)
+	    {
+	      if (where)
+		gfc_error ("Actual argument for %qs at %L must be a pointer",
+			   f->sym->name, &a->expr->where);
+	      ok = false;
+	      goto match;
+	    }
+
+	  if (pre2008 && cmp == 2)
+	    {
+	      if (where)
+		gfc_error ("Fortran 2008: Non-pointer actual argument at %L to "
+			   "pointer dummy %qs", &a->expr->where, f->sym->name);
+	      ok = false;
+	      goto match;
+	    }
+
+	  if (!pre2008 && cmp == 0)
+	    {
+	      if (where)
+		gfc_error ("Actual argument for %qs at %L must be a pointer "
+			   "or a valid target for the dummy pointer in a "
+			   "pointer assignment statement",
+			   f->sym->name, &a->expr->where);
+	      ok = false;
+	      goto match;
+	    }
 	}


diff --git a/gcc/testsuite/gfortran.dg/PR94104a.f90 b/gcc/testsuite/gfortran.dg/PR94104a.f90
new file mode 100644
index 00000000000..a1e578ac9ba
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/PR94104a.f90
@@ -0,0 +1,29 @@
+! { dg-do compile }
+! { dg-options "-std=f2003" }
+!
+! PR fortran/94104
+!
+
+program diag_p
+  implicit none
+
+  integer, parameter :: n = 7
+
+  integer         :: a(n)
+  integer, target :: b(n)
+
+  a = 1
+  print *, sumf(a) ! { dg-error "Actual argument for 'a' at .1. must be a pointer" }
+  print *, sumf(b) ! { dg-error "Fortran 2008: Non-pointer actual argument at .1. to pointer dummy 'a'" }
+
+contains
+
+  function sumf(a) result(s)
+    integer, pointer, intent(in) :: a(:)
+
+    integer :: s
+
+    s = sum(a)
+  end function sumf
+
+end program diag_p
diff --git a/gcc/testsuite/gfortran.dg/PR94104b.f90 b/gcc/testsuite/gfortran.dg/PR94104b.f90
new file mode 100644
index 00000000000..ee7d640b926
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/PR94104b.f90
@@ -0,0 +1,29 @@
+! { dg-do compile }
+! { dg-options "-std=f2008" }
+!
+! PR fortran/94104
+!
+
+program diag_p
+  implicit none
+
+  integer, parameter :: n = 7
+
+  integer         :: a(n)
+  integer, target :: b(n)
+
+  a = 1
+  print *, sumf(a) ! { dg-error "Actual argument for 'a' at .1. must be a pointer or a valid target" }
+  print *, sumf(b)
+
+contains
+
+  function sumf(a) result(s)
+    integer, pointer, intent(in) :: a(:)
+
+    integer :: s
+
+    s = sum(a)
+  end function sumf
+
+end program diag_p
diff --git a/gcc/testsuite/gfortran.dg/parens_2.f90 b/gcc/testsuite/gfortran.dg/parens_2.f90
index bc2acd8e71d..dc5965de014 100644
--- a/gcc/testsuite/gfortran.dg/parens_2.f90
+++ b/gcc/testsuite/gfortran.dg/parens_2.f90
@@ -2,7 +2,7 @@
 ! { dg-do compile }
 ! Originally contributed by Joost VandeVondele
 INTEGER, POINTER :: I
-CALL S1((I)) ! { dg-error "Actual argument for .i. must be a pointer" }
+CALL S1((I)) ! { dg-error "Actual argument for .i. at .1. must be a pointer or a valid target" }
 CONTAINS
  SUBROUTINE S1(I)
   INTEGER, POINTER ::I
--
2.35.3


             reply	other threads:[~2022-11-09 20:50 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-09 20:50 Harald Anlauf [this message]
2022-11-13 10:11 ` Mikael Morin

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-17c8fc65-adb9-4e07-a987-865911332259-1668027022980@3c-app-gmx-bap34 \
    --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).