]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Excess finalization on return of call to dispatching constructor
authorGary Dismukes <dismukes@adacore.com>
Thu, 4 Jan 2024 00:34:33 +0000 (00:34 +0000)
committerMarc Poulhiès <poulhies@adacore.com>
Mon, 6 May 2024 09:11:32 +0000 (11:11 +0200)
The compiler expands a too-early finalization call for the result
object of an extended return statement that returns a call to a
dispatching constructor function for a limited interface type,
resulting in premature (and extra) finalization of the result.
The temporary object that the compiler creates to hold the result
of the build-in-place call loses the fact that it comes from a
return, and the wrong BIP allocation form may be passed in the
call to the dispatching constructor, and the later code for dealing
with finalization in Exp_Ch7.Build_Finalizer incorrectly finalizes
the result object.

gcc/ada/

* exp_ch6.adb
(Make_Build_In_Place_Iface_Call_In_Object_Declaration): Set the
Is_Return_Object flag on the entity of the temp object created to
hold the BIP call result, from the flag on the passed-in object
declaration's entity. Update copyright notice to 2024.

gcc/ada/exp_ch6.adb

index fcfd1d7f0bf1f53f42e31ac784b9513be0bf2c38..a89c9af0bb22cdc30d57747e46c851585a89c143 100644 (file)
@@ -9393,6 +9393,16 @@ package body Exp_Ch6 is
       Insert_Action (Obj_Decl, Tmp_Decl);
       Expander_Mode_Restore;
 
+      --  Inherit Is_Return_Object from the parent object to the temp object,
+      --  so that Make_In_Build_Place_Call_In_Object_Declaration will handle
+      --  the temp properly in cases where there's a BIP_Alloc_Form formal of
+      --  an enclosing function that should be passed along (and which also
+      --  ensures that if the BIP call is used as a function result and it
+      --  requires finalization, then it will not be finalized prematurely
+      --  or redundantly).
+
+      Set_Is_Return_Object (Tmp_Id, Is_Return_Object (Obj_Id));
+
       Make_Build_In_Place_Call_In_Object_Declaration
         (Obj_Decl      => Tmp_Decl,
          Function_Call => Expression (Tmp_Decl));