ada: sem.adb.process_bodies_in_context: check if spec has lib body before use
authorGhjuvan Lacambre <lacambre@adacore.com>
Fri, 18 Oct 2024 14:15:04 +0000 (16:15 +0200)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Tue, 12 Nov 2024 13:00:45 +0000 (14:00 +0100)
Inspector testing shows that calling Body_Lib_Unit on Spec can sometimes
fail due to the following assertion failing:

            pragma Assert
              (Unit (N) in N_Lib_Unit_Declaration_Id
                | N_Lib_Unit_Renaming_Declaration_Id);

Indeed, Unit (N) may sometimes be an N_Subprogram_Body instead of an
N_Lib_Unit_Declaration_Id.

gcc/ada/ChangeLog:

* sem.adb (Process_Bodies_In_Context): check that Spec's unit is
an N_Lib_Unit_Declaration_Id.

gcc/ada/sem.adb

index fd52e3aea39aa9c6f5cbb5c65b8edd1d4098cc45..cfd0493527487216285c811ae04c1dc4810e7c9d 100644 (file)
@@ -2123,27 +2123,32 @@ package body Sem is
          while Present (Clause) loop
             if Nkind (Clause) = N_With_Clause then
                Spec := Withed_Lib_Unit (Clause);
-               Body_CU := Body_Lib_Unit (Spec);
-
-               --  If we are processing the spec of the main unit, load bodies
-               --  only if the with_clause indicates that it forced the loading
-               --  of the body for a generic instantiation. Note that bodies of
-               --  parents that are instances have been loaded already.
-
-               if Present (Body_CU)
-                 and then Body_CU /= Main_CU
-                 and then Nkind (Unit (Body_CU)) /= N_Subprogram_Body
-                 and then Nkind (Unit (Comp)) /= N_Package_Declaration
+               if Unit (Spec) in N_Lib_Unit_Declaration_Id
+                               | N_Lib_Unit_Renaming_Declaration_Id
                then
-                  Body_U := Get_Cunit_Unit_Number (Body_CU);
+                  Body_CU := Body_Lib_Unit (Spec);
 
-                  if not Seen (Body_U)
-                    and then not Depends_On_Main (Body_CU)
+                  --  If we are processing the spec of the main unit, load
+                  --  bodies only if the with_clause indicates that it forced
+                  --  the loading of the body for a generic instantiation.
+                  --  Note that bodies of parents that are instances have been
+                  --  loaded already.
+
+                  if Present (Body_CU)
+                    and then Body_CU /= Main_CU
+                    and then Nkind (Unit (Body_CU)) /= N_Subprogram_Body
+                    and then Nkind (Unit (Comp)) /= N_Package_Declaration
                   then
-                     Seen (Body_U) := True;
-                     Do_Withed_Units (Body_CU, Include_Limited => False);
-                     Do_Action (Body_CU, Unit (Body_CU));
-                     Done (Body_U) := True;
+                     Body_U := Get_Cunit_Unit_Number (Body_CU);
+
+                     if not Seen (Body_U)
+                       and then not Depends_On_Main (Body_CU)
+                     then
+                        Seen (Body_U) := True;
+                        Do_Withed_Units (Body_CU, Include_Limited => False);
+                        Do_Action (Body_CU, Unit (Body_CU));
+                        Done (Body_U) := True;
+                     end if;
                   end if;
                end if;
             end if;
This page took 0.148441 seconds and 5 git commands to generate.