From ad277369b236a39fff13c87a51a8e67f7562a80a Mon Sep 17 00:00:00 2001 From: Ed Schonberg Date: Mon, 22 Jul 2019 13:58:27 +0000 Subject: [PATCH] [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 misleading. 2019-07-22 Ed Schonberg gcc/ada/ * 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. gcc/testsuite/ * gnat.dg/warn26.adb: New testcase. From-SVN: r273694 --- gcc/ada/ChangeLog | 8 ++++++++ gcc/ada/exp_ch4.adb | 9 +++++++++ gcc/testsuite/ChangeLog | 4 ++++ gcc/testsuite/gnat.dg/warn26.adb | 20 ++++++++++++++++++++ 4 files changed, 41 insertions(+) create mode 100644 gcc/testsuite/gnat.dg/warn26.adb diff --git a/gcc/ada/ChangeLog b/gcc/ada/ChangeLog index 17d21f54306..ef329453247 100644 --- a/gcc/ada/ChangeLog +++ b/gcc/ada/ChangeLog @@ -1,3 +1,11 @@ +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. + 2019-07-22 Yannick Moy * sem_spark.adb (Get_Root_Object, Is_Path_Expression, diff --git a/gcc/ada/exp_ch4.adb b/gcc/ada/exp_ch4.adb index a062434018a..7ef75f602f4 100644 --- a/gcc/ada/exp_ch4.adb +++ b/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. + then if In_Instance or else (Ekind (Current_Scope) = E_Function @@ -6279,6 +6283,11 @@ package body Exp_Ch4 is then null; + elsif Nkind (Lo_Orig) = N_Type_Conversion + or else Nkind (Hi_Orig) = N_Type_Conversion + then + null; + else Substitute_Valid_Check; goto Leave; diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 2ac298f87ce..d8daa2dbdde 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,7 @@ +2019-07-22 Ed Schonberg + + * gnat.dg/warn26.adb: New testcase. + 2019-07-22 Javier Miranda * gnat.dg/class_wide5.adb: New testcase. diff --git a/gcc/testsuite/gnat.dg/warn26.adb b/gcc/testsuite/gnat.dg/warn26.adb new file mode 100644 index 00000000000..08b681ffc1a --- /dev/null +++ b/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); +begin + if Input_Period in + Integer (Monitor_Period'First) .. Integer ( Monitor_Period'Last) + then + null; + end if; +end Warn26; -- 2.30.2