Patchwork [Ada] Extended return statements require static compatibility

login
register
mail settings
Submitter Arnaud Charlet
Date Oct. 8, 2010, 10:45 a.m.
Message ID <20101008104523.GA23091@adacore.com>
Download mbox | patch
Permalink /patch/67164/
State New
Headers show

Comments

Arnaud Charlet - Oct. 8, 2010, 10:45 a.m.
AI05-0103 requires that for elementary types and for constrained types, the
subtype indication in an extended return statement and the return subtype of
the enclosing function must match statically. This patch completes the
implementation of this rule for the case of named access types.

Compiling proc.adb must be rejected with the following messages:

   proc.adb:4:17: subtype must statically match function result subtype
   proc.adb:13:17: subtype must statically match function result subtype
   proc.adb:22:21: subtype must statically match function result subtype

---
procedure Proc is
   function F1 return Integer is
   begin
     return X : Natural do              -- ERROR
        X := 17;
     end return;
   end F1;

   type Acc_String is access String;

   function F2 return Acc_String is
   begin
     return X : Acc_String(1..10) do    -- ERROR
       X := new String'("0123456789");
     end return;
   end F2;

   type An_Access is access String;

   function Nice return not null An_Access is
   begin
       return Obj : An_Access do      --  ERROR
           Obj := null;
       end return;
   end Nice;

begin
   if F1 /= 17 then raise Program_Error; end if;
end;

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

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

	* sem_ch6.adb (Check_Return_Subtype): The subtype indication in an
	extended return must match statically the return subtype of the
	enclosing function if the type is an elementary type or if it is
	constrained.

Patch

Index: sem_ch6.adb
===================================================================
--- sem_ch6.adb	(revision 165153)
+++ sem_ch6.adb	(working copy)
@@ -620,7 +620,12 @@  package body Sem_Ch6 is
                   Subtype_Ind);
             end if;
 
-            if Is_Constrained (R_Type) then
+            --  AI05-103 : for elementary types, subtypes must statically
+            --  match.
+
+            if Is_Constrained (R_Type)
+              or else Is_Access_Type (R_Type)
+            then
                if not Subtypes_Statically_Match (R_Stm_Type, R_Type) then
                   Error_Msg_N
                     ("subtype must statically match function result subtype",