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

--- Comment #6 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The releases/gcc-16 branch has been updated by Paul Thomas <[email protected]>:

https://gcc.gnu.org/g:38b61f4fee1e5ca8ac64dd1c9a077e20b09e09ec

commit r16-9541-g38b61f4fee1e5ca8ac64dd1c9a077e20b09e09ec
Author: Christopher Albert <[email protected]>
Date:   Sun Aug 16 13:34:44 2026 +0100

    Fortran: [13-17 regression] Fix duplicated finalization [PR110626]

    For a derived-type intrinsic assignment with a component defined
    assignment, generate_component_assignments emitted a whole-derived-type
    assignment and a defined-assignment call on a temporary copy of the old
    lhs. The whole assignment finalized the lhs. The INTENT(OUT) argument
    then finalized the stale temporary instead of the real component, so both
    finalizations saw the old value.

    For a scalar lhs without pointer or allocatable components, mark the
    whole-derived-type assignment finalize_only. It finalizes the lhs
    without copying the structure, then assigns each component separately.
    The component defined assignment acts on the real component and its
    INTENT(OUT) finalization sees the value left by the first finalization.

    Array lhs assignments remain on the existing path because their
    component-wise expansion needs scalarization. The allocatable-component
    case remains PR fortran/57696.

    Assisted-by: GPT-5.6-sol (OpenAI)
    Assisted-by: Claude (Anthropic)

    2026-08-16  Christopher Albert  <[email protected]>

    gcc/fortran
            PR fortran/110626
            * gfortran.h : Add finalize_only bitfield to gfc_expr.
            * resolve.cc (generate_component_assignments): If the lhs has
            no pointer or allocatable components, set use_finalize_only to
            false. Otherwise it equals finalizable_lhs. Use the value to
            set the expr1 finalize_only field and add the assignment to the
            chain. If use_finalize_only is false, free the statements and
            transfer the value to the final result.
            * trans-expr.cc (gfc_trans_assignment_1): If expr1 is finalize_
            only emit an empty statement.

    gcc/testsuite/
            PR fortran/110626
            * gfortran.dg/finalize_62.f90: New test.

    Signed-off-by: Christopher Albert <[email protected]>
    (cherry picked from commit a03222414043b7155a76f7e564fb688ac5ac7bb6)

Reply via email to