From patchwork Tue Dec 19 14:30:05 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: 82486 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 6A0C1386D638 for ; Tue, 19 Dec 2023 14:30:33 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x32b.google.com (mail-wm1-x32b.google.com [IPv6:2a00:1450:4864:20::32b]) by sourceware.org (Postfix) with ESMTPS id E7456385DC11 for ; Tue, 19 Dec 2023 14:30:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E7456385DC11 Authentication-Results: sourceware.org; dmarc=pass (p=none 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 E7456385DC11 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32b ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702996210; cv=none; b=m0akD8tbtDEWyPlksJ3O/Kye0+KizjMOmr2VbuAtxqSaHpDnzCDROGN6a51Y4Vw/77i0nIS0PCIoidUbiOvEP4t89bBudlGYvDy7GiUTqB/Jc7yhNFON+gGxq1NXb1PRTxjIEOTyQVrRH+xDtFvMZhzjNWVq2A5QLXdi0M2wVcI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702996210; c=relaxed/simple; bh=ofizg912tl7I2Ob241j8LyaSfRTnl90GG1U6je6vCdk=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=w6fCCXlMvOmomUlQt3klgJTj6sigcjGJugETLdC54WGDzj7Y00+s0yxLEQzlXRLX7PVxoRglm9ypH0iPweXfeIuFpS3+WiQi1+kMFhwYIqDdxWyUnk8H7bLkg0oHNDAp8tEyiRWm8QYFo5womuvccBLD8JHNq1SKz4a66QVzA0w= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32b.google.com with SMTP id 5b1f17b1804b1-40d05ebe642so16117885e9.0 for ; Tue, 19 Dec 2023 06:30:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1702996207; x=1703601007; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=ai7JzYLy/LsmXQI97bS1PbXW0/jQfiQLaoHWaOCllpo=; b=BaXN+5iWttTAyg54rnnQV7wRc1ciDWtX5v9X7wBQ/gM/E3CW11o8WEXeJyLW/QnTfA VBzOvjOvKru3UTjruUeCE1e0U+EfEfBQJqPFLfcq9iE1I/3xazgd2AH0qHTBvKqTa4OJ y2Z96ov6471KQMRHu4OlOLHsrOKgbreulp7EE8ABWojQif7S44Vxd17HNbpEacBrDqSi 15s9L/Cqiz/p+7Q5Yx/NMpfdVthnJ6g+KnSD6h0meJ/J8pkvzNrYMYaecAmlfDpkX55d 0Dcq4U9Y/gKJiVc947JY/oM785FxlnsUDC6Q5H4FgYku73al1k9kwcxcDpOqglNvrSE5 slXg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1702996207; x=1703601007; 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=ai7JzYLy/LsmXQI97bS1PbXW0/jQfiQLaoHWaOCllpo=; b=MJaeVvnPzYparsKdLSZId3ejxr2jAr13eopcSmMgPK/CFiIKo7eCE9JWLuPlpbxmg6 NXjLrbsgxqknB9FVyAnLX71DDVGOnD83kNW1MLpJxfOOPLqNHeyXewLDQvQzWfTYi7fl ryVIJWreiPx6FEQPSkcmojebFmlEjop/+Q53Jmo0SSMdI3+0fn4pxjiW9ugTMdzVkqRw 7jmekmQlq34aVB8JPzsE+kBN7fbzn7suk1T1xPzN9/u2H6BOXhPS0uv5Y7HrPW6Zo6/k 6bCZFyjwBinF77rXnIQDaaajOSG7binP5HvosTjvwC7DxtozpQ04CsJe1sQScNKH6EDW 8SUg== X-Gm-Message-State: AOJu0YwUpBM7AcNDA5pV7KUqCWh12STsdvoULIGxpa2iHXMd1T/pbBdA tyY0nUor3ZXobYcesaYm8A8OuzXzHT06WwQGKcw= X-Google-Smtp-Source: AGHT+IGCWPpNqOyXdfCI91qRJjQDiTgtSOu+PlvgshFmGEB4hkCU8695Wmpdy3xz8J2DIFWAQ1sBSw== X-Received: by 2002:a05:600c:3ba3:b0:40b:5e26:2372 with SMTP id n35-20020a05600c3ba300b0040b5e262372mr606163wms.35.1702996207489; Tue, 19 Dec 2023 06:30:07 -0800 (PST) Received: from poulhies-Precision-5550.lan ([2001:861:3382:1a90:fe1e:443:c34f:edaa]) by smtp.gmail.com with ESMTPSA id v16-20020adfe4d0000000b0033672cfca96sm1672065wrm.89.2023.12.19.06.30.06 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Dec 2023 06:30:07 -0800 (PST) From: =?utf-8?q?Marc_Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Steve Baird Subject: [COMMITTED] ada: Illegal instance of Generic_1.Generic_2 incorrectly accepted Date: Tue, 19 Dec 2023 15:30:05 +0100 Message-ID: <20231219143005.454530-1-poulhies@adacore.com> X-Mailer: git-send-email 2.43.0 MIME-Version: 1.0 X-Spam-Status: No, score=-13.3 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.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 From: Steve Baird If G1 is a generic package and G1.G2 is a child unit (also a generic package) then it would be illegal if some third generic unit (declared outside of G1) takes a formal instance of G1.G2, as in "with package I2 is new G1.G2;". This construct was incorrectly accepted in some cases. gcc/ada/ * sem_ch12.adb (Check_Generic_Child_Unit): Introduce a new nested function Adjusted_Inst_Par_Ekind to cope with cases where either a- the visibility of a compiler-generated renaming is incorrect; or b- we are inside of a generic parent unit G1 that has a child unit G1.G2, so instantiation of G1.G2 is permitted. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/sem_ch12.adb | 96 +++++++++++++++++++++++++++++++++++++++----- 1 file changed, 85 insertions(+), 11 deletions(-) diff --git a/gcc/ada/sem_ch12.adb b/gcc/ada/sem_ch12.adb index 5db9754f81d..e229d217555 100644 --- a/gcc/ada/sem_ch12.adb +++ b/gcc/ada/sem_ch12.adb @@ -7234,7 +7234,7 @@ package body Sem_Ch12 is Loc : constant Source_Ptr := Sloc (Gen_Id); Gen_Par : Entity_Id := Empty; E : Entity_Id; - Inst_Par : Entity_Id; + Inst_Par : Entity_Id := Empty; S : Node_Id; function Find_Generic_Child @@ -7440,16 +7440,90 @@ package body Sem_Ch12 is -- the instance of Gpar, so this is illegal. The test below -- recognizes this particular case. - if Is_Child_Unit (E) - and then not Comes_From_Source (Entity (Prefix (Gen_Id))) - and then (not In_Instance - or else Nkind (Parent (Parent (Gen_Id))) = - N_Compilation_Unit) - then - Error_Msg_N - ("prefix of generic child unit must be instance of parent", - Gen_Id); - end if; + declare + -- We want to reject the final instantiation in + -- generic package G1 is end G1; + -- generic package G1.G2 is end G1.G2; + -- with G1; package I1 is new G1; + -- with G1.G2; package I1.I2 is new G1.G2; + -- because the use of G1.G2 should instead be either + -- I1.G2 or simply G2. However, the tree that is built + -- in this case is wrong. In the expanded copy + -- of G2, we need (and therefore generate) a renaming + -- package G1 renames I1; + -- but this renaming should not participate in resolving + -- this occurrence of the name "G1.G2"; unfortunately, + -- it does. Rather than correct this error, we compensate + -- for it in this function. + -- + -- We also perform another adjustment here. If we are + -- currently inside a generic package, then that + -- generic package needs to be treated as a package. + -- For example, if a generic Aaa declares a nested generic + -- Bbb (perhaps as a child unit) then Aaa can also legally + -- declare an instance of Aaa.Bbb. + + function Adjusted_Inst_Par_Ekind return Entity_Kind; + + ----------------------------- + -- Adjusted_Inst_Par_Ekind -- + ----------------------------- + + function Adjusted_Inst_Par_Ekind return Entity_Kind is + Prefix_Entity : Entity_Id; + Inst_Par_GP : Node_Id; + Inst_Par_Parent : Node_Id := Parent (Inst_Par); + begin + if Nkind (Inst_Par_Parent) = N_Defining_Program_Unit_Name + then + Inst_Par_Parent := Parent (Inst_Par_Parent); + end if; + + Inst_Par_GP := Generic_Parent (Inst_Par_Parent); + + if Nkind (Gen_Id) = N_Expanded_Name + and then Present (Inst_Par_GP) + and then Ekind (Inst_Par_GP) = E_Generic_Package + then + Prefix_Entity := Entity (Prefix (Gen_Id)); + + if Present (Prefix_Entity) + and then not Comes_From_Source (Prefix_Entity) + and then Nkind (Parent (Prefix_Entity)) = + N_Package_Renaming_Declaration + and then Chars (Prefix_Entity) = Chars (Inst_Par_GP) + then + return E_Generic_Package; + end if; + end if; + + if Ekind (Inst_Par) = E_Generic_Package + and then In_Open_Scopes (Inst_Par) + then + -- If we are inside a generic package then + -- treat it as a package. + return E_Package; + end if; + + -- The usual path + return Ekind (Inst_Par); + end Adjusted_Inst_Par_Ekind; + + begin + if Is_Child_Unit (E) + and then (No (Inst_Par) + or else Adjusted_Inst_Par_Ekind = + E_Generic_Package) + and then (not In_Instance + or else Nkind (Parent (Parent (Gen_Id))) = + N_Compilation_Unit) + then + Error_Msg_N + ("prefix of generic child unit must be " & + "instance of parent", + Gen_Id); + end if; + end; if not In_Open_Scopes (Inst_Par) and then Nkind (Parent (Gen_Id)) not in