Patchwork [Ada] Improve error message on static vs non-static predicate use

login
register
mail settings
Submitter Arnaud Charlet
Date April 25, 2013, 10:31 a.m.
Message ID <20130425103153.GA18710@adacore.com>
Download mbox | patch
Permalink /patch/239473/
State New
Headers show

Comments

Arnaud Charlet - April 25, 2013, 10:31 a.m.
This patch enhances the error message related to the use of a bad predicate.
The compiler now advises on how to remedy the issue if the context warrants it.

------------
-- Source --
------------

--  main.adb

with Ada.Integer_Text_IO; use Ada.Integer_Text_IO;

procedure Main is
   subtype Even is Integer range 1 .. 10 with
     Dynamic_Predicate => Even in 2 | 4 | 6 | 8 | 10;
begin
   for E in Even loop
      Put (E, 3);
   end loop;
end Main;

----------------------------
-- Compilation and output --
----------------------------

$ gcc -c -gnat12 main.adb
main.adb:7:13: cannot use subtype "Even" with non-static predicate for loop
  iteration
main.adb:7:13: predicate of "Even" should be marked static

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

2013-04-25  Hristian Kirtchev  <kirtchev@adacore.com>

	* sem_case.adb (Analyze_Choices): Enhance the error message
	given on a bad use of subtype predicate.
	* sem_ch5.adb (Analyze_Loop_Parameter_Specification): Enhance
	the error message given on a bad use of subtype predicate.
	* sem_util.adb (Bad_Predicated_Subtype_Use): Add formal parameter
	Suggest_Static. Emit an extra error message advising how to
	remedy the bad use of the predicate if the context warrants it.
	* sem_util.ads (Bad_Predicated_Subtype_Use): Add formal parameter
	Suggest_Static along with a comment explaining its usage.

Patch

Index: sem_ch5.adb
===================================================================
--- sem_ch5.adb	(revision 198243)
+++ sem_ch5.adb	(working copy)
@@ -2310,7 +2310,7 @@ 
          then
             Bad_Predicated_Subtype_Use
               ("cannot use subtype& with non-static predicate for loop " &
-               "iteration", DS, Entity (DS));
+               "iteration", DS, Entity (DS), Suggest_Static => True);
          end if;
       end if;
 
Index: sem_util.adb
===================================================================
--- sem_util.adb	(revision 198287)
+++ sem_util.adb	(working copy)
@@ -449,9 +449,10 @@ 
    --------------------------------
 
    procedure Bad_Predicated_Subtype_Use
-     (Msg : String;
-      N   : Node_Id;
-      Typ : Entity_Id)
+     (Msg            : String;
+      N              : Node_Id;
+      Typ            : Entity_Id;
+      Suggest_Static : Boolean := False)
    is
    begin
       if Has_Predicates (Typ) then
@@ -465,6 +466,13 @@ 
          else
             Error_Msg_FE (Msg, N, Typ);
          end if;
+
+         --  Emit an optional suggestion on how to remedy the error if the
+         --  context warrants it.
+
+         if Suggest_Static and then Present (Static_Predicate (Typ)) then
+            Error_Msg_FE ("\predicate of & should be marked static", N, Typ);
+         end if;
       end if;
    end Bad_Predicated_Subtype_Use;
 
Index: sem_util.ads
===================================================================
--- sem_util.ads	(revision 198287)
+++ sem_util.ads	(working copy)
@@ -122,19 +122,21 @@ 
    --  is an error.
 
    procedure Bad_Predicated_Subtype_Use
-     (Msg : String;
-      N   : Node_Id;
-      Typ : Entity_Id);
+     (Msg            : String;
+      N              : Node_Id;
+      Typ            : Entity_Id;
+      Suggest_Static : Boolean := False);
    --  This is called when Typ, a predicated subtype, is used in a context
-   --  which does not allow the use of a predicated subtype. Msg is passed
-   --  to Error_Msg_FE to output an appropriate message using N as the
-   --  location, and Typ as the entity. The caller must set up any insertions
-   --  other than the & for the type itself. Note that if Typ is a generic
-   --  actual type, then the message will be output as a warning, and a
-   --  raise Program_Error is inserted using Insert_Action with node N as
-   --  the insertion point. Node N also supplies the source location for
-   --  construction of the raise node. If Typ is NOT a type with predicates
-   --  this call has no effect.
+   --  which does not allow the use of a predicated subtype. Msg is passed to
+   --  Error_Msg_FE to output an appropriate message using N as the location,
+   --  and Typ as the entity. The caller must set up any insertions other than
+   --  the & for the type itself. Note that if Typ is a generic actual type,
+   --  then the message will be output as a warning, and a raise Program_Error
+   --  is inserted using Insert_Action with node N as the insertion point. Node
+   --  N also supplies the source location for construction of the raise node.
+   --  If Typ does not have any predicates, the call has no effect. Set flag
+   --  Suggest_Static when the context warrants an advice on how to avoid the
+   --  use error.
 
    function Build_Actual_Subtype
      (T : Entity_Id;
Index: sem_case.adb
===================================================================
--- sem_case.adb	(revision 198221)
+++ sem_case.adb	(working copy)
@@ -1260,7 +1260,8 @@ 
                            then
                               Bad_Predicated_Subtype_Use
                                 ("cannot use subtype& with non-static "
-                                 & "predicate as case alternative", Choice, E);
+                                 & "predicate as case alternative", Choice, E,
+                                 Suggest_Static => True);
 
                               --  Static predicate case