From patchwork Mon May 16 08:43:09 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: 54012 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 5F4C93857C6F for ; Mon, 16 May 2022 08:57:39 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5F4C93857C6F DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1652691459; bh=HbniPOsCHjYNbQOO+wtxrGm1vFBN8lnDx6s3TKAqJTY=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=Got8tOoGkZSRkO7m8PA2xegbPNYnzqBWcKdfjCEzvdhvkJXgZLAsoOoOtMdxJ+U44 T841rixtgS8rZy2ei92tIzbusrR1a7lEr8KxaHvkuimUK0wQK1zfSzFDUI5SaPNXBB aCS2hEgrGBnV9neMnMHYKSFPVcr+olhyOoIB8hgA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x331.google.com (mail-wm1-x331.google.com [IPv6:2a00:1450:4864:20::331]) by sourceware.org (Postfix) with ESMTPS id A9B353857822 for ; Mon, 16 May 2022 08:43:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org A9B353857822 Received: by mail-wm1-x331.google.com with SMTP id p189so8244084wmp.3 for ; Mon, 16 May 2022 01:43:11 -0700 (PDT) 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=HbniPOsCHjYNbQOO+wtxrGm1vFBN8lnDx6s3TKAqJTY=; b=CIN2mG3S4gheFxTXygpl2mohxLY17CntZkvG8vqAC7aaMg3aB3Q2FmDaoSOuiVweGk W/SYAhE+vXPS2x8ics9SqicJKm5kpbZ6g47oTwUn+vAnQdk2A9aE3FVS3Jb8gymbRc0G KTClbjNThIUJtiZ8YiPTxP2zDbYPU/R3HrYT7558QLOfjMLBFJtuxCe7cMiY6oYKrklj EleG0hZPYqWuVtEiEydOne2h9IDHadBd+0o6jZ2k36471poI0/UqGdW21Sfgk2x2CM35 tgBJlEZ4sDOH8LmuK48huIWVVI9yORttzfa2gpP0ElGgLf8TbkSpogDCi0clI6UC6Pgj CZAA== X-Gm-Message-State: AOAM532NStnfdoK6dbpvOxL4/UNdlFvKHZb1VsTBgK44YaEnwoFQmbXD evN5MPH2TmYyKg6wnb4eaKNGSnnWroH+2w== X-Google-Smtp-Source: ABdhPJx2hgllE6wl3L8e37CQ3L/5tBa+0pDtwpm7TFen06bAYMhMAec7IdR73jSnZ2fZqJUQndKgQQ== X-Received: by 2002:a7b:c086:0:b0:393:f6fa:cfc7 with SMTP id r6-20020a7bc086000000b00393f6facfc7mr26670703wmh.62.1652690590460; Mon, 16 May 2022 01:43:10 -0700 (PDT) Received: from adacore.com ([45.147.211.82]) by smtp.gmail.com with ESMTPSA id e2-20020adf9bc2000000b0020d069148bcsm3332695wrc.110.2022.05.16.01.43.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 16 May 2022 01:43:09 -0700 (PDT) Date: Mon, 16 May 2022 08:43:09 +0000 To: gcc-patches@gcc.gnu.org Subject: [Ada] Fix iterated element association loop var escaping loop scope Message-ID: <20220516084309.GA3843604@adacore.com> MIME-Version: 1.0 Content-Disposition: inline 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, 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: Pierre-Marie de Rodat via Gcc-patches From: Pierre-Marie de Rodat Reply-To: Pierre-Marie de Rodat Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" Fix the escaping of the loop variable from the loop scope in both forms of iterated element associations (i.e. "for J in ..." and "for J of ..."). Create a dedicated scope around the analyses of both loops. Also create a copy of the Loop_Parameter_Specification instead of analyzing (and modifying) the original Tree as it will be reanalyzed later. Tested on x86_64-pc-linux-gnu, committed on trunk gcc/ada/ * sem_aggr.adb (Resolve_Iterated_Association): Create scope around N_Iterated_Element_Association handling. Analyze a copy of the Loop_Parameter_Specification. Call Analyze instead Analyze_* to be more homogeneous. (Sem_Ch5): Remove now unused package. diff --git a/gcc/ada/sem_aggr.adb b/gcc/ada/sem_aggr.adb --- a/gcc/ada/sem_aggr.adb +++ b/gcc/ada/sem_aggr.adb @@ -51,7 +51,6 @@ with Sem_Aux; use Sem_Aux; with Sem_Case; use Sem_Case; with Sem_Cat; use Sem_Cat; with Sem_Ch3; use Sem_Ch3; -with Sem_Ch5; use Sem_Ch5; with Sem_Ch8; use Sem_Ch8; with Sem_Ch13; use Sem_Ch13; with Sem_Dim; use Sem_Dim; @@ -2890,12 +2889,12 @@ package body Sem_Aggr is is Loc : constant Source_Ptr := Sloc (N); Choice : Node_Id; + Copy : Node_Id; Ent : Entity_Id; Expr : Node_Id; Key_Expr : Node_Id; Id : Entity_Id; Id_Name : Name_Id; - Iter : Node_Id; Typ : Entity_Id := Empty; begin @@ -2906,15 +2905,29 @@ package body Sem_Aggr is -- is present. In both cases a Key_Expression is present. if Nkind (Comp) = N_Iterated_Element_Association then + + -- Create a temporary scope to avoid some modifications from + -- escaping the Analyze call below. The original Tree will be + -- reanalyzed later. + + Ent := New_Internal_Entity + (E_Loop, Current_Scope, Sloc (Comp), 'L'); + Set_Etype (Ent, Standard_Void_Type); + Set_Parent (Ent, Parent (Comp)); + Push_Scope (Ent); + if Present (Loop_Parameter_Specification (Comp)) then - Analyze_Loop_Parameter_Specification - (Loop_Parameter_Specification (Comp)); + Copy := Copy_Separate_Tree (Comp); + + Analyze + (Loop_Parameter_Specification (Copy)); + Id_Name := Chars (Defining_Identifier (Loop_Parameter_Specification (Comp))); else - Iter := Copy_Separate_Tree (Iterator_Specification (Comp)); - Analyze (Iter); - Typ := Etype (Defining_Identifier (Iter)); + Copy := Copy_Separate_Tree (Iterator_Specification (Comp)); + Analyze (Copy); + Id_Name := Chars (Defining_Identifier (Iterator_Specification (Comp))); end if; @@ -2926,12 +2939,14 @@ package body Sem_Aggr is Key_Expr := Key_Expression (Comp); Analyze_And_Resolve (New_Copy_Tree (Key_Expr), Key_Type); + End_Scope; elsif Present (Iterator_Specification (Comp)) then - Iter := Copy_Separate_Tree (Iterator_Specification (Comp)); + Copy := Copy_Separate_Tree (Iterator_Specification (Comp)); Id_Name := Chars (Defining_Identifier (Comp)); - Analyze (Iter); - Typ := Etype (Defining_Identifier (Iter)); + + Analyze (Copy); + Typ := Etype (Defining_Identifier (Copy)); else Choice := First (Discrete_Choices (Comp)); @@ -2965,7 +2980,8 @@ package body Sem_Aggr is -- analysis. Id := Make_Defining_Identifier (Sloc (Comp), Id_Name); - Ent := New_Internal_Entity (E_Loop, Current_Scope, Sloc (Comp), 'L'); + Ent := New_Internal_Entity (E_Loop, + Current_Scope, Sloc (Comp), 'L'); Set_Etype (Ent, Standard_Void_Type); Set_Parent (Ent, Parent (Comp)); Push_Scope (Ent); @@ -3000,6 +3016,8 @@ package body Sem_Aggr is end Resolve_Iterated_Association; + -- Start of processing for Resolve_Container_Aggregate + begin pragma Assert (Nkind (Asp) = N_Aggregate);