[Ada] Spurious warning on call with out parameter in expression function

Message ID 20191010152947.GA87649@adacore.com
State New
Headers show
  • [Ada] Spurious warning on call with out parameter in expression function
Related show

Commit Message

Pierre-Marie de Rodat Oct. 10, 2019, 3:29 p.m.
If the exprsssion in an expression function includes a function call
with an out-parameter, the corresponding assignment may be flagged as
redundant because it is analyzed twice, once in the expression function
and once in the body constructed for it.

Running this command:

  gcc -c -gnatg warn.adb

On the following sources:

package Warn is
   function T1 (Y : Integer; X : out Integer) return Boolean
    with Import;
   function T2 (X : out Integer) return Boolean is (T1 (20, X));
end Warn;

Should execute silently.

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

2019-10-10  Ed Schonberg  <schonberg@adacore.com>


	* sem_warn.adb (Warn_On_Useless_Assignment): Do not warn if the
	second assignment is at the same source position as the first.


--- gcc/ada/sem_warn.adb
+++ gcc/ada/sem_warn.adb
@@ -4546,9 +4546,15 @@  package body Sem_Warn is
       --  to capture the value. We are not going to capture any value, but
       --  the warning message depends on the same kind of conditions.
+      --  If the assignment appears as an out-parameter in a call within an
+      --  expression function it may be detected twice: once when expression
+      --  itself is analyzed, and once when the constructed body is analyzed.
+      --  We don't want to emit a spurious warning in this case.
       if Is_Assignable (Ent)
         and then not Is_Return_Object (Ent)
         and then Present (Last_Assignment (Ent))
+        and then Last_Assignment (Ent) /= N
         and then not Is_Imported (Ent)
         and then not Is_Exported (Ent)
         and then Safe_To_Capture_Value (N, Ent)