diff mbox series

[Ada] Spurious error and missing warning on static predicate

Message ID 20171215141033.GA97798@adacore.com
State New
Headers show
Series [Ada] Spurious error and missing warning on static predicate | expand

Commit Message

Pierre-Marie de Rodat Dec. 15, 2017, 2:10 p.m. UTC
This patch handles properly a static predicate on a scalar type that
is trivially true. Previous to this patch the compiler rejected the
predicate on the incorrect grounds that it was not a static expression.

Compiling bad_days.ads must yield:

   bad_days.ads:4:34: warning: predicate is redundant (always True)

---
package Bad_Days is
 type Day is (Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday);
 subtype Day_Bad is Day with 
     Static_Predicate => Day_Bad in Day;
end Bad_Days;

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

2017-12-15  Ed Schonberg  <schonberg@adacore.com>

	* exp_ch4.adb (Expand_N_In): Do not replace a membership test on a
	scalar type with a validity test when the membership appears in a
	predicate expression, to prevent a spurious error when predicate is
	specified static.
	* sem_ch13.adb (Build_Predicate_Functions): Add warning if a static
	predicate, after constant-folding, reduces to True and is this
	redundant.
	* par-ch4.adb: Typo fixes and minor reformattings.
diff mbox series

Patch

Index: exp_ch4.adb
===================================================================
--- exp_ch4.adb	(revision 255693)
+++ exp_ch4.adb	(working copy)
@@ -6015,10 +6015,20 @@ 
               --  have a test in the generic that makes sense with some types
               --  and not with other types.
 
-              and then not In_Instance
+              --  Similarly, do not rewrite membership as a validity check if
+              --  within the predicate function for the type.
+
             then
-               Substitute_Valid_Check;
-               goto Leave;
+               if In_Instance
+                 or else (Ekind (Current_Scope) = E_Function
+                           and then Is_Predicate_Function (Current_Scope))
+               then
+                  null;
+
+               else
+                  Substitute_Valid_Check;
+                  goto Leave;
+               end if;
             end if;
 
             --  If we have an explicit range, do a bit of optimization based on
Index: par-ch4.adb
===================================================================
--- par-ch4.adb	(revision 255693)
+++ par-ch4.adb	(working copy)
@@ -645,8 +645,8 @@ 
          --      case of a name which can be extended in the normal manner.
          --      This case is handled by LP_State_Name or LP_State_Expr.
 
-         --      (Ada2020) : the expression can be a reduction_expression_
-         --      psarameter, i.e. a box or  < Simple_Expression >
+         --      (Ada 2020): the expression can be a reduction_expression_
+         --      parameter, i.e. a box or < Simple_Expression >.
 
          --      Note: if and case expressions (without an extra level of
          --      parentheses) are permitted in this context).
@@ -679,7 +679,7 @@ 
          end if;
 
          --  Here we have an expression after all, which may be a reduction
-         --  expression with a binary operator
+         --  expression with a binary operator.
 
          if Token = Tok_Less then
             Scan; -- past <
@@ -2894,7 +2894,7 @@ 
                Node1 := P_Name;
                return Node1;
 
-            --  Ada2020: reduction expression parameter
+            --  Ada 2020: reduction expression parameter
 
             when Tok_Less =>
                Scan; -- past <
Index: sem_ch13.adb
===================================================================
--- sem_ch13.adb	(revision 255678)
+++ sem_ch13.adb	(working copy)
@@ -11919,6 +11919,12 @@ 
       then
          return True;
 
+      elsif Is_Entity_Name (Expr)
+        and then Entity (Expr) = Standard_True
+      then
+         Error_Msg_N ("predicate is redundant (always True)?", Expr);
+         return True;
+
       --  That's an exhaustive list of tests, all other cases are not
       --  predicate-static, so we return False.
 
Index: sem_ch4.adb
===================================================================
--- sem_ch4.adb	(revision 255693)
+++ sem_ch4.adb	(working copy)
@@ -4155,7 +4155,7 @@ 
               and then Parent (Loop_Par) /= N
             then
                --  The parser cannot distinguish between a loop specification
-               --  and an iterator specification. If after pre-analysis the
+               --  and an iterator specification. If after preanalysis the
                --  proper form has been recognized, rewrite the expression to
                --  reflect the right kind. This is needed for proper ASIS
                --  navigation. If expansion is enabled, the transformation is
@@ -4378,7 +4378,7 @@ 
               and then Parent (Loop_Par) /= N
             then
                --  The parser cannot distinguish between a loop specification
-               --  and an iterator specification. If after pre-analysis the
+               --  and an iterator specification. If after preanalysis the
                --  proper form has been recognized, rewrite the expression to
                --  reflect the right kind. This is needed for proper ASIS
                --  navigation. If expansion is enabled, the transformation is