From patchwork Wed Jun 16 08:43:55 2021 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: 1492822 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (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 ozlabs.org (Postfix) with ESMTPS id 4G4f8v3FZ6z9sXL for ; Wed, 16 Jun 2021 18:53:03 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B9A42398B86B for ; Wed, 16 Jun 2021 08:53:00 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from rock.gnat.com (rock.gnat.com [205.232.38.15]) by sourceware.org (Postfix) with ESMTPS id C1EEA398900E for ; Wed, 16 Jun 2021 08:43:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org C1EEA398900E Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: from localhost (localhost.localdomain [127.0.0.1]) by filtered-rock.gnat.com (Postfix) with ESMTP id 90CED117D6E; Wed, 16 Jun 2021 04:43:55 -0400 (EDT) X-Virus-Scanned: Debian amavisd-new at gnat.com Received: from rock.gnat.com ([127.0.0.1]) by localhost (rock.gnat.com [127.0.0.1]) (amavisd-new, port 10024) with LMTP id ZQnjTrpAPuAi; Wed, 16 Jun 2021 04:43:55 -0400 (EDT) Received: from tron.gnat.com (tron.gnat.com [205.232.38.10]) by rock.gnat.com (Postfix) with ESMTP id 77223117762; Wed, 16 Jun 2021 04:43:55 -0400 (EDT) Received: by tron.gnat.com (Postfix, from userid 4862) id 762E5180; Wed, 16 Jun 2021 04:43:55 -0400 (EDT) Date: Wed, 16 Jun 2021 04:43:55 -0400 From: Pierre-Marie de Rodat To: gcc-patches@gcc.gnu.org Subject: [Ada] Implementation of AI12-0152: legality rules for Raise_Expression Message-ID: <20210616084355.GA94726@adacore.com> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.23 (2014-03-12) X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) 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: , Cc: Ed Schonberg Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" This patch implements the legality rules specified in RM 11.3 (2) for the placement of Raise_Expressions. In Ada2020 aspect specifications can appear in several new declarative contexts, and the keyword "with" can be part of a Raise_Expression or the start of a list of aspect specifications. To prevent ambiguities a Raise_Expression in such contexts must appear within parentheses, or be part of a larger parenthesized expression. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_res.adb (Resolve_Raise_Expression): Apply Ada_2020 rules concerning the need for parentheses around Raise_Expressions in various contexts. diff --git a/gcc/ada/sem_res.adb b/gcc/ada/sem_res.adb --- a/gcc/ada/sem_res.adb +++ b/gcc/ada/sem_res.adb @@ -10532,8 +10532,57 @@ package body Sem_Res is if Typ = Raise_Type then Error_Msg_N ("cannot find unique type for raise expression", N); Set_Etype (N, Any_Type); + else Set_Etype (N, Typ); + + -- Apply check for required parentheses in the enclosing + -- context of raise_expressions (RM 11.3 (2)), including default + -- expressions in contexts that can include aspect specifications, + -- and ancestor parts of extension aggregates. + + declare + Par : Node_Id := Parent (N); + Parentheses_Found : Boolean := Paren_Count (N) > 0; + + begin + while Present (Par) + and then Nkind (Par) in N_Has_Etype + loop + if Paren_Count (Par) > 0 then + Parentheses_Found := True; + end if; + + if Nkind (Par) = N_Extension_Aggregate + and then N = Ancestor_Part (Par) + then + exit; + end if; + + Par := Parent (Par); + end loop; + + if not Parentheses_Found + and then Comes_From_Source (Par) + and then + ((Nkind (Par) in N_Modular_Type_Definition + | N_Floating_Point_Definition + | N_Ordinary_Fixed_Point_Definition + | N_Decimal_Fixed_Point_Definition + | N_Extension_Aggregate + | N_Discriminant_Specification + | N_Parameter_Specification + | N_Formal_Object_Declaration) + + or else (Nkind (Par) = N_Object_Declaration + and then + Nkind (Parent (Par)) /= N_Extended_Return_Statement)) + then + Error_Msg_N + ("raise_expression must be parenthesized in this context", + N); + end if; + end; end if; end Resolve_Raise_Expression;