https://gcc.gnu.org/bugzilla/show_bug.cgi?id=102287

anlauf at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |anlauf at gcc dot gnu.org

--- Comment #2 from anlauf at gcc dot gnu.org ---
We need to wrap the deallocation of INTENT(out) allocatable components into
a test for presence, like:

diff --git a/gcc/fortran/trans-expr.c b/gcc/fortran/trans-expr.c
index 18d665192f0..076cffdd77f 100644
--- a/gcc/fortran/trans-expr.c
+++ b/gcc/fortran/trans-expr.c
@@ -6548,6 +6548,19 @@ gfc_conv_procedure_call (gfc_se * se, gfc_symbol * sym,
                    // deallocate the components first
                    tmp = gfc_deallocate_alloc_comp (fsym->ts.u.derived,
                                                     parmse.expr, e->rank);
+                   /* But check whether dummy argument is optional.  */
+                   if (tmp != NULL_TREE
+                       && fsym->attr.optional
+                       && e->expr_type == EXPR_VARIABLE
+                       && e->symtree->n.sym->attr.optional)
+                     {
+                       tree present, notpres;
+                       present = gfc_conv_expr_present (e->symtree->n.sym);
+                       notpres = build_empty_stmt (input_location);
+                       tmp = fold_build3_loc (input_location, COND_EXPR,
+                                              void_type_node,
+                                              present, tmp, notpres);
+                       }
                    if (tmp != NULL_TREE)
                      gfc_add_expr_to_block (&se->pre, tmp);
                  }

Reply via email to