[Ada] Null exclusion not allowed in uninitialzed allocators

Message ID 20101007135956.GA15456@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Oct. 7, 2010, 1:59 p.m.
If Some_Ptr is an access type, the following allocator:

      Ptr := new Some_Ptr;

creates a pointer to a null value. Therefore, the construct:

      Ptr := new not null Some_Ptr;

is known to raise Constraint_Error.  This useless construct has been declared
illegal in Ada2012.

Compiling the following program must yield:

alloc.adb:7:13: an uninitialized allocator cannot have a null exclusion

procedure Alloc is
   type T is tagged null record;
   type Ptr is access all T;
   type Link is access all Ptr;
   Thing : Link;
   Thing := new not null Ptr;   -- ERROR

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

2010-10-07  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch4.adb (Analyze_Allocator): In Ada 2012, a null_exclusion
	indicator is illegal for an uninitialized allocator.


Index: sem_ch4.adb
--- sem_ch4.adb	(revision 165080)
+++ sem_ch4.adb	(working copy)
@@ -507,13 +507,21 @@ 
             --  be a null object, and we can insert an unconditional raise
             --  before the allocator.
+            --  Ada2012 (AI-104) : a not null indication here is altogether
+            --  illegal.
             if Can_Never_Be_Null (Type_Id) then
                   Not_Null_Check : constant Node_Id :=
                                      Make_Raise_Constraint_Error (Sloc (E),
                                        Reason => CE_Null_Not_Allowed);
-                  if Expander_Active then
+                  if Ada_Version >= Ada_12 then
+                     Error_Msg_N
+                       ("an uninitialized allocator cannot have"
+                         & " a null exclusion", N);
+                  elsif Expander_Active then
                      Insert_Action (N, Not_Null_Check);
                      Analyze (Not_Null_Check);