]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Fix premature finalization caused by predicate check on aggregate component
authorEric Botcazou <ebotcazou@adacore.com>
Mon, 24 Nov 2025 17:08:10 +0000 (18:08 +0100)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Fri, 9 Jan 2026 10:57:12 +0000 (11:57 +0100)
The predicate check may cause the creation of a temporary when it is applied
to a function call and the temporary will be finalized, so any assignment of
the temporary must be followed by an adjustment of the target.

gcc/ada/ChangeLog:

* exp_ch5.adb (Expand_N_Assignment_Statement): If a predicate check
made on the RHS forced the capture of a function call to remove its
side effects, demote No_Ctrl_Actions into No_Finalize_Actions on the
N_Assignment_Statement node.

gcc/ada/exp_ch5.adb

index d9ef17ffabc258149c0c457410d196c26cc830bf..6424f0a1d556166ebde81af0030cae645be611fa 100644 (file)
@@ -2527,6 +2527,20 @@ package body Exp_Ch5 is
             end if;
 
             Apply_Predicate_Check (Rhs, Typ);
+
+            --  If the generation of the check required capturing a function
+            --  call to remove its side effects, and the assignment initially
+            --  was to be done without controlled actions, then change it to
+            --  be done without finalization only, in other words restore the
+            --  adjustment of the LHS, because the RHS is now a temporary that
+            --  will be finalized after the assignment is complete.
+
+            if No_Ctrl_Actions (N)
+              and then Is_Captured_Function_Call (Rhs)
+            then
+               Set_No_Ctrl_Actions (N, False);
+               Set_No_Finalize_Actions (N, True);
+            end if;
          end if;
       end if;