From patchwork Fri May 26 07:36:22 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: =?utf-8?q?Marc_Poulhi=C3=A8s?= X-Patchwork-Id: 70144 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 B8FDE3945C0F for ; Fri, 26 May 2023 07:44:20 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B8FDE3945C0F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1685087060; bh=hj5OQfjqgMGQGaNxTIIv5fQCUkGQvxH1bnVH1mVGS5g=; h=To:Cc:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=g+Lb4LnMq7Y82B3wr9fYafKM554xMBgcsbjWw0ljfaq4s2MbyipCtV47TdGEQTZt9 D046u8pFe0jgIz7ogpSzlIA3C6JGvjk2j5RVtX6igPRpCGle9yf4sgZrIE2uzpK2cd O60U2XmFWGpDLsG5ZH7oDmdaIh97jBp5WJbqPivI= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by sourceware.org (Postfix) with ESMTPS id 9A8B73836EA4 for ; Fri, 26 May 2023 07:36:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 9A8B73836EA4 Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-3f6cbdf16d2so2687375e9.2 for ; Fri, 26 May 2023 00:36:25 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1685086584; x=1687678584; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=hj5OQfjqgMGQGaNxTIIv5fQCUkGQvxH1bnVH1mVGS5g=; b=VGNS1Na5QD4sQWzUri8s3sXHPXFm2byAuRPeEQUObGEedm7wPa5rqYCe8bzibTak75 d1rD5Et4Qonbh0RjwlI1l2ziEQkdq+3Z3RE3m5Q1U1GeLO3syZqBnokc16t5da9bO4UL YNyj+JLkGX8iF+vIpGXCLZFXlarUqZzsVCTX/Mu/6XUbcRSl76jEVyIMm7iRdfTvY+A4 xYt2V2TtF2LRnhcVkkf/nbAyk1AN9/YGln8Jl55Uj/14NjOJWrAwIJNMi84ut5cl2/OX gQFH/vk5WbRBZLbmgkqcZKckitinSpLiZfzKGWZqh040IH4C0edSRqoTrdSntFAvxgJB sziA== X-Gm-Message-State: AC+VfDzxRs8xnVcWkYr8yCCwkuJmAYFo3ixi7ZOjD4HkklhgkZWSOgh4 36e57oCWTffnIvT9E/CFoQVIXnh019FfCSdaljWa/Q== X-Google-Smtp-Source: ACHHUZ6NivGXCcGPdODv8rBXAZ2LUXi8sKqXepMEsFBTDDgUtLJhl9rtkD4Zhy8dIkEOKQpvwOVcHA== X-Received: by 2002:a1c:ed0b:0:b0:3f6:3bc:8562 with SMTP id l11-20020a1ced0b000000b003f603bc8562mr679062wmh.1.1685086584387; Fri, 26 May 2023 00:36:24 -0700 (PDT) Received: from localhost.localdomain ([2001:861:3382:1a90:777b:eef4:6f79:f26f]) by smtp.gmail.com with ESMTPSA id z1-20020a05600c220100b003f508115b25sm4396123wml.4.2023.05.26.00.36.23 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 26 May 2023 00:36:23 -0700 (PDT) To: gcc-patches@gcc.gnu.org Cc: Eric Botcazou Subject: [COMMITTED] ada: Reject thin 'Unrestricted_Access value to aliased constrained array Date: Fri, 26 May 2023 09:36:22 +0200 Message-Id: <20230526073622.2069006-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_ASCII_DIVIDERS, 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.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Marc_Poulhi=C3=A8s_via_Gcc-patches?= From: =?utf-8?q?Marc_Poulhi=C3=A8s?= Reply-To: =?utf-8?q?Marc_Poulhi=C3=A8s?= Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" From: Eric Botcazou This rejects the Unrestricted_Access attribute applied to an aliased array with a constrained nominal subtype when its type is resolved to be a thin pointer. The reason is that supporting this case would require the aliased array to contain its bounds, and this is the case only for aliased arrays whose nominal subtype is unconstrained. gcc/ada/ * sem_attr.adb (Is_Thin_Pointer_To_Unc_Array): New predicate. (Resolve_Attribute): Apply the static matching legality rule to an Unrestricted_Access attribute applied to an aliased prefix if the type is a thin pointer. Call Is_Thin_Pointer_To_Unc_Array for the aliasing legality rule as well. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/sem_attr.adb | 74 ++++++++++++++++++++++++++++++-------------- 1 file changed, 51 insertions(+), 23 deletions(-) diff --git a/gcc/ada/sem_attr.adb b/gcc/ada/sem_attr.adb index efea03670c3..39103279fa7 100644 --- a/gcc/ada/sem_attr.adb +++ b/gcc/ada/sem_attr.adb @@ -10982,6 +10982,9 @@ package body Sem_Attr is -- Returns True if Declared_Entity is declared within the declarative -- region of Generic_Unit; otherwise returns False. + function Is_Thin_Pointer_To_Unc_Array (T : Entity_Id) return Boolean; + -- Return True if T is a thin pointer to an unconstrained array type + ---------------------------------- -- Declared_Within_Generic_Unit -- ---------------------------------- @@ -11009,6 +11012,28 @@ package body Sem_Attr is return False; end Declared_Within_Generic_Unit; + ---------------------------------- + -- Is_Thin_Pointer_To_Unc_Array -- + ---------------------------------- + + function Is_Thin_Pointer_To_Unc_Array (T : Entity_Id) return Boolean is + begin + if Is_Access_Type (T) + and then Has_Size_Clause (T) + and then RM_Size (T) = System_Address_Size + then + declare + DT : constant Entity_Id := Designated_Type (T); + + begin + return Is_Array_Type (DT) and then not Is_Constrained (DT); + end; + + else + return False; + end if; + end Is_Thin_Pointer_To_Unc_Array; + -- Start of processing for Resolve_Attribute begin @@ -11484,9 +11509,7 @@ package body Sem_Attr is end if; end if; - if Attr_Id in Attribute_Access | Attribute_Unchecked_Access - and then (Ekind (Btyp) = E_General_Access_Type - or else Ekind (Btyp) = E_Anonymous_Access_Type) + if Ekind (Btyp) in E_General_Access_Type | E_Anonymous_Access_Type then -- Ada 2005 (AI-230): Check the accessibility of anonymous -- access types for stand-alone objects, record and array @@ -11494,6 +11517,7 @@ package body Sem_Attr is -- the level is the same of the enclosing composite type. if Ada_Version >= Ada_2005 + and then Attr_Id = Attribute_Access and then (Is_Local_Anonymous_Access (Btyp) -- Handle cases where Btyp is the anonymous access @@ -11501,7 +11525,6 @@ package body Sem_Attr is or else Nkind (Associated_Node_For_Itype (Btyp)) = N_Object_Declaration) - and then Attr_Id = Attribute_Access -- Verify that static checking is OK (namely that we aren't -- in a specific context requiring dynamic checks on @@ -11540,7 +11563,9 @@ package body Sem_Attr is end if; end if; - if Is_Dependent_Component_Of_Mutable_Object (P) then + if Attr_Id /= Attribute_Unrestricted_Access + and then Is_Dependent_Component_Of_Mutable_Object (P) + then Error_Msg_F ("illegal attribute for discriminant-dependent component", P); @@ -11555,7 +11580,19 @@ package body Sem_Attr is Nom_Subt := Base_Type (Nom_Subt); end if; - if Is_Tagged_Type (Designated_Type (Typ)) then + -- We do not enforce static matching for Unrestricted_Access + -- except for a thin pointer to an unconstrained array type, + -- because, in this case, the designated object must contain + -- its bounds, which means that it must have an unconstrained + -- nominal subtype (and be aliased, as will be checked below). + + if Attr_Id = Attribute_Unrestricted_Access + and then not (Is_Thin_Pointer_To_Unc_Array (Typ) + and then Is_Aliased_View (Original_Node (P))) + then + null; + + elsif Is_Tagged_Type (Designated_Type (Typ)) then -- If the attribute is in the context of an access -- parameter, then the prefix is allowed to be of @@ -11665,8 +11702,9 @@ package body Sem_Attr is Compatible_Alt_Checks : constant Boolean := No_Dynamic_Acc_Checks and then not Debug_Flag_Underscore_B; + begin - if Attr_Id /= Attribute_Unchecked_Access + if Attr_Id = Attribute_Access and then (Ekind (Btyp) = E_General_Access_Type or else No_Dynamic_Acc_Checks) @@ -11856,22 +11894,12 @@ package body Sem_Attr is -- Check for unrestricted access where expected type is a thin -- pointer to an unconstrained array. - elsif Has_Size_Clause (Typ) - and then RM_Size (Typ) = System_Address_Size - then - declare - DT : constant Entity_Id := Designated_Type (Typ); - begin - if Is_Array_Type (DT) - and then not Is_Constrained (DT) - then - Error_Msg_N - ("illegal use of Unrestricted_Access attribute", P); - Error_Msg_N - ("\attempt to generate thin pointer to unaliased " - & "object", P); - end if; - end; + elsif Is_Thin_Pointer_To_Unc_Array (Typ) then + Error_Msg_N + ("illegal use of Unrestricted_Access attribute", P); + Error_Msg_N + ("\attempt to generate thin pointer to unaliased " + & "object", P); end if; end if;