[COMMITTED,24/30] ada: Error on instantiation of generic containing legal container aggregate

Message ID 20240520074858.222435-24-poulhies@adacore.com
State Committed
Commit a74dff4d9d0b26e91d02acb81e9ed2e34d0d38e9
Headers
Series [COMMITTED,01/30] ada: Rework and augment documentation on strict aliasing |

Commit Message

Marc Poulhiès May 20, 2024, 7:48 a.m. UTC
  From: Gary Dismukes <dismukes@adacore.com>

When a container aggregate for a predefined container type (such as
a Vector type) that has an iterated component association occurs within
a generic unit and that generic is instantiated, the compiler reports
a spurious error message "iterated component association can only appear
in an array aggregate" and the compilation aborts (because Unrecoverable_Error
is raised unconditionally after that error). The problem is that as part of
the instantiation process, for aggregates whose type has a partial view,
in Copy_Generic_Node the compiler switches the visibility so that the full
view of the type is available, and for a type whose full view is a record
type this leads to incorrectly trying to process the aggregate as a record
aggregate in Resolve_Aggregate (making a call to Resolve_Record_Aggregate).

Rather than trying to address this by changing what Copy_Generic_Node does,
this can be fixed by reordering and adjusting the code in Resolve_Aggregate,
so that we first test whether we need to resolve as a record aggregate
(if the aggregate is not homogeneous), followed by testing whether the
type has an Aggregate aspect and calling Resolve_Container_Aggregate.
As a bonus, we also remove the subsequent complex condition and redundant
code for handling null container aggregates.

gcc/ada/

	* sem_aggr.adb (Resolve_Aggregate): Move condition and call for
	Resolve_Record_Aggregate in front of code related to calling
	Resolve_Container_Aggregate (and add test that the aggregate is
	not homogeneous), and remove special-case testing and call to
	Resolve_Container_Aggregate for empty aggregates. Also, add error
	check for an attempt to use "[]" for an aggregate of a record type
	that does not specify an Aggregate aspect.
	(Resolve_Record_Aggregate): Remove error check for record
	aggregates with "[]" (now done by Resolve_Aggregate).

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

---
 gcc/ada/sem_aggr.adb | 17 ++++++++---------
 1 file changed, 8 insertions(+), 9 deletions(-)
  

Patch

diff --git a/gcc/ada/sem_aggr.adb b/gcc/ada/sem_aggr.adb
index 6e40e5c2564..60738550ec1 100644
--- a/gcc/ada/sem_aggr.adb
+++ b/gcc/ada/sem_aggr.adb
@@ -1198,6 +1198,14 @@  package body Sem_Aggr is
 
          Resolve_Container_Aggregate (N, Typ);
 
+      --  Check for an attempt to use "[]" for an aggregate of a record type
+      --  after handling the case where the type has an Aggregate aspect,
+      --  because the aspect can be specified for record types, but if it
+      --  wasn't specified, then this is an error.
+
+      elsif Is_Record_Type (Typ) and then Is_Homogeneous_Aggregate (N) then
+         Error_Msg_N ("record aggregate must use (), not '[']", N);
+
       elsif Is_Array_Type (Typ) then
 
          --  First a special test, for the case of a positional aggregate of
@@ -5518,15 +5526,6 @@  package body Sem_Aggr is
          return;
       end if;
 
-      --  A record aggregate can only use parentheses
-
-      if Nkind (N) = N_Aggregate
-        and then Is_Homogeneous_Aggregate (N)
-      then
-         Error_Msg_N ("record aggregate must use (), not '[']", N);
-         return;
-      end if;
-
       --  STEP 2: Verify aggregate structure
 
       Step_2 : declare