https://gcc.gnu.org/bugzilla/show_bug.cgi?id=118856
--- Comment #10 from GCC Commits <cvs-commit at gcc dot gnu.org> --- The trunk branch has been updated by Jason Merrill <ja...@gcc.gnu.org>: https://gcc.gnu.org/g:e96e1bb69c7b46db18e747ee379a62681bc8c82d commit r15-7532-ge96e1bb69c7b46db18e747ee379a62681bc8c82d Author: Jason Merrill <ja...@redhat.com> Date: Fri Feb 14 10:53:01 2025 +0100 c++: extended temp cleanups [PR118856] A later testcase in PR118856 highlights a preexisting problem with multiple reference-extended temporaries in a single declaration; if initializing a later one throws, the cleanup for the earlier one is not in scope yet. Let's deal with this by keeping a dummy TARGET_EXPR to hold the EH cleanup until all other initialization is complete. See the comment for various other considered approaches. We now avoid extending TARGET_EXPRs with CLEANUP_EH_ONLY set; all such TARGET_EXPRs were already only internal iterator/flag variables that don't want to be extended, as they are dead after initialization is complete even if other temporaries are extended. But some other internal temporaries did not have the flag set because they don't have TARGET_EXPR_CLEANUP; I introduce a get_internal_target_expr function to set the flag rather than directly set the flag (and add a comment) in such places. The places changed to call get_internal_target_expr either already set the flag, or have no cleanup at all. PR c++/118856 gcc/cp/ChangeLog: * call.cc (set_up_extended_ref_temp): Retain a TARGET_EXPR for cleanups if something later in initialization throws. (extend_temps_r): Don't extend eliding or EH-only TARGET_EXPRs. * cp-tree.h (get_internal_target_expr): Declare. * tree.cc (get_internal_target_expr): New. * decl.cc (cp_finish_decomp, expand_static_init): Use it. * except.cc (build_throw): Likewise. * init.cc (build_new_1, build_vec_init, build_delete): Likewise. (build_vec_delete): Likewise. * typeck2.cc (maybe_push_temp_cleanup): Likewise. gcc/testsuite/ChangeLog: * g++.dg/eh/ref-temp3.C: New test. * g++.dg/eh/ref-temp4.C: New test.