]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Improve error message in the case of missing indices
authorViljar Indus <indus@adacore.com>
Tue, 5 Aug 2025 13:16:04 +0000 (16:16 +0300)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Tue, 9 Sep 2025 12:39:56 +0000 (14:39 +0200)
When both a missing index and an out of bounds error are present then
we should also mention the error on out of bounds index as it suggests
removing the index that is out of bounds rather than suggesting adding
indices that are also out of bounds.

gcc/ada/ChangeLog:

* sem_aggr.adb (Resolve_Array_Aggregate): Indicate an out of
bounds index error also in the case of a missing index.

gcc/ada/sem_aggr.adb

index 58460b8e3bb860edc88e4c40f1b0836be41b5b2f..bbd0eaefb98e241eb17ef0245fb689b94d8548bc 100644 (file)
@@ -3331,6 +3331,24 @@ package body Sem_Aggr is
                      --  Not needed if others, since missing impossible.
 
                      if No (Others_N) then
+                        --  We are dealing with a gap in the indicies. However
+                        --  we should also check for an out of bounds error. If
+                        --  there exists an out of bounds error then we should
+                        --  not be suggesting that the user should add the
+                        --  missing indices but rather remove the ones that are
+                        --  out of bounds.
+
+                        Aggr_Low := Table (1).Lo;
+                        Aggr_High := Table (Nb_Discrete_Choices).Hi;
+
+                        Check_Bounds
+                          (Index_Typ_Low, Index_Typ_High, Aggr_Low, Aggr_High);
+                        Check_Bounds
+                          (Index_Base_Low,
+                           Index_Base_High,
+                           Aggr_Low,
+                           Aggr_High);
+
                         for J in 2 .. Nb_Discrete_Choices loop
                            Lo_Val := Expr_Value (Table (J).Lo);
                            Hi_Val := Table (J - 1).Highest;
@@ -3358,11 +3376,13 @@ package body Sem_Aggr is
                                  if Hi_Val + 1 = Lo_Val - 1 then
                                     Error_Msg_N
                                       ("missing index value "
-                                       & "in array aggregate!", Error_Node);
+                                       & "in array aggregate!",
+                                       Error_Node);
                                  else
                                     Error_Msg_N
                                       ("missing index values "
-                                       & "in array aggregate!", Error_Node);
+                                       & "in array aggregate!",
+                                       Error_Node);
                                  end if;
 
                                  Output_Bad_Choices