Patchwork [Ada] Illegal iterators over arrays

login
register
mail settings
Submitter Arnaud Charlet
Date Sept. 5, 2011, 1:43 p.m.
Message ID <20110905134315.GA25859@adacore.com>
Download mbox | patch
Permalink /patch/113366/
State New
Headers show

Comments

Arnaud Charlet - Sept. 5, 2011, 1:43 p.m.
In early versions of Ada, an illegal iterator of the form "for X in S" where
S is an array, is most likely an attempt to iterate over the range of S, and
this is the appropriate error message to emit.  In Ada 2012, there is a new
iterator specification form "for E of S", and the error message must indicate
that the new form uses a different keyword.

Compiling the following program in Ada 2012 mode must yield:

    main.adb:7:14: to iterate over the elements of an array, use "of"
    main.adb:15:14: to iterate over the elements of an array, use "of"

Compiling with an earlier version of the language must yield;

    main.adb:7:14: missing Range attribute in iteration over an array
    main.adb:15:14: missing Range attribute in iteration over an array

---
with Ada.Text_IO;
with Tools;
with Type_Declarations;
package body Main is
   procedure Process is
   begin
         for Index in Tools.Result loop
            Ada.Text_IO.Put_Line (Tools.Result (Index)'Img);
         end loop;
   end Process;

   procedure Process2 is
      Ct : constant Type_Declarations.Array_Type := Tools.Result;
   begin
         for Index in Ct loop
            Ada.Text_IO.Put_Line (Tools.Result (Index)'Img);
         end loop;
   end Process2;
end Main;
--
with Type_Declarations;

package Tools is
   function Result
      return Type_Declarations.Array_Type;
end Tools;
---
package Type_Declarations is

   type Integer_Type is new Integer;
   Ct_Max : constant := 40;

   type Large_Index_Type is new Integer range 0 .. Ct_Max;
   subtype Index_Type is Large_Index_Type range 1 .. Large_Index_Type'Last;

   type Array_Type is array (Index_Type range <>) of Integer_Type;
end Type_Declarations;

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

2011-09-05  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch5.adb (Analyze_Iteration_Specification): Improve error
	message on an iterator over an array.

Patch

Index: sem_ch5.adb
===================================================================
--- sem_ch5.adb	(revision 178448)
+++ sem_ch5.adb	(working copy)
@@ -2336,13 +2336,18 @@ 
       if Is_Array_Type (Typ) then
          if Of_Present (N) then
             Set_Etype (Def_Id, Component_Type (Typ));
+
+         elsif Ada_Version < Ada_2012 then
+            Error_Msg_N
+              ("missing Range attribute in iteration over an array", N);
+
          else
             Error_Msg_N
               ("to iterate over the elements of an array, use OF", N);
 
             --  Prevent cascaded errors
 
-            Set_Ekind (Def_Id, E_Constant);
+            Set_Ekind (Def_Id, E_Loop_Parameter);
             Set_Etype (Def_Id, Etype (First_Index (Typ)));
          end if;