diff mbox series

[Ada] Crash in expression function defined in protected object

Message ID 20201019095422.GA91050@adacore.com
State New
Headers show
Series [Ada] Crash in expression function defined in protected object | expand

Commit Message

Pierre-Marie de Rodat Oct. 19, 2020, 9:54 a.m. UTC
The frontend crashes processing an expression function defined
in the body of protected type that depends on discriminants.

Tested on x86_64-pc-linux-gnu, committed on trunk

gcc/ada/

	* sem_ch8.adb (Find_Direct_Name): Do not trust in the decoration
	of the Entity attribute in constants associated with
	discriminals of protected types.
diff mbox series

Patch

diff --git a/gcc/ada/sem_ch8.adb b/gcc/ada/sem_ch8.adb
--- a/gcc/ada/sem_ch8.adb
+++ b/gcc/ada/sem_ch8.adb
@@ -5627,6 +5627,21 @@  package body Sem_Ch8 is
          if Is_Type (Entity (N)) then
             Set_Etype (N, Entity (N));
 
+         --  The exception to this general rule are constants associated with
+         --  discriminals of protected types because for each protected op
+         --  a new set of discriminals is internally created by the frontend
+         --  (see Exp_Ch9.Set_Discriminals), and the current decoration of the
+         --  entity pointer may have been set as part of a preanalysis, where
+         --  discriminals still reference the first subprogram or entry to be
+         --  expanded (see Expand_Protected_Body_Declarations).
+
+         elsif Full_Analysis
+           and then Ekind (Entity (N)) = E_Constant
+           and then Present (Discriminal_Link (Entity (N)))
+           and then Is_Protected_Type (Scope (Discriminal_Link (Entity (N))))
+         then
+            goto Find_Name;
+
          else
             declare
                Entyp : constant Entity_Id := Etype (Entity (N));
@@ -5667,6 +5682,8 @@  package body Sem_Ch8 is
          return;
       end if;
 
+      <<Find_Name>>
+
       --  Preserve relevant elaboration-related attributes of the context which
       --  are no longer available or very expensive to recompute once analysis,
       --  resolution, and expansion are over.