* [COMMITTED] ada: Avoid renaming_decl in case of constrained array
@ 2023-07-11 9:25 Marc Poulhiès
0 siblings, 0 replies; only message in thread
From: Marc Poulhiès @ 2023-07-11 9:25 UTC (permalink / raw)
To: gcc-patches; +Cc: Bob Duff
From: Bob Duff <duff@adacore.com>
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
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2023-07-11 9:26 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-11 9:25 [COMMITTED] ada: Avoid renaming_decl in case of constrained array Marc Poulhiès
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).