This patch improves the error message on an iterator specification whose name
is a function call that does not yield a type that implements an iterator
interface.

Compiling try_containers.adb must yield:

   try_containers.adb:17:18: expect object that implements iterator interface

--
with Ada.Text_Io; use Ada.Text_Io;
with Ada.Containers.Vectors;
procedure Try_Containers
is
   package Integer_Vectors is new Ada.Containers.Vectors (Natural, Integer);
   use Integer_Vectors;

   A : Vector := To_Vector (1, 10);
begin
   Loop_1 :
   for Element of A loop
      Put_Line ("A (i) => " & Integer'Image (Element));
      -- can't do Element := 2;
   end loop Loop_1;

   Loop_2 :
   for Cursor in First (A) loop -- oops! should be:
   --  for Cursor in Iterate (A) loop

      Put_Line ("A (I) => " & Integer'Image (Element (Cursor)));
      Replace_Element (A, Cursor, 2);
      Reference (A, Cursor) := 2;
   end loop Loop_2;

end Try_Containers;

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

2014-10-17  Ed Schonberg  <schonb...@adacore.com>

        * sem_ch5.adb (Analyze_Iterator_Specification): If the domain
        of iteration is given by an expression that is not an array type,
        verify that its type implements an iterator iterface.

Index: sem_ch5.adb
===================================================================
--- sem_ch5.adb (revision 216367)
+++ sem_ch5.adb (working copy)
@@ -1838,6 +1838,17 @@
 
             else
                Typ := Etype (Iter_Name);
+
+               --  Verify that the expression produces an iterator.
+
+               if not Of_Present (N) and then not Is_Iterator (Typ)
+                 and then not Is_Array_Type (Typ)
+                 and then No (Find_Aspect (Typ, Aspect_Iterable))
+               then
+                  Error_Msg_N
+                    ("expect object that implements iterator interface",
+                        Iter_Name);
+               end if;
             end if;
 
             --  Protect against malformed iterator

Reply via email to