From a87bf1d20a37bb69c9fa6d2211ffd963aa69240d Mon Sep 17 00:00:00 2001 From: Paul Thomas Date: Sun, 15 Dec 2024 10:42:34 +0000 Subject: Fortran: Pointer fcn results must not be finalized [PR117897] 2024-12-15 Paul Thomas gcc/fortran PR fortran/117897 * trans-expr.cc (gfc_trans_assignment_1): RHS pointer function results must not be finalized. gcc/testsuite/ PR fortran/117897 * gfortran.dg/finalize_59.f90: New test. --- gcc/fortran/trans-expr.cc | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'gcc/fortran/trans-expr.cc') diff --git a/gcc/fortran/trans-expr.cc b/gcc/fortran/trans-expr.cc index 3718b0e..44a50c0 100644 --- a/gcc/fortran/trans-expr.cc +++ b/gcc/fortran/trans-expr.cc @@ -12596,13 +12596,20 @@ gfc_trans_assignment_1 (gfc_expr * expr1, gfc_expr * expr2, bool init_flag, && (expr2->ts.type == BT_CLASS || gfc_may_be_finalized (expr2->ts))) { expr2->must_finalize = 1; + /* F2023 7.5.6.3: If an executable construct references a nonpointer + function, the result is finalized after execution of the innermost + executable construct containing the reference. */ + if (expr2->expr_type == EXPR_FUNCTION + && (gfc_expr_attr (expr2).pointer + || (expr2->ts.type == BT_CLASS && CLASS_DATA (expr2)->attr.class_pointer))) + expr2->must_finalize = 0; /* F2008 4.5.6.3 para 5: If an executable construct references a structure constructor or array constructor, the entity created by the constructor is finalized after execution of the innermost executable construct containing the reference. These finalizations were later deleted by the Combined Techical Corrigenda 1 TO 4 for fortran 2008 (f08/0011). */ - if (gfc_notification_std (GFC_STD_F2018_DEL) + else if (gfc_notification_std (GFC_STD_F2018_DEL) && (expr2->expr_type == EXPR_STRUCTURE || expr2->expr_type == EXPR_ARRAY)) expr2->must_finalize = 0; -- cgit v1.1