From patchwork Mon Feb 26 12:24:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Botcazou X-Patchwork-Id: 86375 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 45E303858415 for ; Mon, 26 Feb 2024 12:25:54 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32a.google.com (mail-wm1-x32a.google.com [IPv6:2a00:1450:4864:20::32a]) by sourceware.org (Postfix) with ESMTPS id 2C04A3858C39 for ; Mon, 26 Feb 2024 12:24:57 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2C04A3858C39 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 2C04A3858C39 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32a ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708950299; cv=none; b=Zyl9HYTP8XGhJaQ9e/l2+p4qyKAJo/6/z9O18Pt8nrQF4icz2LBX9DcF5iMfxZ02S1jNKbW+FfDwrGQnagUzGGuN6HGmVmYaTF59LX6PaSF7tJT/RJ4RkkfvzTLSYfqpJ8JJLGmBsIlIOC6YqeoeooIV+96VCK4qC83ZfAZLVjk= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708950299; c=relaxed/simple; bh=u33DDUKUl0Ai4ncGzIsSyjofJuQwDc4goPF0jLSEzcs=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=QHS+VfYudQ1smqTJd2Qgkq7ANsQH44ADcZT6prYhoHREWCGgOwhEWTrO8sTJA72TfFPBUTlO1NqjLkzMpbrTu69EJ7Fw6Wj6TZB8Xbs5b/Cquy88kqSkmpOsUKrdcrKcX0OnvZHnZFlfLSmgjZQmnhAh+gvTveEE0u5VrGOgWZE= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32a.google.com with SMTP id 5b1f17b1804b1-412a14299a4so11070725e9.1 for ; Mon, 26 Feb 2024 04:24:57 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1708950296; x=1709555096; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:from:to:cc:subject:date:message-id:reply-to; bh=dcDHEVt9Stl6VaeLd/HLojJGnHtFEC4zPrtZb22dHjM=; b=aHJV2IegOM5UeTlJpBlggwKCvj34PphnjSmqOrqnYPTKXblR+Q21VWo3tRmCJIaDBN jpnHCYgEcZPTk06ERgq4YhUxjZR+YdgO2zpV6h38OTQfjyfVYSW770r7m53LUI162ZeE j5SUHiIk55UOtMsk6gOV3E9XNR+mK2J0FtATbUjSVIgZ2JcVH/x0fcfBOsKShW4EscCP LlFd7UWL4HqMVXTht3Gwnjye7630UUrxT1kXLBnAwzEpr+sPqEQQDoYmOSKNIfdj0I05 p6kZOazGEcrMzPGJEBasM8GTv7m9QkisqPzHcqx+a6BG5MQc/Ke6PkgqxEkoaaW/U257 Ozng== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708950296; x=1709555096; h=content-transfer-encoding:mime-version:message-id:date:subject:to :from:x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=dcDHEVt9Stl6VaeLd/HLojJGnHtFEC4zPrtZb22dHjM=; b=jvJPuGXCGjj2Ljpgf+UtFrJoysVJ8c5FdmCiV/5NKF8J+RWoTGJ3hOWkCEKMrobkJi 3Sh/qNvO5tQ4Rs9hhEKelgJnINPPmDOwPt7U6m5JBhXKY9NBywZUoZieKtXLcMs4MuRx +T+F/PWj73hz7dZezcMiCu2fd1gOOWwL5BmBVrRdjW0h06b3w27AE/L9aOOYZ0EpOeCx YEUUOFmlKVPROxAXv0hfgCEuM69+5kmJi0YxRSiOjuZ9YZQX7O5yVk50Oyk0PG1cRuyW LXqMm4jLvvZVYArZI6/svznPFym2AIjF2X5oT08NBXubzIbkLxos6SrCU3hlNcKRnlTT CaOg== X-Gm-Message-State: AOJu0YzQ+axDe/J1h3scdiU5W53/NUlheCM6XUqyPWPuMO23rKEnlz9s VH+g6GtssyUHtwiaad6fBO/j7dVT9CScsaGtvKenq8gQKO1k3C8Y/JgKr2XWedUua3LJHwPvAJ8 = X-Google-Smtp-Source: AGHT+IE8eGGGb59QysCBCOhG7BuhwYZdRQhLVqXVk71sne0OrAR6PyYbOPjZzew2USAI40iwaW5h5g== X-Received: by 2002:a05:600c:4f52:b0:412:8872:e8f4 with SMTP id m18-20020a05600c4f5200b004128872e8f4mr5326459wmq.1.1708950295234; Mon, 26 Feb 2024 04:24:55 -0800 (PST) Received: from fomalhaut.localnet ([2a01:e0a:8d5:d990:e654:e8ff:fe8f:2ce6]) by smtp.gmail.com with ESMTPSA id fa13-20020a05600c518d00b00412a7cf0308sm1609375wmb.3.2024.02.26.04.24.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 26 Feb 2024 04:24:54 -0800 (PST) From: Eric Botcazou X-Google-Original-From: Eric Botcazou To: gcc-patches@gcc.gnu.org Subject: [Ada] Fix PR ada/113893 Date: Mon, 26 Feb 2024 13:24:54 +0100 Message-ID: <3290278.44csPzL39Z@fomalhaut> MIME-Version: 1.0 X-Spam-Status: No, score=-10.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_NUMSUBJECT, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org The finalization of objects dynamically allocated through an anonymous access type is deferred to the enclosing library unit in the current implementation and a warning is given on each of them. However this cannot be done if the designated type is local, because this would generate dangling references to the local finalization routine, so the finalization needs to be dropped in this case and the warning adjusted. Tested on x86-64/Linux, applied on all active branches. 2024-02-26 Eric Botcazou PR ada/113893 * exp_ch7.adb (Build_Anonymous_Master): Do not build the master for a local designated type. * exp_util.adb (Build_Allocate_Deallocate_Proc): Force Needs_Fin to false if no finalization master is attached to an access type and assert that it is anonymous in this case. * sem_res.adb (Resolve_Allocator): Mention that the object might not be finalized at all in the warning given when the type is an anonymous access-to-controlled type. 2024-02-26 Eric Botcazou * gnat.dg/access10.adb: New test. diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb index 2ac73101351..e594a534244 100644 --- a/gcc/ada/exp_ch7.adb +++ b/gcc/ada/exp_ch7.adb @@ -749,6 +749,7 @@ package body Exp_Ch7 is Desig_Typ : Entity_Id; FM_Id : Entity_Id; Priv_View : Entity_Id; + Scop : Entity_Id; Unit_Decl : Node_Id; Unit_Id : Entity_Id; @@ -787,6 +788,18 @@ package body Exp_Ch7 is Desig_Typ := Priv_View; end if; + -- For a designated type not declared at library level, we cannot create + -- a finalization collection attached to an outer unit since this would + -- generate dangling references to the dynamic scope through access-to- + -- procedure values designating the local Finalize_Address primitive. + + Scop := Enclosing_Dynamic_Scope (Desig_Typ); + if Scop /= Standard_Standard + and then Scope_Depth (Scop) > Scope_Depth (Unit_Id) + then + return; + end if; + -- Determine whether the current semantic unit already has an anonymous -- master which services the designated type. diff --git a/gcc/ada/exp_util.adb b/gcc/ada/exp_util.adb index 31cd47de7d2..04d114694ab 100644 --- a/gcc/ada/exp_util.adb +++ b/gcc/ada/exp_util.adb @@ -936,6 +936,16 @@ package body Exp_Util is Needs_Finalization (Desig_Typ) and then not No_Heap_Finalization (Ptr_Typ); + -- The allocation/deallocation of a controlled object must be associated + -- with an attachment to/detachment from a finalization master, but the + -- implementation cannot guarantee this property for every anonymous + -- access tyoe, see Build_Anonymous_Collection. + + if Needs_Fin and then No (Finalization_Master (Ptr_Typ)) then + pragma Assert (Ekind (Ptr_Typ) = E_Anonymous_Access_Type); + Needs_Fin := False; + end if; + if Needs_Fin then -- Do nothing if the access type may never allocate / deallocate @@ -945,11 +955,6 @@ package body Exp_Util is return; end if; - -- The allocation / deallocation of a controlled object must be - -- chained on / detached from a finalization master. - - pragma Assert (Present (Finalization_Master (Ptr_Typ))); - -- The only other kind of allocation / deallocation supported by this -- routine is on / from a subpool. diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb index 8e9714c1c86..075c0d85ccd 100644 --- a/gcc/ada/sem_res.adb +++ b/gcc/ada/sem_res.adb @@ -5679,19 +5679,19 @@ package body Sem_Res is Set_Is_Dynamic_Coextension (N, False); Set_Is_Static_Coextension (N, False); - -- Anonymous access-to-controlled objects are not finalized on - -- time because this involves run-time ownership and currently - -- this property is not available. In rare cases the object may - -- not be finalized at all. Warn on potential issues involving - -- anonymous access-to-controlled objects. + -- Objects allocated through anonymous access types are not + -- finalized on time because this involves run-time ownership + -- and currently this property is not available. In rare cases + -- the object might not be finalized at all. Warn on potential + -- issues involving anonymous access-to-controlled types. if Ekind (Typ) = E_Anonymous_Access_Type and then Is_Controlled_Active (Desig_T) then Error_Msg_N - ("??object designated by anonymous access object might " + ("??object designated by anonymous access value might " & "not be finalized until its enclosing library unit " - & "goes out of scope", N); + & "goes out of scope, or not be finalized at all", N); Error_Msg_N ("\use named access type instead", N); end if; end if;