https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128856
--- Comment #2 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The master branch has been updated by Jerry DeLisle <[email protected]>: https://gcc.gnu.org/g:284b847b3856e7d35949c6e5cdefbe3cfab8021a commit r17-6566-g284b847b3856e7d35949c6e5cdefbe3cfab8021a Author: Jerry DeLisle <[email protected]> Date: Sat Nov 21 19:30:41 2026 -0800 fortran: [PR128856] Finalize function results passed as arguments A nonpointer function result passed as an actual argument was not finalized when the call was an operand or in a condition, and never when its type had a component with a defined assignment, although F2023 7.5.6.3 requires it. gfc_conv_procedure_call collects the finalization of such results in the finalblock of the enclosing call, but only assignments, subroutine calls and I/O emit that block; operators, intrinsic functions and conditions drop it. gfc_finalize_tree_expr also skips every function result of a type with a defined-assignment component, although generate_component_assignments handles only a result that is the right-hand side of an intrinsic assignment. For a function result not returned by reference, move the finalization of the arguments to the front of the post block, which every enclosing expression keeps. Count the nesting of actual argument evaluation in actual_arg_level and apply the skip only outside it. move_alloc_20.f03 counted only the finalization in the assignment inside GET_U; it now also counts that of its result. Assisted-by: Claude Opus 5.5 PR fortran/128856 gcc/fortran/ChangeLog: * trans-expr.cc (gfc_conv_procedure_call): Increment actual_arg_level while evaluating the arguments. For a function result not returned by reference, move the finalization of the arguments to the post block. * trans.cc (actual_arg_level): New. (gfc_finalize_tree_expr): Do not skip a type with a defined-assignment component for an actual argument. * trans.h (actual_arg_level): Declare. gcc/testsuite/ChangeLog: * gfortran.dg/move_alloc_20.f03: Count the finalization of the result of GET_U. * gfortran.dg/finalize_65.f90: New test.
