[Ada] Avoid false positive on subtype conversion warning

Arnaud Charlet charlet@adacore.com
Fri Oct 30 13:17:00 GMT 2009


In some circumstances it is necessary to use a conversion which at
first glance has the same source type and destination type, and
therefore looks redundant. But in fact the expression's type has
non-static bounds because of technical rules as to what is or is
not static. So the conversion is not after all redundant. This
patch avoids the false positive warning in this case.

The following example shows the case in question:

Compiling: badconversionwarn.adb

     1. procedure badconversionwarn is
     2.    type INTRA_CHECK_T is RANGE 1..11;
     3.
     4.    subtype TT is INTRA_CHECK_T range 1 .. 1;
     5.    type INTRA_CHECK_TO_DO_T is array (TT) of BOOLEAN;
     6.
     7.     type R is record
     8.        X : INTRA_CHECK_TO_DO_T;
     9.     end record;
    10.
    11.     A : constant R := R'(X => (1 => False));
    12. begin
    13.      for I in A.X'range loop
    14.        case TT (I) is
    15.            when 1 => null;
    16.        end case;
    17.
    18.        case I is
               1    3
        >>> missing case values: -128 .. 0
        >>> missing case values: 2 .. 127
        >>> bounds of "I" are not static, alternatives must
            cover base type

    19.           when 1 => null;
    20.        end case;
    21.
    22.        case TT'(I) is
    23.            when 1 => null;
    24.        end case;
    25.      end loop;
    26. end;

This is compiled with -gnatwa -gnatld7 -gnatj60. Before the patch,
the apparently redundant conversion on line 14 was flagged with a
warning. But as line 18 shows, this conversion si not redundant.

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

2009-10-30  Robert Dewar  <dewar@adacore.com>

	* sem_res.adb (Resolve_Type_Conversion): Avoid false positive when
	converting non-static subtype to "identical" static subtype.

-------------- next part --------------
Index: sem_res.adb
===================================================================
--- sem_res.adb	(revision 153739)
+++ sem_res.adb	(working copy)
@@ -8254,8 +8254,8 @@ package body Sem_Res is
    -----------------------------
 
    procedure Resolve_Type_Conversion (N : Node_Id; Typ : Entity_Id) is
-      Conv_OK     : constant Boolean := Conversion_OK (N);
-      Operand     : constant Node_Id := Expression (N);
+      Conv_OK     : constant Boolean   := Conversion_OK (N);
+      Operand     : constant Node_Id   := Expression (N);
       Operand_Typ : constant Entity_Id := Etype (Operand);
       Target_Typ  : constant Entity_Id := Etype (N);
       Rop         : Node_Id;
@@ -8400,9 +8400,25 @@ package body Sem_Res is
                   (Ekind (Entity (Orig_N)) = E_Loop_Parameter
                      and then Covers (Orig_T, Etype (Entity (Orig_N)))))
          then
-            Error_Msg_Node_2 := Orig_T;
-            Error_Msg_NE -- CODEFIX
-              ("?redundant conversion, & is of type &!", N, Entity (Orig_N));
+            --  One more check, do not give warning if the analyzed conversion
+            --  has an expression with non-static bounds, and the bounds of the
+            --  target are static. This avoids junk warnings in cases where the
+            --  conversion is necessary to establish staticness, for example in
+            --  a case statement.
+
+            if not Is_OK_Static_Subtype (Operand_Typ)
+              and then Is_OK_Static_Subtype (Target_Typ)
+            then
+               null;
+
+            --  Here we give the redundant conversion warning
+
+            else
+               Error_Msg_Node_2 := Orig_T;
+               Error_Msg_NE -- CODEFIX
+                 ("?redundant conversion, & is of type &!",
+                  N, Entity (Orig_N));
+            end if;
          end if;
       end if;
 


More information about the Gcc-patches mailing list