This patch adds a warning on a redundant others_clause in an array aggregate
when all index positions are already specified in previous positional or named
associations. The warning is emitted when Warn_On_Redundant_Constructs is
enabled.

Tested on x86_64-pc-linux-gnu, committed on trunk

2018-05-24  Ed Schonberg  <schonb...@adacore.com>

gcc/ada/

        * exp_aggr.adb (Flatten): Add a warning on an others clause in an array
        aggregate with static bounds when named associations cover all index
        positions and the others clause is redundant.

gcc/testsuite/

        * gnat.dg/others1.adb: New testcase.
--- gcc/ada/exp_aggr.adb
+++ gcc/ada/exp_aggr.adb
@@ -4581,6 +4581,12 @@ package body Exp_Aggr is
                         end if;
                      end loop;
 
+                     if Rep_Count = 0
+                       and then Warn_On_Redundant_Constructs
+                     then
+                        Error_Msg_N ("there are no others?r?", Elmt);
+                     end if;
+
                      exit Component_Loop;
 
                   --  Case of a subtype mark, identifier or expanded name

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/others1.adb
@@ -0,0 +1,13 @@
+--  { dg-do compile }
+--  { dg-options "-gnatwr" }
+
+procedure Others1 is
+   type Ar is Array (1..10) of Natural;
+   function five return integer is (5);
+   THing : Ar;
+begin
+   Thing := (1..5 => 22, 6 ..10 => 111, others => Five); --  { dg-warning "there are no others" }
+   if Thing (1) /= thing (5) then
+      raise Program_Error;
+   end if;
+end;

Reply via email to