public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Fortran: improve diagnostic message for COMMON with automatic object [PR32986]
@ 2023-08-23 19:16 Harald Anlauf
  2023-08-23 19:33 ` Steve Kargl
  0 siblings, 1 reply; 2+ messages in thread
From: Harald Anlauf @ 2023-08-23 19:16 UTC (permalink / raw)
  To: fortran, gcc-patches

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

Dear all,

here's a simple patch for a very old PR that suggests a more helpful
error message for an automatic object in a COMMON.  The patch also
suppresses the less helpful old error message after the new one has
been emitted.

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

Thanks,
Harald


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

From 829c0c06fe7ba2cf3e83508b95999b884b21236d Mon Sep 17 00:00:00 2001
From: Harald Anlauf <anlauf@gmx.de>
Date: Wed, 23 Aug 2023 21:08:01 +0200
Subject: [PATCH] Fortran: improve diagnostic message for COMMON with automatic
 object [PR32986]

gcc/fortran/ChangeLog:

	PR fortran/32986
	* resolve.cc (is_non_constant_shape_array): Add forward declaration.
	(resolve_common_vars): Diagnose automatic array object in COMMON.
	(resolve_symbol): Prevent confusing follow-on error.

gcc/testsuite/ChangeLog:

	PR fortran/32986
	* gfortran.dg/common_28.f90: New test.
---
 gcc/fortran/resolve.cc                  | 15 ++++++++++++++-
 gcc/testsuite/gfortran.dg/common_28.f90 |  7 +++++++
 2 files changed, 21 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/gfortran.dg/common_28.f90

diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index ce8261d646a..1042b8c18e8 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -959,6 +959,10 @@ cleanup:
 }


+/* Forward declaration.  */
+static bool is_non_constant_shape_array (gfc_symbol *sym);
+
+
 /* Resolve common variables.  */
 static void
 resolve_common_vars (gfc_common_head *common_block, bool named_common)
@@ -1007,6 +1011,15 @@ resolve_common_vars (gfc_common_head *common_block, bool named_common)
 	gfc_error_now ("%qs at %L cannot appear in COMMON "
 		       "[F2008:C5100]", csym->name, &csym->declared_at);

+      if (csym->attr.dimension && is_non_constant_shape_array (csym))
+	{
+	  gfc_error_now ("Automatic object %qs at %L cannot appear in "
+			 "COMMON at %L", csym->name, &csym->declared_at,
+			 &common_block->where);
+	  /* Avoid confusing follow-on error.  */
+	  csym->error = 1;
+	}
+
       if (csym->ts.type != BT_DERIVED)
 	continue;

@@ -16612,7 +16625,7 @@ resolve_symbol (gfc_symbol *sym)
   /* Resolve array specifier. Check as well some constraints
      on COMMON blocks.  */

-  check_constant = sym->attr.in_common && !sym->attr.pointer;
+  check_constant = sym->attr.in_common && !sym->attr.pointer && !sym->error;

   /* Set the formal_arg_flag so that check_conflict will not throw
      an error for host associated variables in the specification
diff --git a/gcc/testsuite/gfortran.dg/common_28.f90 b/gcc/testsuite/gfortran.dg/common_28.f90
new file mode 100644
index 00000000000..9b583b9948d
--- /dev/null
+++ b/gcc/testsuite/gfortran.dg/common_28.f90
@@ -0,0 +1,7 @@
+! { dg-do compile }
+! PR fortran/32986 - Improve diagnostic message for COMMON with automatic object
+
+function a(n)
+  real :: x(n) ! { dg-error "Automatic object" }
+  common /c/ x ! { dg-error "cannot appear in COMMON" }
+end function
--
2.35.3


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] Fortran: improve diagnostic message for COMMON with automatic object [PR32986]
  2023-08-23 19:16 [PATCH] Fortran: improve diagnostic message for COMMON with automatic object [PR32986] Harald Anlauf
@ 2023-08-23 19:33 ` Steve Kargl
  0 siblings, 0 replies; 2+ messages in thread
From: Steve Kargl @ 2023-08-23 19:33 UTC (permalink / raw)
  To: Harald Anlauf via Fortran; +Cc: gcc-patches

On Wed, Aug 23, 2023 at 09:16:08PM +0200, Harald Anlauf via Fortran wrote:
> 
> here's a simple patch for a very old PR that suggests a more helpful
> error message for an automatic object in a COMMON.  The patch also
> suppresses the less helpful old error message after the new one has
> been emitted.
> 
> Regtested on x86_64-pc-linux-gnu.  OK for mainline?
> 

OK.  I leave the decision on backporting to you.

-- 
Steve

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-08-23 19:33 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-08-23 19:16 [PATCH] Fortran: improve diagnostic message for COMMON with automatic object [PR32986] Harald Anlauf
2023-08-23 19:33 ` Steve Kargl

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).