[Ada] Warning on redundant conversion

Submitted by Arnaud Charlet on Sept. 10, 2010, 10:29 a.m.


Message ID 20100910102903.GA30561@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Sept. 10, 2010, 10:29 a.m.
If the argument of a conversion is a qualified expression with the same type,
the compiler will warn that the conversion is redundant. However, the qualifi-
cation may be used to disambiguate a function call in an context that requires
a name, and the conversion is used to provide it. This patch disables the
warning in this special case.

The following must compile quietly:

   gcc -c -gnatwa redconv.adb
procedure RedConv is
   type E is record
      C : Integer;
   end record;

   type EE is new E;

   function Get return E is
      R : constant E := (C => 2);
      return R;
   end Get;

   function Get return EE is
      R : constant EE := (C => 1);
      return R;
   end Get;

   V : Integer;

   V := 1 + E (E'(Get)).C;
   V := 1 + EE (EE'(Get)).C;
   if V > 5 then null; end if;
end RedConv;

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

2010-09-10  Ed Schonberg  <schonberg@adacore.com>

	* sem_res.adb (Resolve_Type_Conversion): Do not warn on a redundant
	conversion is the expression is a qualified expression used to
	disambiguate a function call.

Patch hide | download patch | download mbox

Index: sem_res.adb
--- sem_res.adb	(revision 164157)
+++ sem_res.adb	(working copy)
@@ -8831,6 +8831,19 @@ 
+            --  Finally, the expression may be a qualified expression whose
+            --  own expression is a possibly overloaded function call. The
+            --  qualified expression is needed to be disambiguate the call,
+            --  but it appears in a context in which a name is needed, forcing
+            --  the use of a conversion.
+            --  In Ada2012 a qualified expression is a name, and this idiom
+            --  is not needed any longer.
+            elsif Nkind (Orig_N) = N_Qualified_Expression
+              and then Nkind (Expression (Orig_N)) = N_Function_Call
+            then
+               null;
             --  Here we give the redundant conversion warning. If it is an
             --  entity, give the name of the entity in the message. If not,
             --  just mention the expression.