From patchwork Thu Jan 6 17:12:59 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Pierre-Marie de Rodat X-Patchwork-Id: 1576198 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: bilbo.ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=sWug2O+z; dkim-atps=neutral Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by bilbo.ozlabs.org (Postfix) with ESMTPS id 4JVCs35qtcz9sPC for ; Fri, 7 Jan 2022 04:24:38 +1100 (AEDT) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B38EA3858416 for ; Thu, 6 Jan 2022 17:24:36 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B38EA3858416 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1641489876; bh=YH1ikx/4sTd9WwhJSmq4Um3XHqP2cgtgZfrDc7g8aEk=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:Cc:From; b=sWug2O+zGzUVTWZpTnM+UdXl2/GgoYYhF4zF2sWfp8JnHbhaCNCqtSMQ3Uo2fAgWs fmfuPeQWa3ekSbvuqVwJEXJ0jdAnV11761Btud0y8hQyJJxOr3S9a52EWuRO/Fi6hO GVoynfHhzUA0nNtm2hq2514/1N9Wa2Prw/FA+EjE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32f.google.com (mail-wm1-x32f.google.com [IPv6:2a00:1450:4864:20::32f]) by sourceware.org (Postfix) with ESMTPS id 4819F3858033 for ; Thu, 6 Jan 2022 17:13:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 4819F3858033 Received: by mail-wm1-x32f.google.com with SMTP id v10-20020a05600c214a00b00345e59928eeso4063172wml.0 for ; Thu, 06 Jan 2022 09:13:02 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition; bh=YH1ikx/4sTd9WwhJSmq4Um3XHqP2cgtgZfrDc7g8aEk=; b=NetYO12SL1hWK721F8mbAnKD6uqMCs2R8If6jhKoM+zEV8X0FPrYQNS4kzc7Za78RE lB7L/REX2rsiKGNWNITAVYOuu8bi+/5luIR+k0vPu5WJcXI06zkx3bF4q5EhmTXzA6IY 9uWxyR6NU+RJK4A3wzMqcbu+wDQdeQjPANJsknoBQ3mt/E9zBzk+NgVVkH0Tncpfjpgg MP2duRKhUrcP2FIcvKWP/qNBqZi93ekNyCtNEMRKqkS0st2Oyn79tiJ9F0UEP/fDg5pZ 5ojCrb9UnxRJCtjD/zYSZdf/1u8mgu+u2EyWZ2b/1IbZCfr1eKXnCDxtnNwhWmJiuoa6 RFuA== X-Gm-Message-State: AOAM531c5VoAyr6wu6YOSzsyq+vjQ99+xv2rvP9MKv7zUwLyPdFpYkNP aR8R80ZIcEVCWJHs/UmsR2Frn3ta5ZEdAA== X-Google-Smtp-Source: ABdhPJxq22Jxu5OrjWtt0XdAqbtD6TwX5CYbOPs7ZEUZ1+cYP3B3UTPWCD7qxbPT5EZPVkmlZKlTig== X-Received: by 2002:a05:600c:1c18:: with SMTP id j24mr7911212wms.189.1641489181355; Thu, 06 Jan 2022 09:13:01 -0800 (PST) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id r1sm2922115wrz.30.2022.01.06.09.13.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 06 Jan 2022 09:13:00 -0800 (PST) Date: Thu, 6 Jan 2022 17:12:59 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Spurious error when using current instance of type Message-ID: <20220106171259.GA2921550@adacore.com> MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-13.1 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 autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) 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: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Cc: Justin Squirek Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch fixes an issue in the compiler whereby it fails to recognize the presence of a current instance of an incomplete type when the instance is used within a default expression for a record component. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * exp_ch3.adb (Build_Assignment): Replace current instance of type with Init_Proc formal. * sem_attr.adb (OK_Self_Reference): Handle recognition of Current_Instance to detect certain expansion. * sem_ch4.adb (Analyze_One_Call): Set actual's type when the actual in question is a current instance and its corresponding formal is an incomplete type. * sem_util.adb (Is_Current_Instance): Add check for incomplete views and add comment. diff --git a/gcc/ada/exp_ch3.adb b/gcc/ada/exp_ch3.adb --- a/gcc/ada/exp_ch3.adb +++ b/gcc/ada/exp_ch3.adb @@ -2022,6 +2022,25 @@ package body Exp_Ch3 is Elmt2 => Defining_Identifier (First (Parameter_Specifications (Parent (Proc_Id))))); + + -- If the type has an incomplete view, a current instance + -- may have an incomplete type. In that case, it must also be + -- replaced by the formal of the Init_Proc. + + if Nkind (Parent (Rec_Type)) = N_Full_Type_Declaration + and then Present (Incomplete_View (Parent (Rec_Type))) + then + Append_Elmt ( + N => Defining_Identifier + (Incomplete_View (Parent (Rec_Type))), + To => Map); + Append_Elmt ( + N => Defining_Identifier + (First + (Parameter_Specifications + (Parent (Proc_Id)))), + To => Map); + end if; end if; Exp := New_Copy_Tree (Exp, New_Scope => Proc_Id, Map => Map); diff --git a/gcc/ada/sem_attr.adb b/gcc/ada/sem_attr.adb --- a/gcc/ada/sem_attr.adb +++ b/gcc/ada/sem_attr.adb @@ -989,7 +989,15 @@ package body Sem_Attr is Set_Etype (P, Typ); end if; - if Typ = Scop then + -- A current instance typically appears immediately within + -- the type declaration, but may be nested within an internally + -- generated temporary scope - as for an aggregate of a + -- discriminated component. + + if Typ = Scop + or else (In_Open_Scopes (Typ) + and then not Comes_From_Source (Scop)) + then declare Q : Node_Id := Parent (N); diff --git a/gcc/ada/sem_ch4.adb b/gcc/ada/sem_ch4.adb --- a/gcc/ada/sem_ch4.adb +++ b/gcc/ada/sem_ch4.adb @@ -3727,6 +3727,24 @@ package body Sem_Ch4 is Next_Actual (Actual); Next_Formal (Formal); + -- A current instance used as an actual of a function, + -- whose body has not been seen, may include a formal + -- whose type is an incomplete view of an enclosing + -- type declaration containing the current call (e.g. + -- in the Expression for a component declaration). + + -- In this case, update the signature of the subprogram + -- so the formal has the type of the full view. + + elsif Inside_Init_Proc + and then Nkind (Actual) = N_Identifier + and then Ekind (Etype (Formal)) = E_Incomplete_Type + and then Etype (Actual) = Full_View (Etype (Formal)) + then + Set_Etype (Formal, Etype (Actual)); + Next_Actual (Actual); + Next_Formal (Formal); + -- Handle failed type check else diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb --- a/gcc/ada/sem_util.adb +++ b/gcc/ada/sem_util.adb @@ -16680,7 +16680,18 @@ package body Sem_Util is | N_Private_Type_Declaration | N_Subtype_Declaration and then Comes_From_Source (P) - and then Defining_Entity (P) = Typ + + -- If the type has a previous incomplete declaration, the + -- reference in the type definition may have the incomplete + -- view. So, here we detect if this incomplete view is a current + -- instance by checking if its full view is the entity of the + -- full declaration begin analyzed. + + and then + (Defining_Entity (P) = Typ + or else + (Ekind (Typ) = E_Incomplete_Type + and then Full_View (Typ) = Defining_Entity (P))) then return True;