https://gcc.gnu.org/g:a929418aa211a8f12a57d96368dcc1ba2505f079

commit r16-6606-ga929418aa211a8f12a57d96368dcc1ba2505f079
Author: Viljar Indus <[email protected]>
Date:   Thu Nov 27 11:07:19 2025 +0200

    ada: Fix crash when checking ghost levels of call arguments
    
    We should avoid the check if the called entity cannot have formals
    
    gcc/ada/ChangeLog:
    
            * einfo-utils.adb (Can_Have_Formals): New function for checking
            if the entity can support formals.
            * einfo-utils.ads (Can_Have_Formals): Likewise.
            * ghost.adb (Check_Procedure_Call_Argument_Levels): Avoid checking
            the type of the formal if the called entity cannot hold formals.

Diff:
---
 gcc/ada/einfo-utils.adb | 26 +++++++++-----------------
 gcc/ada/einfo-utils.ads |  3 +++
 gcc/ada/ghost.adb       |  3 ++-
 3 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/gcc/ada/einfo-utils.adb b/gcc/ada/einfo-utils.adb
index 22f50221ddcd..87fb2f56af75 100644
--- a/gcc/ada/einfo-utils.adb
+++ b/gcc/ada/einfo-utils.adb
@@ -704,6 +704,12 @@ package body Einfo.Utils is
       end return;
    end Base_Type_If_Set;
 
+   function Can_Have_Formals (Id : Entity_Id) return Boolean
+   is (Is_Generic_Subprogram (Id)
+       or else Is_Overloadable (Id)
+       or else Ekind (Id)
+               in E_Entry_Family | E_Subprogram_Body | E_Subprogram_Type);
+
    ----------------------
    -- Declaration_Node --
    ----------------------
@@ -856,12 +862,7 @@ package body Einfo.Utils is
       Formal : Entity_Id;
 
    begin
-      pragma Assert
-        (Is_Generic_Subprogram (Id)
-           or else Is_Overloadable (Id)
-           or else Ekind (Id) in E_Entry_Family
-                               | E_Subprogram_Body
-                               | E_Subprogram_Type);
+      pragma Assert (Can_Have_Formals (Id));
 
       if Ekind (Id) = E_Enumeration_Literal then
          return Empty;
@@ -897,12 +898,7 @@ package body Einfo.Utils is
       Formal : Entity_Id;
 
    begin
-      pragma Assert
-        (Is_Generic_Subprogram (Id)
-           or else Is_Overloadable (Id)
-           or else Ekind (Id) in E_Entry_Family
-                               | E_Subprogram_Body
-                               | E_Subprogram_Type);
+      pragma Assert (Can_Have_Formals (Id));
 
       if Ekind (Id) = E_Enumeration_Literal then
          return Empty;
@@ -1823,11 +1819,7 @@ package body Einfo.Utils is
       Formal : Entity_Id;
 
    begin
-      pragma Assert
-        (Is_Overloadable (Id)
-          or else Ekind (Id) in E_Entry_Family
-                              | E_Subprogram_Body
-                              | E_Subprogram_Type);
+      pragma Assert (Can_Have_Formals (Id));
 
       if Ekind (Id) = E_Enumeration_Literal then
          return Empty;
diff --git a/gcc/ada/einfo-utils.ads b/gcc/ada/einfo-utils.ads
index 212caf0ddf22..858652739a2f 100644
--- a/gcc/ada/einfo-utils.ads
+++ b/gcc/ada/einfo-utils.ads
@@ -551,6 +551,9 @@ package Einfo.Utils is
    --  Call [Set_]Is_Volatile_Type/Is_Volatile_Object as appropriate for the
    --  Ekind of Id.
 
+   function Can_Have_Formals (Id : Entity_Id) return Boolean;
+   --  A utility function to see whether the entity can have formals.
+
    function Convention
      (N : Entity_Id) return Convention_Id renames Basic_Convention;
    procedure Set_Convention (E : Entity_Id; Val : Convention_Id);
diff --git a/gcc/ada/ghost.adb b/gcc/ada/ghost.adb
index 58d320006eb1..5231ef90e084 100644
--- a/gcc/ada/ghost.adb
+++ b/gcc/ada/ghost.adb
@@ -2334,13 +2334,14 @@ package body Ghost is
       --  we are not dealing with an expanded construct.
 
       if Present (Id)
+        and then Can_Have_Formals (Id)
         and then Comes_From_Source (N)
         and then Ghost_Config.Ghost_Mode /= None
       then
          Orig_Actual := First_Actual (N);
          Formal := First_Formal (Id);
 
-         while Present (Orig_Actual) loop
+         while Present (Orig_Actual) and then Present (Formal) loop
             --  Similarly to Mark_And_Set_Ghost_Procedure_Call we need to
             --  analyze the call argument first to get its level for this
             --  analysis.

Reply via email to