public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [COMMITTED] ada: Fix crash on vector initialization
@ 2023-05-29  8:29 Marc Poulhiès
  2023-07-07  8:49 ` Eric Botcazou
  0 siblings, 1 reply; 2+ messages in thread
From: Marc Poulhiès @ 2023-05-29  8:29 UTC (permalink / raw)
  To: gcc-patches; +Cc: Marc Poulhiès

Initializing a vector using

 Vec : V.Vector := [Some_Type'(Some_Abstract_Type with F => 0)];

may crash the compiler. The expander marks the N_Extension_Aggregate for
delayed expansion which never happens and incorrectly ends up in gigi.

The delayed expansion is needed for nested aggregates, which the
original code is testing for, but container aggregates are handled
differently.

Such assignments to container aggregates are later transformed into
procedure calls to the procedures named in the Aggregate aspect
definition, for which the delayed expansion is not required/expected.

gcc/ada/

	* exp_aggr.adb (Convert_To_Assignments): Do not mark node for
	delayed expansion if parent type has the Aggregate aspect.
	* sem_util.adb (Is_Container_Aggregate): Move...
	* sem_util.ads (Is_Container_Aggregate): ... here and make it
	public.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_aggr.adb | 7 +++++--
 gcc/ada/sem_util.adb | 3 ---
 gcc/ada/sem_util.ads | 3 +++
 3 files changed, 8 insertions(+), 5 deletions(-)

diff --git a/gcc/ada/exp_aggr.adb b/gcc/ada/exp_aggr.adb
index a6a7d810185..75e5e1402df 100644
--- a/gcc/ada/exp_aggr.adb
+++ b/gcc/ada/exp_aggr.adb
@@ -5000,9 +5000,12 @@ package body Exp_Aggr is
 
       if
          --  Internal aggregate (transformed when expanding the parent)
+         --  excluding the Container aggregate as these are transformed to
+         --  procedure call later.
 
-         Parent_Kind in
-           N_Aggregate | N_Extension_Aggregate | N_Component_Association
+         (Parent_Kind in
+            N_Component_Association | N_Aggregate | N_Extension_Aggregate
+            and then not Is_Container_Aggregate (Parent_Node))
 
          --  Allocator (see Convert_Aggr_In_Allocator)
 
diff --git a/gcc/ada/sem_util.adb b/gcc/ada/sem_util.adb
index 64abfb11ce5..a42b2dff60f 100644
--- a/gcc/ada/sem_util.adb
+++ b/gcc/ada/sem_util.adb
@@ -132,9 +132,6 @@ package body Sem_Util is
    --  Determine whether arbitrary entity Id denotes an atomic object as per
    --  RM C.6(7).
 
-   function Is_Container_Aggregate (Exp : Node_Id) return Boolean;
-   --  Is the given expression a container aggregate?
-
    generic
       with function Is_Effectively_Volatile_Entity
         (Id : Entity_Id) return Boolean;
diff --git a/gcc/ada/sem_util.ads b/gcc/ada/sem_util.ads
index b5bcd267e33..d1bbc6af0e8 100644
--- a/gcc/ada/sem_util.ads
+++ b/gcc/ada/sem_util.ads
@@ -1475,6 +1475,9 @@ package Sem_Util is
    --  Return True if the loop has no side effect and can therefore be
    --  marked for removal. Return False if N is not a N_Loop_Statement.
 
+   function Is_Container_Aggregate (Exp : Node_Id) return Boolean;
+   --  Is the given expression a container aggregate?
+
    function Is_Newly_Constructed
      (Exp : Node_Id; Context_Requires_NC : Boolean) return Boolean;
    --  Indicates whether a given expression is "newly constructed" (RM 4.4).
-- 
2.40.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [COMMITTED] ada: Fix crash on vector initialization
  2023-05-29  8:29 [COMMITTED] ada: Fix crash on vector initialization Marc Poulhiès
@ 2023-07-07  8:49 ` Eric Botcazou
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Botcazou @ 2023-07-07  8:49 UTC (permalink / raw)
  To: Marc Poulhiès; +Cc: gcc-patches

> Such assignments to container aggregates are later transformed into
> procedure calls to the procedures named in the Aggregate aspect
> definition, for which the delayed expansion is not required/expected.
> 
> gcc/ada/
> 
> 	* exp_aggr.adb (Convert_To_Assignments): Do not mark node for
> 	delayed expansion if parent type has the Aggregate aspect.
> 	* sem_util.adb (Is_Container_Aggregate): Move...
> 	* sem_util.ads (Is_Container_Aggregate): ... here and make it
> 	public.

This is not a regression but the problem is quite visible in Ada 2022 so I 
backported the fix onto the 13 branch.

-- 
Eric Botcazou



^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-07-07  8:49 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-05-29  8:29 [COMMITTED] ada: Fix crash on vector initialization Marc Poulhiès
2023-07-07  8:49 ` Eric Botcazou

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).