From patchwork Mon Sep 5 07:26:06 2022 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: 976 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp58278wrt; Mon, 5 Sep 2022 00:34:45 -0700 (PDT) X-Google-Smtp-Source: AA6agR7BxM0WsmjUfPLoNVT0Dbtpp9uFmfLnPaBJb1aW65Xw0dVRQjnAAXy7WarUp/4m5y3hOv0p X-Received: by 2002:a05:6402:ea1:b0:443:d90a:5d31 with SMTP id h33-20020a0564020ea100b00443d90a5d31mr42876922eda.121.1662363284992; Mon, 05 Sep 2022 00:34:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1662363284; cv=none; d=google.com; s=arc-20160816; b=KD56JGnZYuFrUcYjnzLelYEduGly1bTIfl+w4qOTEcpMGauhosXQFt7eZvjCCBbv9R ExAPUIpBIK0I3YLprELXXv/q1YTL61itLct01zIH1+gqRHmq1hN6ZP11qI2KhwO8cqow 20Vli8Oe7pPcU4ESCnneMjbqrPuhdLSFtNPYfbVDyr+gnM/R/spfyT8s+mMgY7bBSdSY 4ClrXiPsLaV+on5PHrpxkZm+rLJogQ4zcNghvFWjrwu/VgL0VFavtYueX88fErYYZzI/ 6BDFTKbtV5wSXipBPg9TVZpKlbC3Bcszo3mlyKbeELC3dBKC2t1VraTrJPIOlexgEBnD smuQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:cc:reply-to:from:list-subscribe:list-help :list-post:list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=l2v8nq1fQVC8j38z0VpRvoM9/KAn3e9TNpYD7X2c5kI=; b=uToNPSet2asKowuN1ct/fd3KTTswjQuD95LOjrFW+dH8s02f0k+6r4fHu7Bk70/282 /pvbVnFwlHZxJDgeqg5XJ3/DQ/5WQyVB0E5D4PNQ9Ztx+UojrKm3TZqKjmoGZwmdEVtE Wfg7cTUhbLKgt+f825zocdDEY3kZD9mK6lZ//0sCTn0qhJUW5n/wfvW7lX4nqBfHvU2s N6WYIKRTURFCGNZwHU2LZPNZTt2j4NHU3o8+XxHcSvF/G3sZFK4Edzo4Tj4sbmfPtH9t Lzu0D1ipT0EtJwSF6H3iSyyFfLcpcy+qFvxqGIzXlEy34G1nDKyKQGfsfQNmwgrzPd9F AJVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="EkWz/dE6"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id hd35-20020a17090796a300b0072a72bb6f80si6575744ejc.744.2022.09.05.00.34.44 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Sep 2022 00:34:44 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b="EkWz/dE6"; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 89398380DBF0 for ; Mon, 5 Sep 2022 07:29:59 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 89398380DBF0 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1662362999; bh=l2v8nq1fQVC8j38z0VpRvoM9/KAn3e9TNpYD7X2c5kI=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=EkWz/dE6QVouSNJUYO+Z9pxMJWNOuF4SCr5Jz6BiIgB719S4O5dtgru1klEwquRsH drciw04JoiMQGOjViB+opAg95WLvr4X0K0P4ZBw9lhKsfuo+MnmbVZBLtdmXsx8Ups QGkgl7OqcfJzKvwUmPIvWw9EBFKWz84xQ1ZlHfNU= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x430.google.com (mail-wr1-x430.google.com [IPv6:2a00:1450:4864:20::430]) by sourceware.org (Postfix) with ESMTPS id 771023856DF5 for ; Mon, 5 Sep 2022 07:26:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 771023856DF5 Received: by mail-wr1-x430.google.com with SMTP id f15so5318380wrw.3 for ; Mon, 05 Sep 2022 00:26:08 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-disposition:mime-version:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date; bh=l2v8nq1fQVC8j38z0VpRvoM9/KAn3e9TNpYD7X2c5kI=; b=garvjav/fpW8JAokhC1xv2JHPrjnkmo3psuegmzWC0CanvDQn3wh1T7oCxEyfqWiq3 LWJGCiuJ1B+QeRIKpw5mYrpxGjTTeJ8ga2ZR9NElmPeerEqnbzdkYZejzIRo/JlwRI9M 1HJHI+5M/DeKUe50EWbT7IaTp861mMfkLXd/6TKXve4pH/rxJS9w5Ebr3pffxA3vLy7w GXegpkU7aApzgkmIGoeP5f5wDEyQCcNFx9oGtoHQOh1+Fpx+AUKMIVAv9oQcbYY6Odqt OLKL3+QJUTDjA8pZAhaD5d1aNwv6LacZQdXE+hoXlbPMj0BgvIAIbueUVvtQrJ9fSEG1 3+Rg== X-Gm-Message-State: ACgBeo0GcS21JgbdRHv2bopCiRcsA3QxEmhEaehUXWu4k4JaSCYUDtic SpWqS6knVI17SEg6gI5r97YxMqO3gPaSlw== X-Received: by 2002:a5d:47ca:0:b0:220:5cbc:1c59 with SMTP id o10-20020a5d47ca000000b002205cbc1c59mr23453054wrc.662.1662362768106; Mon, 05 Sep 2022 00:26:08 -0700 (PDT) Received: from poulhies-Precision-5550 (static-176-191-105-132.ftth.abo.bbox.fr. [176.191.105.132]) by smtp.gmail.com with ESMTPSA id n6-20020a05600c4f8600b003a54d610e5fsm17186119wmq.26.2022.09.05.00.26.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 05 Sep 2022 00:26:07 -0700 (PDT) Date: Mon, 5 Sep 2022 09:26:06 +0200 To: gcc-patches@gcc.gnu.org Subject: [Ada] Fix bogus discriminant check failure for type with predicate Message-ID: <20220905072606.GA1174730@poulhies-Precision-5550> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-12.6 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 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: Marc =?iso-8859-1?q?Poulhi=E8s?= Cc: Eric Botcazou Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743114243622249449?= X-GMAIL-MSGID: =?utf-8?q?1743114243622249449?= This reorders the processing in Freeze_Entity_Checks so that building the predicate functions, which first requires building discriminated checking functions for record types with a variant part, is done after processing and checking this variant part. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_ch13.adb (Freeze_Entity_Checks): Build predicate functions only after checking the variant part of a record type, if any. diff --git a/gcc/ada/sem_ch13.adb b/gcc/ada/sem_ch13.adb --- a/gcc/ada/sem_ch13.adb +++ b/gcc/ada/sem_ch13.adb @@ -12901,139 +12901,6 @@ package body Sem_Ch13 is Inside_Freezing_Actions := Inside_Freezing_Actions - 1; - -- If we have a type with predicates, build predicate function. This is - -- not needed in the generic case, nor within e.g. TSS subprograms and - -- other predefined primitives. For a derived type, ensure that the - -- parent type is already frozen so that its predicate function has been - -- constructed already. This is necessary if the parent is declared - -- in a nested package and its own freeze point has not been reached. - - if Is_Type (E) - and then Nongeneric_Case - and then Has_Predicates (E) - and then Predicate_Check_In_Scope (N) - then - declare - Atyp : constant Entity_Id := Nearest_Ancestor (E); - begin - if Present (Atyp) - and then Has_Predicates (Atyp) - and then not Is_Frozen (Atyp) - then - Freeze_Before (N, Atyp); - end if; - end; - - -- Before we build a predicate function, ensure that discriminant - -- checking functions are available. The predicate function might - -- need to call these functions if the predicate references - -- any components declared in a variant part. - if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then - Build_Or_Copy_Discr_Checking_Funcs (Parent (E)); - end if; - - Build_Predicate_Function (E, N); - end if; - - -- If type has delayed aspects, this is where we do the preanalysis at - -- the freeze point, as part of the consistent visibility check. Note - -- that this must be done after calling Build_Predicate_Function or - -- Build_Invariant_Procedure since these subprograms fix occurrences of - -- the subtype name in the saved expression so that they will not cause - -- trouble in the preanalysis. - - -- This is also not needed in the generic case - - if Nongeneric_Case - and then Has_Delayed_Aspects (E) - and then Scope (E) = Current_Scope - then - declare - Ritem : Node_Id; - - begin - -- Look for aspect specification entries for this entity - - Ritem := First_Rep_Item (E); - while Present (Ritem) loop - if Nkind (Ritem) = N_Aspect_Specification - and then Entity (Ritem) = E - and then Is_Delayed_Aspect (Ritem) - then - if Get_Aspect_Id (Ritem) in Aspect_CPU - | Aspect_Dynamic_Predicate - | Aspect_Predicate - | Aspect_Static_Predicate - | Aspect_Priority - then - -- Retrieve the visibility to components and discriminants - -- in order to properly analyze the aspects. - - Push_Type (E); - Check_Aspect_At_Freeze_Point (Ritem); - - -- In the case of predicate aspects, there will be - -- a corresponding Predicate pragma associated with - -- the aspect, and the expression of the pragma also - -- needs to be analyzed at this point, to ensure that - -- Save_Global_References will capture global refs in - -- expressions that occur in generic bodies, for proper - -- later resolution of the pragma in instantiations. - - if Is_Type (E) - and then Inside_A_Generic - and then Has_Predicates (E) - and then Present (Aspect_Rep_Item (Ritem)) - then - declare - Pragma_Args : constant List_Id := - Pragma_Argument_Associations - (Aspect_Rep_Item (Ritem)); - Pragma_Expr : constant Node_Id := - Expression (Next (First (Pragma_Args))); - begin - if Present (Pragma_Expr) then - Analyze_And_Resolve - (Pragma_Expr, Standard_Boolean); - end if; - end; - end if; - - Pop_Type (E); - - else - Check_Aspect_At_Freeze_Point (Ritem); - end if; - - -- A pragma Predicate should be checked like one of the - -- corresponding aspects, wrt possible misuse of ghost - -- entities. - - elsif Nkind (Ritem) = N_Pragma - and then No (Corresponding_Aspect (Ritem)) - and then - Get_Pragma_Id (Pragma_Name (Ritem)) = Pragma_Predicate - then - -- Retrieve the visibility to components and discriminants - -- in order to properly analyze the pragma. - - declare - Arg : constant Node_Id := - Next (First (Pragma_Argument_Associations (Ritem))); - begin - Push_Type (E); - Preanalyze_Spec_Expression - (Expression (Arg), Standard_Boolean); - Pop_Type (E); - end; - end if; - - Next_Rep_Item (Ritem); - end loop; - end; - - end if; - -- For a record type, deal with variant parts. This has to be delayed to -- this point, because of the issue of statically predicated subtypes, -- which we have to ensure are frozen before checking choices, since we @@ -13199,6 +13066,140 @@ package body Sem_Ch13 is end Check_Variant_Part; end if; + -- If we have a type with predicates, build predicate function. This is + -- not needed in the generic case, nor within e.g. TSS subprograms and + -- other predefined primitives. For a derived type, ensure that the + -- parent type is already frozen so that its predicate function has been + -- constructed already. This is necessary if the parent is declared + -- in a nested package and its own freeze point has not been reached. + + if Is_Type (E) + and then Nongeneric_Case + and then Has_Predicates (E) + and then Predicate_Check_In_Scope (N) + then + declare + Atyp : constant Entity_Id := Nearest_Ancestor (E); + + begin + if Present (Atyp) + and then Has_Predicates (Atyp) + and then not Is_Frozen (Atyp) + then + Freeze_Before (N, Atyp); + end if; + end; + + -- Before we build a predicate function, ensure that discriminant + -- checking functions are available. The predicate function might + -- need to call these functions if the predicate references any + -- components declared in a variant part. + + if Ekind (E) = E_Record_Type and then Has_Discriminants (E) then + Build_Or_Copy_Discr_Checking_Funcs (Parent (E)); + end if; + + Build_Predicate_Function (E, N); + end if; + + -- If type has delayed aspects, this is where we do the preanalysis at + -- the freeze point, as part of the consistent visibility check. Note + -- that this must be done after calling Build_Predicate_Function or + -- Build_Invariant_Procedure since these subprograms fix occurrences of + -- the subtype name in the saved expression so that they will not cause + -- trouble in the preanalysis. + + -- This is also not needed in the generic case + + if Nongeneric_Case + and then Has_Delayed_Aspects (E) + and then Scope (E) = Current_Scope + then + declare + Ritem : Node_Id; + + begin + -- Look for aspect specification entries for this entity + + Ritem := First_Rep_Item (E); + while Present (Ritem) loop + if Nkind (Ritem) = N_Aspect_Specification + and then Entity (Ritem) = E + and then Is_Delayed_Aspect (Ritem) + then + if Get_Aspect_Id (Ritem) in Aspect_CPU + | Aspect_Dynamic_Predicate + | Aspect_Predicate + | Aspect_Static_Predicate + | Aspect_Priority + then + -- Retrieve the visibility to components and discriminants + -- in order to properly analyze the aspects. + + Push_Type (E); + Check_Aspect_At_Freeze_Point (Ritem); + + -- In the case of predicate aspects, there will be + -- a corresponding Predicate pragma associated with + -- the aspect, and the expression of the pragma also + -- needs to be analyzed at this point, to ensure that + -- Save_Global_References will capture global refs in + -- expressions that occur in generic bodies, for proper + -- later resolution of the pragma in instantiations. + + if Is_Type (E) + and then Inside_A_Generic + and then Has_Predicates (E) + and then Present (Aspect_Rep_Item (Ritem)) + then + declare + Pragma_Args : constant List_Id := + Pragma_Argument_Associations + (Aspect_Rep_Item (Ritem)); + Pragma_Expr : constant Node_Id := + Expression (Next (First (Pragma_Args))); + begin + if Present (Pragma_Expr) then + Analyze_And_Resolve + (Pragma_Expr, Standard_Boolean); + end if; + end; + end if; + + Pop_Type (E); + + else + Check_Aspect_At_Freeze_Point (Ritem); + end if; + + -- A pragma Predicate should be checked like one of the + -- corresponding aspects, wrt possible misuse of ghost + -- entities. + + elsif Nkind (Ritem) = N_Pragma + and then No (Corresponding_Aspect (Ritem)) + and then + Get_Pragma_Id (Pragma_Name (Ritem)) = Pragma_Predicate + then + -- Retrieve the visibility to components and discriminants + -- in order to properly analyze the pragma. + + declare + Arg : constant Node_Id := + Next (First (Pragma_Argument_Associations (Ritem))); + begin + Push_Type (E); + Preanalyze_Spec_Expression + (Expression (Arg), Standard_Boolean); + Pop_Type (E); + end; + end if; + + Next_Rep_Item (Ritem); + end loop; + end; + end if; + if not In_Generic_Scope (E) and then Ekind (E) = E_Record_Type and then Is_Tagged_Type (E)