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.
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;
}
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
! { 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