https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128856
Bug ID: 128856
Summary: unction result passed as actual argument not finalized
(in expressions, or always if the type has a component
with defined assignment)
Product: gcc
Version: 17.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: fortran
Assignee: unassigned at gcc dot gnu.org
Reporter: abensonca at gcc dot gnu.org
Target Milestone: ---
F2018 7.5.6.3, paragraph 5: "If an executable construct references a nonpointer
function, the result is finalized after execution of the innermost executable
construct containing the reference."
gfortran does not finalize a function result that is passed directly as an
actual argument in some contexts. For a type with a final procedure
(fnresult_final_contexts.f90), counting final calls after each statement:
statement gfortran 13.2 .. trunk ifx 2024.0
s = get(make(1)) 1 1
s = 1 + get(make(1)) 0 1
call sub(make(1)) 1 1
x = pass(make(1)) 3 3
if (get(make(1)) /= 1) ... 0 1
print *, get(make(1)) 1 1
so a reference in an operand of an expression, or in an IF condition, is never
finalized.
If the type of the result has a component with a type-bound defined assignment
(fnresult_final_defassign.f90: the same program, with "generic :: assignment(=)
=> asg" added to the finalizable component type), the result is not finalized
in any of these contexts:
statement gfortran 13.2 .. trunk ifx 2024.0
s = get(make(1)) 0 1
s = 1 + get(make(1)) 0 1
call sub(make(1)) 0 1
x = pass(make(1)) 3 4
if (get(make(1)) /= 1) ... 0 1
print *, get(make(1)) 0 1
The second case looks like the early return in gfc_finalize_tree_expr
(trans.cc), which skips function results of a type with defined_assign_comp on
the grounds that generate_component_assignments handles them; that is only true
when the result is the rhs of an intrinsic assignment.
With a final procedure that releases memory (as in the reproducers), each
missed finalization is a leak: valgrind reports one block per loop iteration
for "s = s + get(make(i))".