aboutsummaryrefslogtreecommitdiff
path: root/gcc/fortran
diff options
context:
space:
mode:
authorPaul Thomas <pault@gcc.gnu.org>2023-04-14 11:14:00 +0100
committerPaul Thomas <pault@gcc.gnu.org>2023-04-14 11:15:12 +0100
commitb0e85485fbf042abccee5c0a9eb499da386c8db3 (patch)
tree2d26d1a45f52c274db699e9e80f993b37520b27c /gcc/fortran
parentbf24f2db2841b97bc5e86bf9294d61eef32f83b3 (diff)
downloadgcc-b0e85485fbf042abccee5c0a9eb499da386c8db3.zip
gcc-b0e85485fbf042abccee5c0a9eb499da386c8db3.tar.gz
gcc-b0e85485fbf042abccee5c0a9eb499da386c8db3.tar.bz2
Fortran: Fix an excess finalization during allocation [PR104272]
2023-04-14 Paul Thomas <pault@gcc.gnu.org> gcc/fortran PR fortran/104272 * gfortran.h : Add expr3_not_explicit bit field to gfc_code. * resolve.cc (resolve_allocate_expr): Set bit field when the default initializer is applied to expr3. * trans-stmt.cc (gfc_trans_allocate): If expr3_not_explicit is set, do not deallocate expr3. gcc/testsuite/ PR fortran/104272 * gfortran.dg/class_result_8.f90 : Number of builtin_frees down from 6 to 5 without memory leaks. * gfortran.dg/finalize_52.f90: New test
Diffstat (limited to 'gcc/fortran')
-rw-r--r--gcc/fortran/gfortran.h2
-rw-r--r--gcc/fortran/resolve.cc3
-rw-r--r--gcc/fortran/trans-stmt.cc7
3 files changed, 10 insertions, 2 deletions
diff --git a/gcc/fortran/gfortran.h b/gcc/fortran/gfortran.h
index 130d5d7..db77d24 100644
--- a/gcc/fortran/gfortran.h
+++ b/gcc/fortran/gfortran.h
@@ -3005,6 +3005,8 @@ typedef struct gfc_code
/* Take the array specification from expr3 to allocate arrays
without an explicit array specification. */
unsigned arr_spec_from_expr3:1;
+ /* expr3 is not explicit */
+ unsigned expr3_not_explicit:1;
}
alloc;
diff --git a/gcc/fortran/resolve.cc b/gcc/fortran/resolve.cc
index 58013d4..55d8e32 100644
--- a/gcc/fortran/resolve.cc
+++ b/gcc/fortran/resolve.cc
@@ -8089,6 +8089,7 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
if (!t)
goto failure;
+ code->ext.alloc.expr3_not_explicit = 0;
if (e->ts.type == BT_CLASS && CLASS_DATA (e)->attr.dimension
&& !code->expr3 && code->ext.alloc.ts.type == BT_DERIVED)
{
@@ -8097,6 +8098,7 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
when the allocated type is different from the declared type but
no SOURCE exists by setting expr3. */
code->expr3 = gfc_default_initializer (&code->ext.alloc.ts);
+ code->ext.alloc.expr3_not_explicit = 1;
}
else if (flag_coarray != GFC_FCOARRAY_LIB && e->ts.type == BT_DERIVED
&& e->ts.u.derived->from_intmod == INTMOD_ISO_FORTRAN_ENV
@@ -8104,6 +8106,7 @@ resolve_allocate_expr (gfc_expr *e, gfc_code *code, bool *array_alloc_wo_spec)
{
/* We have to zero initialize the integer variable. */
code->expr3 = gfc_get_int_expr (gfc_default_integer_kind, &e->where, 0);
+ code->ext.alloc.expr3_not_explicit = 1;
}
if (e->ts.type == BT_CLASS && !unlimited && !UNLIMITED_POLY (code->expr3))
diff --git a/gcc/fortran/trans-stmt.cc b/gcc/fortran/trans-stmt.cc
index f788754..776f98d 100644
--- a/gcc/fortran/trans-stmt.cc
+++ b/gcc/fortran/trans-stmt.cc
@@ -6458,12 +6458,15 @@ gfc_trans_allocate (gfc_code * code)
/* Deallocate any allocatable components in expressions that use a
temporary object, i.e. are not a simple alias of to an EXPR_VARIABLE.
E.g. temporaries of a function call need freeing of their components
- here. */
+ here. Explicit derived type allocation of class entities uses expr3
+ to carry the default initializer. This must not be deallocated or
+ finalized. */
if ((code->expr3->ts.type == BT_DERIVED
|| code->expr3->ts.type == BT_CLASS)
&& (code->expr3->expr_type != EXPR_VARIABLE || temp_obj_created)
&& code->expr3->ts.u.derived->attr.alloc_comp
- && !code->expr3->must_finalize)
+ && !code->expr3->must_finalize
+ && !code->ext.alloc.expr3_not_explicit)
{
tmp = gfc_deallocate_alloc_comp (code->expr3->ts.u.derived,
expr3, code->expr3->rank);