This is the mail archive of the mailing list for the GCC project.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Remove misleading warning/suggestion in membership test

This patch removes a warning on a membership test whose right operand is
given by a range. In many cases the check can be replaced by the use of
attribute 'Valid, but if the bounds of range are type conversion this
replacement would be invorrect and the warning and suggestion are

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

2019-07-22  Ed Schonberg  <>


	* exp_ch4.adb (Expand_N_In): Do not suggest the use of attribute
	'Valid as a replacement for a range check on a discrete type
	when the bounds of the range are given by type conversions,
	because in such a case there are distinct types involved and the
	subbested attribute replacement would be misplaced.


	* gnat.dg/warn26.adb: New testcase.
--- gcc/ada/exp_ch4.adb
+++ gcc/ada/exp_ch4.adb
@@ -6272,6 +6272,10 @@ package body Exp_Ch4 is
               --  Similarly, do not rewrite membership as a validity check if
               --  within the predicate function for the type.
+              --  Finally, if the original bounds are type conversions, even
+              --  if they have been folded into constants, there are different
+              --  types involved and 'Valid is not appropriate.
                if In_Instance
                  or else (Ekind (Current_Scope) = E_Function
@@ -6279,6 +6283,11 @@ package body Exp_Ch4 is
+               elsif Nkind (Lo_Orig) = N_Type_Conversion
+                 or else Nkind (Hi_Orig) = N_Type_Conversion
+               then
+                  null;
                   goto Leave;

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/warn26.adb
@@ -0,0 +1,20 @@
+--  { dg-do compile }
+procedure Warn26 is
+   Monitor_Period_Min : constant := 5;
+   Monitor_Period_Max : constant := 30;
+   type Monitor_Period is range Monitor_Period_Min .. Monitor_Period_Max;
+   subtype Period_T is Positive range 5 .. 30;
+   function Id (X : Period_T) return Period_T is (X);
+   Input_Period : Period_T := Id (20);
+   if Input_Period in
+      Integer (Monitor_Period'First) .. Integer ( Monitor_Period'Last)
+   then
+      null;
+   end if;
+end Warn26;

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]