https://gcc.gnu.org/g:21742b80f95af8b884cc45d4f0752578a5462345

commit r16-4933-g21742b80f95af8b884cc45d4f0752578a5462345
Author: Eric Botcazou <[email protected]>
Date:   Sun Nov 2 16:43:47 2025 +0100

    Ada: Fix use type clause invalidated by use clause in nested package
    
    gcc/ada/
            PR ada/52319
            * sem_ch8.adb (End_Use_Package): Use the scope of the operator.
    
    gcc/testsuite/
            * gnat.dg/use_type4.adb: New test.

Diff:
---
 gcc/ada/sem_ch8.adb                 | 24 +++++++++++++++---------
 gcc/testsuite/gnat.dg/use_type4.adb | 29 +++++++++++++++++++++++++++++
 2 files changed, 44 insertions(+), 9 deletions(-)

diff --git a/gcc/ada/sem_ch8.adb b/gcc/ada/sem_ch8.adb
index e9d00d0d4a29..a83ac645e928 100644
--- a/gcc/ada/sem_ch8.adb
+++ b/gcc/ada/sem_ch8.adb
@@ -5330,11 +5330,6 @@ package body Sem_Ch8 is
    ---------------------
 
    procedure End_Use_Package (N : Node_Id) is
-      Pack      : Entity_Id;
-      Pack_Name : Node_Id;
-      Id        : Entity_Id;
-      Elmt      : Elmt_Id;
-
       function Type_In_Use (T : Entity_Id; P : Entity_Id) return Boolean;
       --  Check whether type T is declared in P and appears in an active
       --  use_type clause.
@@ -5349,6 +5344,14 @@ package body Sem_Ch8 is
          return Scope (BT) = P and then (In_Use (T) or else In_Use (BT));
       end Type_In_Use;
 
+      --  Local variables
+
+      Elmt      : Elmt_Id;
+      Id        : Entity_Id;
+      Pack      : Entity_Id;
+      Pack_Name : Node_Id;
+      Scop      : Entity_Id;
+
    --  Start of processing for End_Use_Package
 
    begin
@@ -5373,17 +5376,20 @@ package body Sem_Ch8 is
 
                --  Preserve use-visibility of operators that are primitive
                --  operators of a type that is use-visible through an active
-               --  use_type_clause.
+               --  use_type_clause. Note that we compare with the scope of
+               --  the operator and not Pack itself, lest Pack be a renaming.
+
+               Scop := Scope (Id);
 
                if Nkind (Id) = N_Defining_Operator_Symbol
                  and then
-                   (Type_In_Use (Etype (Id), Pack)
-                     or else Type_In_Use (Etype (First_Formal (Id)), Pack)
+                   (Type_In_Use (Etype (Id), Scop)
+                     or else Type_In_Use (Etype (First_Formal (Id)), Scop)
                      or else
                        (Present (Next_Formal (First_Formal (Id)))
                          and then
                            Type_In_Use
-                             (Etype (Next_Formal (First_Formal (Id))), Pack)))
+                             (Etype (Next_Formal (First_Formal (Id))), Scop)))
                then
                   null;
                else
diff --git a/gcc/testsuite/gnat.dg/use_type4.adb 
b/gcc/testsuite/gnat.dg/use_type4.adb
new file mode 100644
index 000000000000..5ceb28877fe4
--- /dev/null
+++ b/gcc/testsuite/gnat.dg/use_type4.adb
@@ -0,0 +1,29 @@
+-- { dg-do compile }
+
+procedure Use_Type4 is
+
+  package P1 is
+    type T is new Integer;
+    function "and" (L, R : in Integer) return T;
+  end P1;
+
+  package body P1 is
+    function "and" (L, R : in Integer) return T is
+    begin
+      return T (L * R);
+    end "and";
+  end P1;
+
+  use type P1.T;
+
+  package Renaming renames P1;
+
+  package P2 is
+    use Renaming;
+  end P2;
+
+  G : P1.T := Integer'(1) and Integer'(2);
+
+begin
+  null;
+end;

Reply via email to