This implements a minimal form of the old RM 8.5.4(6) rule, which forbids the 
use of (the name of) a formal parameter of the specification in the name of a 
renaming subprogram declaration; it turns out that implementing the full rule 
breaks existing code that works fine otherwise.

Tested on x86-64/Linux, applied on the mainline.


2025-12-30  Eric Botcazou  <[email protected]>

        PR ada/15605
        * sem_ch8.adb (Analyze_Subprogram_Renaming): Give an error if the
        name is also that of a formal parameter of the specification.


2025-12-30  Eric Botcazou  <[email protected]>

        * gnat.dg/specs/profile1.ads: New test.

-- 
Eric Botcazou
diff --git a/gcc/ada/sem_ch8.adb b/gcc/ada/sem_ch8.adb
index 11f2b19b0b0..8f534af7e1b 100644
--- a/gcc/ada/sem_ch8.adb
+++ b/gcc/ada/sem_ch8.adb
@@ -3678,13 +3678,34 @@ package body Sem_Ch8 is
       then
          --  Do not mention the renaming if it comes from an instance
 
-         if not Is_Actual then
-            Error_Msg_N ("expect valid subprogram name in renaming", N);
-         else
+         if Is_Actual then
             Error_Msg_NE ("no visible subprogram for formal&", N, Nam);
+         else
+            Error_Msg_N ("expect valid subprogram name in renaming", N);
          end if;
 
          return;
+
+      --  RM 8.5.4(6): A name that denotes a formal parameter of the subprogram
+      --  specification is not allowed within Nam. But this was not enforced by
+      --  GNAT historically, so we restrict it to direct names.
+
+      elsif Nkind (Nam) = N_Identifier and then not Is_Actual then
+         declare
+            F : Node_Id;
+
+         begin
+            F := First_Formal (New_S);
+            while Present (F) loop
+               if Chars (F) = Chars (Nam) then
+                  Error_Msg_NE
+                    ("formal parameter& cannot be used in renaming", N, F);
+                  return;
+               end if;
+
+               Next_Formal (F);
+            end loop;
+         end;
       end if;
 
       --  Find the renamed entity that matches the given specification. Disable
-- PR ada/15605
-- { dg-do compile }

package Profile1 is

  subtype Int is Integer;
  function F (Int : Integer) return Int; -- { dg-error "formal parameter" }

  procedure Foo (X : Integer);
  procedure P (Foo : Integer) renames Foo; -- { dg-error "formal parameter" }

end Profile1;

Reply via email to