diff mbox series

[Ada] Crash on illegal current instance

Message ID 20170918091158.GA36995@adacore.com
State New
Headers show
Series [Ada] Crash on illegal current instance | expand

Commit Message

Pierre-Marie de Rodat Sept. 18, 2017, 9:11 a.m. UTC
If the type_mark of a qualified_expression refers to the current
instance of the type, do not crash; instead give a proper error
message. This is illegal by RM-8.6(17).

The following test should get an error:

current_instance_default.ads:2:54: current instance not allowed

package Current_Instance_Default is
   type Color is (Red, Orange) with Default_Value => Color'(Red); -- ERROR:
end Current_Instance_Default;

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

2017-09-18  Bob Duff  <duff@adacore.com>

	* sem_ch4.adb (Analyze_Qualified_Expression): Give an error if the type
	mark refers to the current instance. Set the type to Any_Type in that
	case, to avoid later crashes.
diff mbox series

Patch

Index: sem_ch4.adb
===================================================================
--- sem_ch4.adb	(revision 252907)
+++ sem_ch4.adb	(working copy)
@@ -3930,6 +3930,23 @@ 
       Set_Etype (N, Any_Type);
       Find_Type (Mark);
       T := Entity (Mark);
+
+      if Nkind_In
+        (Enclosing_Declaration (N),
+         N_Formal_Type_Declaration,
+         N_Full_Type_Declaration,
+         N_Incomplete_Type_Declaration,
+         N_Protected_Type_Declaration,
+         N_Private_Extension_Declaration,
+         N_Private_Type_Declaration,
+         N_Subtype_Declaration,
+         N_Task_Type_Declaration)
+        and then T = Defining_Identifier (Enclosing_Declaration (N))
+      then
+         Error_Msg_N ("current instance not allowed", Mark);
+         T := Any_Type;
+      end if;
+
       Set_Etype (N, T);
 
       if T = Any_Type then