Hi All, Happy new year! This thread broke off in February last year, as did my effort to resolve all the issues. However, prodded by Damian, I picked up the mantle again about a month ago. Please consider this posting to be a placeholder. All the dependencies of PR37366 appear to be fixed although some minor issues remain and some divergences with the other brands. I will be contacting the vendors of the other brands today or tomorrow and will try to achieve some resolution with them. In the meantime, I will break the patch down to half a dozen more digestible chunks and will aim to submit formally in a week or so. Of the remaining issues: Function results of finalizable type with zero components confound the gimplifier: see PR65347 comment 3. finalize_38.f90 loses 38 bytes in 4 blocks and has a load of invalid writes. finalize_49.f90 has a number of invalid writes. Please give the patch a whirl and any feedback that you might have would be very welcome. Cheers Paul Fortran:Implement missing finalization features [PR37336] 2022-02-02 Paul Thomas gcc/fortran PR fortran/103854 * class.cc (has_finalizer_component): Do not return true for procedure pointer components. PR fortran/96122 * class.cc (finalize_component): Include the missing arguments in the call to the component's finalizer wrapper. PR fortran/37336 * class.cc (finalizer_insert_packed_call): Remove the redundant argument in the call to the final subroutine. (generate_finalization_wrapper): Add support for assumed rank finalizers. (gfc_may_be_finalized): New helper function. * dump_parse_tree.cc (show_expr): Mark expressions with must_finalize set. * gfortran.h : Add prototype for gfc_may_be_finalized. * resolve.cc (resolve_where, gfc_resolve_where_code_in_forall, gfc_resolve_forall_body, gfc_resolve_code): Check that the op code is still EXEC_ASSIGN. If it is set lhs to must finalize. (generate_component_assignments): Set must_finalize if needed. (gfc_resolve_finalizers): Error if assumed rank finalizer is not the only one. Warning on lack of scalar finalizer modified to account for assumed rank finalizers. (resolve_symbol): Set referenced an unreferenced symbol that will be finalized. * trans-array.cc (gfc_trans_array_constructor_value): Add code to finalize the constructor result. Warn that this feature was removed in F2018 and that it is suppressed by -std=2018. (trans_array_constructor): Add finalblock, pass to previous and apply to loop->post if filled. (gfc_add_loop_ss_code): Add se finalblock to outer loop post. (gfc_trans_array_cobounds, gfc_trans_array_bounds): Add any generated finalization code to the main block. (structure_alloc_comps): Add boolean argument to suppress finalization and use it for calls from gfc_deallocate_alloc_comp_no_caf. Otherwise it defaults to false. Add a second, additional boolean argument to nullify pointer components and use it in gfc_copy_alloc_comp_del_ptrs. (gfc_copy_alloc_comp_del_ptrs): New wrapper for structure_alloc_comps. (gfc_alloc_allocatable_for_assignment): Suppress finalization by setting new arg in call to gfc_deallocate_alloc_comp_no_caf. (gfc_trans_deferred_array): Use gfc_may_be_finalized. * trans-array.h : Add the new boolean argument to the prototype of gfc_deallocate_alloc_comp_no_caf with a default of false. Add prototype for gfc_copy_alloc_comp_del_ptrs. * trans-decl.cc (gfc_get_symbol_decl): Make sure that temporary variables from resolve.cc are not finalized by detection of a leading '_' in the symbol name. (init_intent_out_dt): Tidy up the code. * trans-expr.cc (gfc_init_se): Initialize finalblock. (finalize_function_result): New function that finalizes function results in the correct order. (gfc_conv_procedure_call): Use new function for finalizable function results. Replace in-line block for class results with call to new function. (gfc_conv_expr): Finalize structure constructors for F2003 and F2008. Warn that this feature was deleted in F2018 and, unlike array constructors, is not default. Add array constructor finalblock to the post block. (gfc_trans_scalar_assign): Suppress finalization by setting new argument in call to gfc_deallocate_alloc_comp_no_caf. Add the finalization blocks to the main block. (gfc_assignment_finalizer_call): New function to provide finalization on intrinsic assignment. (gfc_trans_arrayfunc_assign): Use the previous and ensure that finalization occurs after the evaluation of the rhs but must use the initial value for the lhs. (trans_class_assignment, gfc_trans_assignment_1): Call it and add the block between the rhs evaluation and any reallocation on assignment that there might be. * trans-io.cc (gfc_trans_transfer): Add the final block. * trans-stmt.cc (gfc_trans_call, gfc_trans_allocate): likewise. * trans.h: Add finalblock to gfc_se. Add the prototype for gfc_finalize_function_result. gcc/testsuite/ PR fortran/64290 * gfortran.dg/finalize_38.f90 : New test. * gfortran.dg/allocate_with_source_25.f90 : The number of final calls goes down from 6 to 4. * gfortran.dg/unlimited_polymorphic_8.f90 : Tree dump reveals foo.1.x rather than foo.0.x PR fortran/67444 * gfortran.dg/finalize_39.f90 : New test. PR fortran/67471 * gfortran.dg/finalize_40.f90 : New test. PR fortran/69298 PR fortran/70863 * gfortran.dg/finalize_41.f90 : New test. PR fortran/71798 * gfortran.dg/finalize_42.f90 : New test. PR fortran/80524 * gfortran.dg/finalize_43.f90 : New test. PR fortran/82996 * gfortran.dg/finalize_44.f90 : New test. PR fortran/84472 * gfortran.dg/finalize_45.f90 : New test. PR fortran/88735 PR fortran/93691 * gfortran.dg/finalize_46.f90 : New test. PR fortran/91316 * gfortran.dg/finalize_47.f90 : New test. PR fortran/106576 * gfortran.dg/finalize_48.f90 : New test. PR fortran/91316 * gfortran.dg/finalize_47.f90 : New test.