From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x330.google.com (mail-wm1-x330.google.com [IPv6:2a00:1450:4864:20::330]) by sourceware.org (Postfix) with ESMTPS id CA6453849AF5 for ; Thu, 16 May 2024 09:26:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org CA6453849AF5 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine 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 CA6453849AF5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::330 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715851589; cv=none; b=njASmEIcyGuuQ3erLlCX08wGvlgRto+0mP8a6Atkm5WWhQVCYZXcNtI86Log3kvbYyejJ4eMn3Z9iWZUPn9bpTAVus0jcCBHyztL6Ui0JscZTTFngv8oc4WzDKjrZEYz/JsyztT2FRqF650A+oc1IO2/3P35xWSI5tRLxpqPYZw= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1715851589; c=relaxed/simple; bh=sQ4pODlSq0jYj55ZUw5nt2yshgHLOGEgFZRjYqWe0/k=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=RMd3CGFPjGsZlf4nurL3yGMfq3ROoOQCrPYXh5fmvpcC0TFMHyQfVA6Rv79lGewP7Zw3/wY+P/S3BUm/BoZVxvGkc6RDrl/yRgZPvtdLIJUS6pTa7Dwv2P9bb5nY/7TaONGtRFtd0s18nwJfh5Y7myCdvZy30O8//5KWXcAiopk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x330.google.com with SMTP id 5b1f17b1804b1-420160f8f52so29093445e9.0 for ; Thu, 16 May 2024 02:26:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1715851582; x=1716456382; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=Wf/B8rvcPl+PzYQMITWlD0KZDvIKruNH0YRodvXIxvA=; b=jf3d89Zt7JDpdytFifrrWb/2T3iBfSdLJVqWDS4MHklpk7JZf3wLDpo1icntl+ZfQd mw87r/Pf6eJioEnnZV/UT/b/CihfsgyIRUkAVfYGq1NTELvVlUv1Tg5UhShMmnqyDREP 5yqh+BjGzpiqAgICww5x/uMR6Cw1umk9QsRd0oXmMn8haRRoqSylguvBGnhB7WY86pN8 VZIX9FUaC0AaIDLh+e7jGuoGnhMI0uij5Qxp+C1MTCJFTdfqmW2ZDlXl7FtcZKhhngtd UFSXDuSS6zx+EyRBBXuO7utIpBIuCZ3w6h4MMWJv/BkNSdiWuu4XG+7jmS8GvzgA9rRY EDYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1715851582; x=1716456382; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=Wf/B8rvcPl+PzYQMITWlD0KZDvIKruNH0YRodvXIxvA=; b=o4VKPVGt78OwY5jcm35GlfsuVCRveeJQx5Xjdy6ZWaDVJIXm7aBsHhqe1M81Y4WQ/N j5BoRBj1q0M4qwt+Tn0UKAmcuFYbyPxpoh9isMqom5cs1Pxff2TyLyZaabe2BNag9Rsk 4FNHhqJ/BCvkM+ON0kbc3jfjTW7Gxj8aVZYN+cX0Z7N9sbyqxugqxOqve+89B3tv6GSi EAibrI7IKomEzdh/KD/bbARYgxeG+OqZGRynFFWpW68Y/nBr1hguNohkug6/zof6so7M BVgJs2YTPiSUBa+uWBF89ihG3fMJz31oMLtBtLAetsXovqhKadYA2JpoCs8FJrEGLEZ/ FwMQ== X-Gm-Message-State: AOJu0Yy2jxguSM/8+7nKTRC+7G5lgL1CkaydpMb2TX1vpIEf+bfTdc+X jL/v+WUiakkVsaYZ/2esKe1xU8eOVmihVsvOsE1oUUbReRdh4XHH7hiIK+QlNCIyQ9vYiD/e5QY = X-Google-Smtp-Source: AGHT+IEWyXREEj/yV8hAhQ7YDDzruSYfPaEH9oL7IfuJrta5I/v6BKtjBW/5xeMNJxArVOVy4urYNQ== X-Received: by 2002:a05:600c:1c02:b0:41b:da5b:a60d with SMTP id 5b1f17b1804b1-41fea93af25mr146676895e9.6.1715851582498; Thu, 16 May 2024 02:26:22 -0700 (PDT) Received: from poulhies-Precision-5550.telnowedge.local (lmontsouris-659-1-24-67.w81-250.abo.wanadoo.fr. [81.250.175.67]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-41fd10cf8besm135802635e9.1.2024.05.16.02.26.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 May 2024 02:26:22 -0700 (PDT) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= Subject: [COMMITTED 18/35] ada: Fixup one more pattern of broken scope information Date: Thu, 16 May 2024 11:25:47 +0200 Message-ID: <20240516092606.41242-18-poulhies@adacore.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240516092606.41242-1-poulhies@adacore.com> References: <20240516092606.41242-1-poulhies@adacore.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.7 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.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: When an array's initialization contains a `others =>` clause with an expression that involves finalization, the resulting scope information is incorrect and can cause crashes with backend (i.e. gnat-llvm) that also use unnesting. The observable symptom is a nested object declaration (created by the compiler) within a loop wrapped in a procedure created by the unnester that has incoherent scope information: its Scope field points to the scope of the procedure (1 level too high) and is contained in the entity chain of some entity nested in the procedure (correct). The correct solution would be to fix the scope information when it is created, but this revealed too large of a task with many interaction with existing code. This change adds another pattern to the Fixup_Inner_Scopes procedure to detect the problematic case and fix the scope, "after the facts". gcc/ada/ * exp_ch7.adb (Unnest_Loop::Fixup_Inner_Scopes): detect a new problematic pattern and fixup the scope accordingly. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch7.adb | 66 ++++++++++++++++++++++++++++++++++++++------- 1 file changed, 56 insertions(+), 10 deletions(-) diff --git a/gcc/ada/exp_ch7.adb b/gcc/ada/exp_ch7.adb index 25a7c0b2b46..6d76572f405 100644 --- a/gcc/ada/exp_ch7.adb +++ b/gcc/ada/exp_ch7.adb @@ -8809,8 +8809,11 @@ package body Exp_Ch7 is procedure Unnest_Loop (Loop_Stmt : Node_Id) is - procedure Fixup_Inner_Scopes (Loop_Stmt : Node_Id); - -- The loops created by the compiler for array aggregates can have + procedure Fixup_Inner_Scopes (Loop_Or_Block : Node_Id); + -- This procedure fixes the scope for 2 identified cases of incorrect + -- scope information. + -- + -- 1) The loops created by the compiler for array aggregates can have -- nested finalization procedure when the type of the array components -- needs finalization. It has the following form: @@ -8825,7 +8828,7 @@ package body Exp_Ch7 is -- obj (J4b) := ...; -- When the compiler creates the N_Block_Statement, it sets its scope to - -- the upper scope (the one containing the loop). + -- the outer scope (the one containing the loop). -- The Unnest_Loop procedure moves the N_Loop_Statement inside a new -- procedure and correctly sets the scopes for both the new procedure @@ -8833,25 +8836,68 @@ package body Exp_Ch7 is -- leaves the Tree in an incoherent state (i.e. the inner procedure must -- have its enclosing procedure in its scope ancestries). - -- This procedure fixes the scope links. + -- 2) The second case happens when an object declaration is created + -- within a loop used to initialize the 'others' components of an + -- aggregate that is nested within a transient scope. When the transient + -- scope is removed, the object scope is set to the outer scope. For + -- example: + + -- package pack + -- ... + -- L98s : for J90s in 2 .. 19 loop + -- B101s : declare + -- R92s : aliased some_type; + -- ... + + -- The loop L98s was initially wrapped in a transient scope B72s and + -- R92s was nested within it. Then the transient scope is removed and + -- the scope of R92s is set to 'pack'. And finally, when the unnester + -- moves the loop body in a new procedure, R92s's scope is still left + -- unchanged. + + -- This procedure finds the two previous patterns and fixes the scope + -- information. -- Another (better) fix would be to have the block scope set to be the -- loop entity earlier (when the block is created or when the loop gets -- an actual entity set). But unfortunately this proved harder to -- implement ??? - procedure Fixup_Inner_Scopes (Loop_Stmt : Node_Id) is - Stmt : Node_Id := First (Statements (Loop_Stmt)); - Loop_Stmt_Ent : constant Entity_Id := Entity (Identifier (Loop_Stmt)); - Ent_To_Fix : Entity_Id; + procedure Fixup_Inner_Scopes (Loop_Or_Block : Node_Id) is + Stmt : Node_Id; + Loop_Or_Block_Ent : Entity_Id; + Ent_To_Fix : Entity_Id; + Decl : Node_Id := Empty; begin + pragma Assert (Nkind (Loop_Or_Block) in + N_Loop_Statement | N_Block_Statement); + + Loop_Or_Block_Ent := Entity (Identifier (Loop_Or_Block)); + if Nkind (Loop_Or_Block) = N_Loop_Statement then + Stmt := First (Statements (Loop_Or_Block)); + else -- N_Block_Statement + Stmt := First + (Statements (Handled_Statement_Sequence (Loop_Or_Block))); + Decl := First (Declarations (Loop_Or_Block)); + end if; + + -- Fix scopes for any object declaration found in the block + while Present (Decl) loop + if Nkind (Decl) = N_Object_Declaration then + Ent_To_Fix := Defining_Identifier (Decl); + Set_Scope (Ent_To_Fix, Loop_Or_Block_Ent); + end if; + Next (Decl); + end loop; + while Present (Stmt) loop if Nkind (Stmt) = N_Block_Statement and then Is_Abort_Block (Stmt) then Ent_To_Fix := Entity (Identifier (Stmt)); - Set_Scope (Ent_To_Fix, Loop_Stmt_Ent); - elsif Nkind (Stmt) = N_Loop_Statement then + Set_Scope (Ent_To_Fix, Loop_Or_Block_Ent); + elsif Nkind (Stmt) in N_Block_Statement | N_Loop_Statement + then Fixup_Inner_Scopes (Stmt); end if; Next (Stmt); -- 2.43.2