]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Fortran: Fix error recovery for bad component arrayspecs [PR108434]
authorPaul Thomas <pault@gcc.gnu.org>
Sat, 11 Jan 2025 08:23:48 +0000 (08:23 +0000)
committerPaul Thomas <pault@gcc.gnu.org>
Sat, 11 Jan 2025 08:23:48 +0000 (08:23 +0000)
2025-01-11  Paul Thomas  <pault@gcc.gnu.org>

gcc/fortran/
PR fortran/108434
* class.cc (generate_finalization_wrapper): To avoid memory
leaks from callocs, return immediately if the derived type
error flag is set.
* decl.cc (build_struct): If the declaration of a derived type
or class component does not have a deferred arrayspec, correct,
set the error flag of the derived type and emit an immediate
error.

gcc/testsuite/
PR fortran/108434
* gfortran.dg/pr108434.f90 : Add tests from comment 1.

gcc/fortran/class.cc
gcc/fortran/decl.cc
gcc/testsuite/gfortran.dg/pr108434.f90

index 3e0dce1b54d8cb52ecfc3e80fcb07171a077a8e1..97ff54df5e1cd5c9f085708b38012354c2e6ade5 100644 (file)
@@ -1739,7 +1739,7 @@ generate_finalization_wrapper (gfc_symbol *derived, gfc_namespace *ns,
   gfc_expr *ancestor_wrapper = NULL, *rank;
   gfc_iterator *iter;
 
-  if (derived->attr.unlimited_polymorphic)
+  if (derived->attr.unlimited_polymorphic || derived->error)
     {
       vtab_final->initializer = gfc_get_null_expr (NULL);
       return;
index 1bc86ae54bf7eae5c24ee664e34d9b6939f43ab0..0c597607bd8b6daec6e2279f093c954ba46546b2 100644 (file)
@@ -2421,11 +2421,24 @@ build_struct (const char *name, gfc_charlen *cl, gfc_expr **init,
     }
   else if (c->attr.allocatable)
     {
+      const char *err = G_("Allocatable component of structure at %C must have "
+                          "a deferred shape");
       if (c->as->type != AS_DEFERRED)
        {
-         gfc_error ("Allocatable component of structure at %C must have a "
-                    "deferred shape");
-         return false;
+         if (c->ts.type == BT_CLASS || c->ts.type == BT_DERIVED)
+           {
+             /* Issue an immediate error and allow this component to pass for
+                the sake of clean error recovery.  Set the error flag for the
+                containing derived type so that finalizers are not built.  */
+             gfc_error_now (err);
+             s->sym->error = 1;
+             c->as->type = AS_DEFERRED;
+           }
+         else
+           {
+             gfc_error (err);
+             return false;
+           }
        }
     }
   else
index e1768a57574404302188f2e5373bfca99d3ef53d..b7f435338051d515449c2447c2c63d92d7a5eaac 100644 (file)
@@ -1,11 +1,19 @@
 ! { dg-do compile }
 ! PR fortran/108434 - ICE in class_allocatable
-! Contributed by G.Steinmetz
+! Contributed by G.Steinmetz  <gscfq@t-online.de>
 
 program p
   type t
      class(c), pointer :: a(2) ! { dg-error "must have a deferred shape" }
   end type t
+  type s
+     class(d), allocatable :: a(2) ! { dg-error "must have a deferred shape|not been declared" }
+  end type
+  type u
+     type(e),  allocatable :: b(2) ! { dg-error "must have a deferred shape|not been declared" }
+  end type
   class(t), allocatable :: x
   class(t), pointer     :: y
+  class(s), allocatable :: x2
+  class(s), pointer :: y2
 end