From: Eric Botcazou <[email protected]>

The recent finalization fix made for limited types has uncovered cases where
the object returned by calls to build-in-place functions was not finalized
in selected anonymous contexts, most notably the dependent expressions of
conditional expressions.  The specific finalization machinery that handles
conditional expressions requires the temporaries built for their dependent
expressions to be visible as early as possible, and this was not the case.

gcc/ada/ChangeLog:

        * exp_ch4.adb (Expand_N_Case_Expression): When not optimizing for a
        specific context, call Make_Build_In_Place_Call_In_Anonymous_Context
        on expressions of alternatives when they are calls to BIP functions.
        (Expand_N_If_Expression): Likewise for the Then & Else expressions.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_ch4.adb | 24 ++++++++++++++++++++++--
 1 file changed, 22 insertions(+), 2 deletions(-)

diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb
index 7c90aa0d1b8..520ab683a6e 100644
--- a/gcc/ada/exp_ch4.adb
+++ b/gcc/ada/exp_ch4.adb
@@ -5368,7 +5368,17 @@ package body Exp_Ch4 is
          --  When the alternative's expression involves controlled function
          --  calls, generated temporaries are chained on the corresponding
          --  list of actions. These temporaries need to be finalized after
-         --  the case expression is evaluated.
+         --  the case expression is evaluated. We first need to make them
+         --  explicit for build-in-place functions in anonymous contexts,
+         --  because calls to these do not go through Expand_Ctrl_Actions.
+
+         if Is_Build_In_Place_Function_Call (Expression (Alt))
+           and then not Optimize_Assignment_Stmt
+           and then not Optimize_Return_Stmt
+           and then not Optimize_Object_Decl
+         then
+            Make_Build_In_Place_Call_In_Anonymous_Context (Expression (Alt));
+         end if;
 
          Process_Transients_In_Expression (N, Actions (Alt));
 
@@ -6345,7 +6355,17 @@ package body Exp_Ch4 is
          --  When the "then" or "else" expressions involve controlled function
          --  calls, generated temporaries are chained on the corresponding list
          --  of actions. These temporaries need to be finalized after the if
-         --  expression is evaluated.
+         --  expression is evaluated. We first need to make them explicit for
+         --  build-in-place functions in anonymous contexts, because calls to
+         --  these do not go through Expand_Ctrl_Actions.
+
+         if Is_Build_In_Place_Function_Call (Thenx) then
+            Make_Build_In_Place_Call_In_Anonymous_Context (Thenx);
+         end if;
+
+         if Is_Build_In_Place_Function_Call (Elsex) then
+            Make_Build_In_Place_Call_In_Anonymous_Context (Elsex);
+         end if;
 
          Process_Transients_In_Expression (N, Then_Actions (N));
          Process_Transients_In_Expression (N, Else_Actions (N));
-- 
2.51.0

Reply via email to