Proxy ping [PATCH] Fortran: diagnostics for actual arguments to pointer dummy arguments [PR94104]

Message ID trinity-17c8fc65-adb9-4e07-a987-865911332259-1668027022980@3c-app-gmx-bap34
State Accepted
Headers
Series Proxy ping [PATCH] Fortran: diagnostics for actual arguments to pointer dummy arguments [PR94104] |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Harald Anlauf Nov. 9, 2022, 8:50 p.m. UTC
  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
  

Comments

Mikael Morin Nov. 13, 2022, 10:11 a.m. UTC | #1
Le 09/11/2022 à 21:50, Harald Anlauf via Fortran a écrit :
> 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?
> 
OK, thanks.
  

Patch

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