From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x329.google.com (mail-wm1-x329.google.com [IPv6:2a00:1450:4864:20::329]) by sourceware.org (Postfix) with ESMTPS id D1FAE3882172 for ; Thu, 13 Jun 2024 13:33:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D1FAE3882172 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org D1FAE3882172 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::329 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718285635; cv=none; b=RD+Z4k0hPFahnszImt8PNiFX2fNxUeFNIqi/S4rx+tt/Qu8Q7Wj5HhqM0Uxsuw0HrsSWl4SAHf+6w25V+2Pww2r9+L2rWmG9eGtXargIf7tWKK7T/l1iLSAdeza6IabzuUEivf0vlnK9vOtljOK5Mknvk11r5jxqMvGEaeZdY4M= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1718285635; c=relaxed/simple; bh=mUIYY6jrkf2ZKzir0FLtslwvxBGdZ3gYDXSbk+lq/MU=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=PbiMcSTJGaeoDybyKDhLsZk5EkVzzQDSxA47cmeVP6xQ6/YhOtNq2yyrMGlACp60Ttw3t8QhWkpYjYovo3fjtBTs2xHN8ZG0VrrfJ6p6t14ImcyqvuI1rw4BO+5qlOCUZqcZ2Ej/ZHzXFoTCZycvA4FYurTLhd4QLB7KembaGXg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x329.google.com with SMTP id 5b1f17b1804b1-42172ed3597so6750515e9.0 for ; Thu, 13 Jun 2024 06:33:52 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1718285631; x=1718890431; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=QE+eywf9bNEMJR4EGHJbmTxMGdthoxizyKK9ZBCsHtc=; b=DjW2yz0H3Brlc2jNyx/+2yIxODOwZIbsh/HXOr5fmgMzAuRRnvwfPnipB9LNTQsjYK 9cVAi7BMj2YQ1kVa0u/MtE6P7rBebBbw9Nv5OiKj1ZT4o9FUs363gtHg8/QAFibIYvE5 +SmRKnOEICrOCpHJSeEslGXVSgpxt+CiFQuWPbxoCWWAB/wraC8e4F/Z3H7gMIyEJeCo NTKFBpdp4nBJnC3MOYJKs0W+jxfWJ6t4vZALjDuv+CUyRxxSUUh1AuOyXWvUkKUyfJYK KIxj/LBOoKs3s6SvLUNLNITcbsjZd6MigvNNAz7CAG0SepGNg1wJJays0F7GYYfpA+Ec cb9g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1718285631; x=1718890431; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=QE+eywf9bNEMJR4EGHJbmTxMGdthoxizyKK9ZBCsHtc=; b=R000NHbtPBR5w7MyQEoFZNmqWEmLf7DS+zu5OpOGikFzm0jpOO/x2mjB+T1oLOdUMl jKkdw5dhwpFahqzxyzuXVBnH3zpR4K6nE8ydJUBJTobd3rVDBPx/sBWiL54B1TsXKb4V TjV/f1yXn3JJeO/ZYplTMDDXkbdjKWsEWkn/agWMn9HOEMx39g3zqrnKNUAo7dIwlPMv mwSxLV8eoZkRDswc9ahuHwLhABM1LmITZfOjkP6LCF2LkHu3GEOUAmXlVA22hNBjkY8m UGszU3+LPy6TjfHSPS0NVA4L+qDFA9O62Dhs0BM7iOd6ku8pN14a3CAaba0ei/WFcHqV eTIQ== X-Gm-Message-State: AOJu0Yx+FRg+zHqtWFwCRqzOuauq4Gk+X+zL23K9QF8Nxr6SsLSxqcYh /LsU8OhJq4A2fF0JjeD0wORBx5AuCAfMuw7yB/y4tmQFxZMr0Fbk95vtDhATNiMmzIYVMHBfdJM = X-Google-Smtp-Source: AGHT+IG5WfkE33d7WZ9jD4f/y7Ms7v+ldizjTdcbtMJDFDh+lZC9wC1aCNLl3eS8XPJP5AWm9my2Jg== X-Received: by 2002:a05:600c:1c19:b0:421:805f:ab3c with SMTP id 5b1f17b1804b1-422b88b6808mr29997695e9.14.1718285631251; Thu, 13 Jun 2024 06:33:51 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-422870e9145sm62391955e9.22.2024.06.13.06.33.50 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 13 Jun 2024 06:33:50 -0700 (PDT) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED 02/30] ada: Fix too late finalization of temporary object Date: Thu, 13 Jun 2024 15:33:08 +0200 Message-ID: <20240613133338.1809385-2-poulhies@adacore.com> X-Mailer: git-send-email 2.45.1 In-Reply-To: <20240613133338.1809385-1-poulhies@adacore.com> References: <20240613133338.1809385-1-poulhies@adacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.7 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Eric Botcazou The problem is that Is_Finalizable_Transient returns false when a transient object is subject to a renaming by another transient object present in the same transient scope, thus forcing its finalization to be deferred to the enclosing scope. That's not necessary, as only renamings by nontransient objects serviced by transient scopes need to be rejected by the predicate. The change also removes now dead code in the finalization machinery. gcc/ada/ PR ada/114710 * exp_ch7.adb (Build_Finalizer.Process_Declarations): Remove dead code dealing with renamings. * exp_util.ads (Is_Finalizable_Transient): Rename Rel_Node to N. * exp_util.adb (Is_Finalizable_Transient): Likewise. (Is_Aliased): Remove obsolete code dealing wih EWA nodes and only consider renamings present in N itself. (Requires_Cleanup_Actions): Remove dead code dealing with renamings. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch7.adb | 20 -------- gcc/ada/exp_util.adb | 116 ++++++++++++++++--------------------------- gcc/ada/exp_util.ads | 10 ++-- 3 files changed, 48 insertions(+), 98 deletions(-) diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb index fd1d9db0654..3583ed3138f 100644 --- a/gcc/ada/exp_ch7.adb +++ b/gcc/ada/exp_ch7.adb @@ -2477,26 +2477,6 @@ package body Exp_Ch7 is Processing_Actions (Decl, Is_Protected => True); end if; - -- Specific cases of object renamings - - elsif Nkind (Decl) = N_Object_Renaming_Declaration then - Obj_Id := Defining_Identifier (Decl); - Obj_Typ := Base_Type (Etype (Obj_Id)); - - -- Bypass any form of processing for objects which have their - -- finalization disabled. This applies only to objects at the - -- library level. - - if For_Package and then Finalize_Storage_Only (Obj_Typ) then - null; - - -- Ignored Ghost object renamings do not need any cleanup - -- actions because they will not appear in the final tree. - - elsif Is_Ignored_Ghost_Entity (Obj_Id) then - null; - end if; - -- Inspect the freeze node of an access-to-controlled type and -- look for a delayed finalization collection. This case arises -- when the freeze actions are inserted at a later time than the diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb index 654ea7d9124..6ad464e6701 100644 --- a/gcc/ada/exp_util.adb +++ b/gcc/ada/exp_util.adb @@ -8646,8 +8646,8 @@ package body Exp_Util is ------------------------------ function Is_Finalizable_Transient - (Decl : Node_Id; - Rel_Node : Node_Id) return Boolean + (Decl : Node_Id; + N : Node_Id) return Boolean is Obj_Id : constant Entity_Id := Defining_Identifier (Decl); Obj_Typ : constant Entity_Id := Base_Type (Etype (Obj_Id)); @@ -8889,61 +8889,53 @@ package body Exp_Util is -- Start of processing for Is_Aliased begin - -- A controlled transient object is not considered aliased when it - -- appears inside an expression_with_actions node even when there are - -- explicit aliases of it: - - -- do - -- Trans_Id : Ctrl_Typ ...; -- transient object - -- Alias : ... := Trans_Id; -- object is aliased - -- Val : constant Boolean := - -- ... Alias ...; -- aliasing ends - -- -- object safe to finalize - -- in Val end; - - -- Expansion ensures that all aliases are encapsulated in the actions - -- list and do not leak to the expression by forcing the evaluation - -- of the expression. - - if Nkind (Rel_Node) = N_Expression_With_Actions then - return False; - - -- Otherwise examine the statements after the controlled transient - -- object and look for various forms of aliasing. - - else - Stmt := First_Stmt; - while Present (Stmt) loop - if Nkind (Stmt) = N_Object_Declaration then - Expr := Expression (Stmt); + -- Examine the statements following the controlled object and look + -- for various forms of aliasing. + + Stmt := First_Stmt; + while Present (Stmt) loop + -- Transient objects initialized by a reference are finalized + -- (see Initialized_By_Reference above), so we must make sure + -- not to finalize the referenced object twice. And we cannot + -- finalize it at all if it is referenced by the nontransient + -- object serviced by the transient scope. + + if Nkind (Stmt) = N_Object_Declaration then + Expr := Expression (Stmt); + + -- Aliasing of the form: + -- Obj : ... := Trans_Id'reference; + + if Present (Expr) + and then Nkind (Expr) = N_Reference + and then Is_Entity_Name (Prefix (Expr)) + and then Entity (Prefix (Expr)) = Trans_Id + then + return True; + end if; - -- Aliasing of the form: - -- Obj : ... := Trans_Id'reference; + -- (Transient) renamings are never finalized so we need not bother + -- about finalizing transient renamed objects twice. Therefore, we + -- we only need to look at the nontransient object serviced by the + -- transient scope, if it exists and is declared as a renaming. - if Present (Expr) - and then Nkind (Expr) = N_Reference - and then Nkind (Prefix (Expr)) = N_Identifier - and then Entity (Prefix (Expr)) = Trans_Id - then - return True; - end if; - - elsif Nkind (Stmt) = N_Object_Renaming_Declaration then - Ren_Obj := Find_Renamed_Object (Stmt); + elsif Nkind (Stmt) = N_Object_Renaming_Declaration + and then Stmt = N + then + Ren_Obj := Find_Renamed_Object (Stmt); - -- Aliasing of the form: - -- Obj : ... renames ... Trans_Id ...; + -- Aliasing of the form: + -- Obj : ... renames ... Trans_Id ...; - if Present (Ren_Obj) and then Ren_Obj = Trans_Id then - return True; - end if; + if Present (Ren_Obj) and then Ren_Obj = Trans_Id then + return True; end if; + end if; - Next (Stmt); - end loop; + Next (Stmt); + end loop; - return False; - end if; + return False; end Is_Aliased; -------------------------- @@ -9161,8 +9153,8 @@ package body Exp_Util is return Ekind (Obj_Id) in E_Constant | E_Variable and then Needs_Finalization (Desig) - and then Nkind (Rel_Node) /= N_Simple_Return_Statement - and then not Is_Part_Of_BIP_Return_Statement (Rel_Node) + and then Nkind (N) /= N_Simple_Return_Statement + and then not Is_Part_Of_BIP_Return_Statement (N) -- Do not consider a transient object that was already processed @@ -13488,26 +13480,6 @@ package body Exp_Util is return True; end if; - -- Specific cases of object renamings - - elsif Nkind (Decl) = N_Object_Renaming_Declaration then - Obj_Id := Defining_Identifier (Decl); - Obj_Typ := Base_Type (Etype (Obj_Id)); - - -- Bypass any form of processing for objects which have their - -- finalization disabled. This applies only to objects at the - -- library level. - - if Lib_Level and then Finalize_Storage_Only (Obj_Typ) then - null; - - -- Ignored Ghost object renamings do not need any cleanup actions - -- because they will not appear in the final tree. - - elsif Is_Ignored_Ghost_Entity (Obj_Id) then - null; - end if; - -- Inspect the freeze node of an access-to-controlled type and look -- for a delayed finalization collection. This case arises when the -- freeze actions are inserted at a later time than the expansion of diff --git a/gcc/ada/exp_util.ads b/gcc/ada/exp_util.ads index 3c7e70ed13b..8d64b11d750 100644 --- a/gcc/ada/exp_util.ads +++ b/gcc/ada/exp_util.ads @@ -764,12 +764,10 @@ package Exp_Util is -- Rnn.all function Is_Finalizable_Transient - (Decl : Node_Id; - Rel_Node : Node_Id) return Boolean; - -- Determine whether declaration Decl denotes a controlled transient which - -- should be finalized. Rel_Node is the related context. Even though some - -- transients are controlled, they may act as renamings of other objects or - -- function calls. + (Decl : Node_Id; + N : Node_Id) return Boolean; + -- Determine whether declaration Decl denotes a controlled transient object + -- that must be finalized. N is the node serviced by the transient context. function Is_Fully_Repped_Tagged_Type (T : Entity_Id) return Boolean; -- Tests given type T, and returns True if T is a non-discriminated tagged -- 2.45.1