From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x334.google.com (mail-wm1-x334.google.com [IPv6:2a00:1450:4864:20::334]) by sourceware.org (Postfix) with ESMTPS id 23F543858408 for ; Tue, 11 Jul 2023 09:26:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 23F543858408 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=adacore.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=adacore.com Received: by mail-wm1-x334.google.com with SMTP id 5b1f17b1804b1-3fb4146e8fcso39382275e9.0 for ; Tue, 11 Jul 2023 02:26:02 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=adacore.com; s=google; t=1689067561; x=1691659561; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=TdKuv4ZMISAK/Eb2LoiSVK/x10K0FVZFXU2+PSKAfxQ=; b=OGtXPCU4zMxlQv3ypfY3yyLMFTVNtcYpvHkhnBTGPJM7J464qz4F3A1ltqx93bwX1F 63KuEclkiAD3btIeZv+iu3c77UFkeBY85mxaQepTjbQSxJ99tFrQrKcftOMU6Ae2Wa+w g/GabicA7z1RL/ccjjGwkcBTZM+jKU21R2sq7NYS84nVPbrHtkuONeFDYEOH90cKMFzb ngGd+mJTQ4E/yn6ba471jiHQo1eDpc7kz7Y0IsC41NbW6ykWZqDH1/tInA/lWyrwmcKW I+S96DpI8cEbRroXlRFAqqzusxDU+3ygYqkCTZAzq2zgQ9ctfafVfPLZ7PWJK0IFrmLU DJpw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689067561; x=1691659561; 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=TdKuv4ZMISAK/Eb2LoiSVK/x10K0FVZFXU2+PSKAfxQ=; b=bc+LOdAqcJ5r+zDNfT1jN8Ba8scZvTGm3uzSHUyheI+DTQoFMvNWs5/1LMHrm7VgsF S+nHgfl5Vo75X9CCZhU1GqbWLNWPXLuzEb1CDD8A+LGFtvxx8W8O8dVx0wQJ+gXlytk2 T9SgdwC2Za9piGrssKZ3DDkZyt/PmdSb54mYiZJB+pNInJcE8wWQUpZvu+qmLQu7o5ug Ma7/t0tlc4TKAUJI6mPpxi5n9XSwvLs701o4Q0itGzlDgmRWuE2w+lsAGIukUpIRGrbs xvY10MNxAUTkpk6XSAmYH3ngB22EBvyBmCneRM9vJF1OcFvL5xnxIuN3w+iw3/79RN8W pjwg== X-Gm-Message-State: ABy/qLbYGtOA63RC31KsN7We9HHw1PfBCveF1YPCd4ha8kdnNoUtHGDG dAFUylaF5jl/5N5q6Bl8ovQZK59Z6IRSP/vMIfynyg== X-Google-Smtp-Source: APBJJlE8sDxN26XICHANS7DH6d+AKsOUyPFrz7J+1iycHCWTtHbH7hb6tpliWxx2tHhH/F7+S3Q30w== X-Received: by 2002:a05:600c:3c86:b0:3fb:c257:9f48 with SMTP id bg6-20020a05600c3c8600b003fbc2579f48mr16646050wmb.16.1689067560880; Tue, 11 Jul 2023 02:26:00 -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 u19-20020a05600c00d300b003fc02e8ea68sm9909890wmm.13.2023.07.11.02.26.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 11 Jul 2023 02:26:00 -0700 (PDT) From: =?UTF-8?q?Marc=20Poulhi=C3=A8s?= To: gcc-patches@gcc.gnu.org Cc: Bob Duff Subject: [COMMITTED] ada: Avoid renaming_decl in case of constrained array Date: Tue, 11 Jul 2023 11:25:59 +0200 Message-Id: <20230711092559.2324105-1-poulhies@adacore.com> X-Mailer: git-send-email 2.40.0 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,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 List-Id: From: Bob Duff This patch avoids rewriting "X: S := F(...);" as "X: S renames F(...);". That rewrite is incorrect if S is a constrained array subtype, because it changes the semantics. In the original, the bounds of X are that of S. But constraints are ignored in renamings, so the bounds of X would come from F'Result. This can cause spurious Constraint_Errors in some obscure cases. It causes unnecessary checks to be inserted, and even when such checks pass (more common case), they might be less efficient. gcc/ada/ * exp_ch3.adb (Expand_N_Object_Declaration): Avoid transforming to a renaming in case of constrained array that comes from source. Tested on x86_64-pc-linux-gnu, committed on master. --- gcc/ada/exp_ch3.adb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/gcc/ada/exp_ch3.adb b/gcc/ada/exp_ch3.adb index daf27fb25e9..db27a5f68b6 100644 --- a/gcc/ada/exp_ch3.adb +++ b/gcc/ada/exp_ch3.adb @@ -7275,6 +7275,13 @@ package body Exp_Ch3 is Rewrite_As_Renaming : Boolean := False; -- Whether to turn the declaration into a renaming at the end + Nominal_Subtype_Is_Constrained_Array : constant Boolean := + Comes_From_Source (Obj_Def) + and then Is_Array_Type (Typ) and then Is_Constrained (Typ); + -- Used to avoid rewriting as a renaming for constrained arrays, + -- which is only a problem for source arrays; others have the + -- correct bounds (see below). + -- Start of processing for Expand_N_Object_Declaration begin @@ -8030,7 +8037,14 @@ package body Exp_Ch3 is or else (Nkind (Expr_Q) = N_Slice and then OK_To_Rename_Ref (Prefix (Expr_Q)) - and then not Special_Ret_Obj)); + and then not Special_Ret_Obj)) + + -- If we have "X : S := ...;", and S is a constrained array + -- subtype, then we cannot rename, because renamings ignore + -- the constraints of S, so that would change the semantics + -- (sliding would not occur on the initial value). + + and then not Nominal_Subtype_Is_Constrained_Array; -- If the type needs finalization and is not inherently limited, -- then the target is adjusted after the copy and attached to the -- 2.40.0