[COMMITTED,16/31] ada: Fix visibility bug related to target name

Message ID 20250911091904.1505690-16-poulhies@adacore.com
State Committed
Commit 629f1de233948a06f1c9e0064b2c2804892a02e6
Headers
Series [COMMITTED,01/31] ada: Disable new warning for composite equality ops that can raise Program_Error |

Commit Message

Marc Poulhiès Sept. 11, 2025, 9:18 a.m. UTC
  From: Bob Duff <duff@adacore.com>

This patch fixes the following bug:
If the right-hand side of an expression contains a target name
(i.e. "@"), and also contains a reference to a user-defined operator
that is directly visible because of a "use type" clause on a renaming of
the package where the operator is declared, the compiler gives an
incorrect error saying that the renamed package is not visible.

It turns out that setting Entity of resolved nodes is unnecessary
and wrong; the fix is to simply remove that code.

gcc/ada/ChangeLog:

	* exp_ch5.adb
	(Expand_Assign_With_Target_Names.Replace_Target):
	Remove code setting Entity to Empty.
	* sinfo.ads (Has_Target_Names):
	Improve comment: add "@" to clarify what "target name"
	means, and remove the content-free phrase "and must
	be expanded accordingly."

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_ch5.adb | 17 ++---------------
 gcc/ada/sinfo.ads   |  2 +-
 2 files changed, 3 insertions(+), 16 deletions(-)
  

Patch

diff --git a/gcc/ada/exp_ch5.adb b/gcc/ada/exp_ch5.adb
index d78df5c8315a..0949d5864bb5 100644
--- a/gcc/ada/exp_ch5.adb
+++ b/gcc/ada/exp_ch5.adb
@@ -2254,7 +2254,8 @@  package body Exp_Ch5 is
       function Replace_Target (N : Node_Id) return Traverse_Result;
       --  Replace occurrences of the target name by the proper entity: either
       --  the entity of the LHS in simple cases, or the formal of the
-      --  constructed procedure otherwise.
+      --  constructed procedure otherwise. Mark all nodes as Analyzed=False
+      --  so reanalysis will occur.
 
       --------------------
       -- Replace_Target --
@@ -2264,20 +2265,6 @@  package body Exp_Ch5 is
       begin
          if Nkind (N) = N_Target_Name then
             Rewrite (N, New_Occurrence_Of (Ent, Sloc (N)));
-
-         --  The expression will be reanalyzed when the enclosing assignment
-         --  is reanalyzed, so reset the entity, which may be a temporary
-         --  created during analysis, e.g. a loop variable for an iterated
-         --  component association. However, if entity is callable then
-         --  resolution has established its proper identity (including in
-         --  rewritten prefixed calls) so we must preserve it.
-
-         elsif Is_Entity_Name (N) then
-            if Present (Entity (N))
-              and then not Is_Overloadable (Entity (N))
-            then
-               Set_Entity (N, Empty);
-            end if;
          end if;
 
          Set_Analyzed (N, False);
diff --git a/gcc/ada/sinfo.ads b/gcc/ada/sinfo.ads
index eb9529f52cec..bde67a05b6e6 100644
--- a/gcc/ada/sinfo.ads
+++ b/gcc/ada/sinfo.ads
@@ -1476,7 +1476,7 @@  package Sinfo is
 
    --  Has_Target_Names
    --    Present in assignment statements. Indicates that the RHS contains
-   --    target names (see AI12-0125-3) and must be expanded accordingly.
+   --    target names ("@" -- see AI12-0125-3).
 
    --  Has_Wide_Character
    --    Present in string literals, set if any wide character (i.e. character