]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
[Ada] Fix formal parameter incorrectly visible from outside of instance
authorEric Botcazou <ebotcazou@adacore.com>
Thu, 30 Oct 2025 14:41:09 +0000 (15:41 +0100)
committerEric Botcazou <ebotcazou@adacore.com>
Thu, 30 Oct 2025 14:43:16 +0000 (15:43 +0100)
The problem had been partially fixed two decades ago and the original
testcase correctly rejected, but almost 4 years later the submitter
made a small tweak to it which exposed the issue again...

The original fix was a change to Find_Expanded_Name, this additional fix
is to make exactly the same change to the processing of Collect_Interps
for expanded names.

gcc/ada/
PR ada/15610
* sem_type.adb (Collect_Interps): Apply the same visibility
criterion to expanded names as Find_Expanded_Name.

gcc/testsuite/
* gnat.dg/specs/generic_inst7.ads: New test.
* gnat.dg/specs/generic_inst8.ads: New test.

gcc/ada/sem_type.adb
gcc/testsuite/gnat.dg/specs/generic_inst7.ads [new file with mode: 0644]
gcc/testsuite/gnat.dg/specs/generic_inst8.ads [new file with mode: 0644]

index 32d0833f3a82cbfb3b138a254016e14a7a389adc..31a2acdfc64646043bd2240d9870a7eef5e7a55b 100644 (file)
@@ -610,14 +610,17 @@ package body Sem_Type is
       First_Interp := All_Interp.Last;
       Add_One_Interp (N, Ent, Etype (N));
 
-      --  For expanded name, pick up all additional entities from the
-      --  same scope, since these are obviously also visible. Note that
-      --  these are not necessarily contiguous on the homonym chain.
+      --  For an expanded name, pick up additional visible entities from
+      --  the same scope. Note that these are not necessarily contiguous
+      --  on the homonym chain.
 
       if Nkind (N) = N_Expanded_Name then
          H := Homonym (Ent);
          while Present (H) loop
-            if Scope (H) = Scope (Entity (N)) then
+            if Scope (H) = Scope (Entity (N))
+              and then (not Is_Hidden (H)
+                         or else Is_Immediately_Visible (H))
+            then
                Add_One_Interp (N, H, Etype (H));
             end if;
 
diff --git a/gcc/testsuite/gnat.dg/specs/generic_inst7.ads b/gcc/testsuite/gnat.dg/specs/generic_inst7.ads
new file mode 100644 (file)
index 0000000..3132525
--- /dev/null
@@ -0,0 +1,17 @@
+-- { dg-do compile }
+
+package Generic_Inst7 is
+
+   function F return Integer is (0);
+
+   generic
+     with function Foo return Integer;
+   package P is
+     type Color is (Foo);
+   end P;
+
+   package My_P is new P (F);
+
+   I : Integer := My_P.Foo; -- { dg-error "expected type|found type" }
+
+end Generic_Inst7;
diff --git a/gcc/testsuite/gnat.dg/specs/generic_inst8.ads b/gcc/testsuite/gnat.dg/specs/generic_inst8.ads
new file mode 100644 (file)
index 0000000..0eac709
--- /dev/null
@@ -0,0 +1,18 @@
+-- { dg-do compile }
+
+package Generic_Inst8 is
+
+   function F return Integer is (0);
+
+   generic
+     with function Foo return Integer;
+   package P is
+     type Color1 is (Foo);
+     type Color2 is (Foo);
+   end P;
+
+   package My_P is new P (F);
+
+   I : Integer := My_P.Foo; -- { dg-error "no visible interpretation|use" }
+
+end Generic_Inst8;