diff mbox

[Ada] Legality of aliased keyword in extended return statement

Message ID 20121205104744.GA10436@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Dec. 5, 2012, 10:47 a.m. UTC
An early version of the Ada 2012 RM made it illegal to declare the return object
aliased in an extended return statement. A later decision (AI05-0277) made it
legal in the confirming case where the return type is immutably limited, This
patch accordingly moves the application of this rule from the syntax to the
semantics.

compiling test_aliased.adb below in Ada 2005 mode must yield:

    test_aliased.adb:8:08: warning: aliased only allowed for limited return objects in Ada 2012
    test_aliased.adb:15:06: warning: aliased only allowed for limited return objects in Ada 2012

while compiling it in Ada 2012 mode must yield:

    test_aliased.adb:8:08: aliased only allowed for limited return objects

--
procedure  Test_Aliased is
   type Lim is limited record
      Value : Integer;
   end record;

   function Wrong return Natural is
   begin
       return Result : Aliased Natural do
        Result := 15;
       End return;
  end Wrong;

  function OK return Lim is
  begin
     return Result : aliased Lim do
        Result.Value := 123;
     end return;
  end OK;
begin
   null;
end Test_Aliased;

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

2012-12-05  Ed Schonberg  <schonberg@adacore.com>

	* par-ch6.adb (P_Return_Object_Declaration): Do not check for
	legality of Aliased keyword.
	* sem_ch6.adb (Analyze_Function_Return): The keyword aliased is
	legal in an extended return statement only if the return type
	is immutably limited.
diff mbox

Patch

Index: par-ch6.adb
===================================================================
--- par-ch6.adb	(revision 194188)
+++ par-ch6.adb	(working copy)
@@ -1721,13 +1721,8 @@ 
          Scan; -- past ALIASED
          Set_Aliased_Present (Decl_Node);
 
-         if Ada_Version < Ada_2012 then
-            Error_Msg_SC -- CODEFIX
-              ("ALIASED not allowed in extended return in Ada 2012?");
-         else
-            Error_Msg_SC -- CODEFIX
-              ("ALIASED not allowed in extended return");
-         end if;
+         --  The restrictions on the use of aliased in an extended return
+         --  are semantic, not syntactic.
 
          if Token = Tok_Constant then
             Scan; -- past CONSTANT
Index: sem_ch6.adb
===================================================================
--- sem_ch6.adb	(revision 194199)
+++ sem_ch6.adb	(working copy)
@@ -787,6 +787,7 @@ 
 
             Analyze_And_Resolve (Expr, R_Type);
             Check_Limited_Return (Expr);
+
          end if;
 
          --  RETURN only allowed in SPARK as the last statement in function
@@ -806,8 +807,9 @@ 
          --  Analyze parts specific to extended_return_statement:
 
          declare
-            Obj_Decl : constant Node_Id :=
+            Obj_Decl    : constant Node_Id :=
                          Last (Return_Object_Declarations (N));
+            Has_Aliased : constant Boolean := Aliased_Present (Obj_Decl);
 
             HSS : constant Node_Id := Handled_Statement_Sequence (N);
 
@@ -842,6 +844,19 @@ 
             Set_Referenced (Defining_Identifier (Obj_Decl));
 
             Check_References (Stm_Entity);
+
+            --  Check RM 6.5 (5.9/3)
+
+            if Has_Aliased then
+               if Ada_Version < Ada_2012 then
+                  Error_Msg_N ("aliased only allowed for limited"
+                     & " return objects in Ada 2012?", N);
+
+               elsif not Is_Immutably_Limited_Type (R_Type) then
+                  Error_Msg_N ("aliased only allowed for limited"
+                     & " return objects", N);
+               end if;
+            end if;
          end;
       end if;