]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: improve diagnostic message for COMMON with automatic object [PR32986]
authorHarald Anlauf <anlauf@gmx.de>
Wed, 23 Aug 2023 19:08:01 +0000 (21:08 +0200)
committerHarald Anlauf <anlauf@gmx.de>
Wed, 23 Aug 2023 19:08:01 +0000 (21:08 +0200)
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
gcc/testsuite/gfortran.dg/common_28.f90 [new file with mode: 0644]

index ce8261d646a961a62c281388753dbee02192fec2..1042b8c18e8eba741ddcb0f817920f5510cdd9ac 100644 (file)
@@ -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 (file)
index 0000000..9b583b9
--- /dev/null
@@ -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