]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
Ada: Fix use type clause invalidated by use clause in nested package
authorEric Botcazou <ebotcazou@adacore.com>
Sun, 2 Nov 2025 15:43:47 +0000 (16:43 +0100)
committerEric Botcazou <ebotcazou@adacore.com>
Sun, 2 Nov 2025 15:45:57 +0000 (16:45 +0100)
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.

gcc/ada/sem_ch8.adb
gcc/testsuite/gnat.dg/use_type4.adb [new file with mode: 0644]

index e9d00d0d4a292920dc5b071be9d6c30daff38c99..a83ac645e928bc920efa3eb31d8ed39fa78adaff 100644 (file)
@@ -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 (file)
index 0000000..5ceb288
--- /dev/null
@@ -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;