[Ada] Actual for a formal derived non-limited type

Submitted by Arnaud Charlet on Oct. 7, 2010, 1:22 p.m.

Details

Message ID 20101007132220.GA14160@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Oct. 7, 2010, 1:22 p.m.
If a formal derived type is non-limited, an actual for it cannot be limited.
This rule is needed to prevent anomalies with limited interfaces, because an
extension of a limited interface is not limited if the appropriate keyword
does not appear in the declaration. Without this rule, it would be possible
to constructs assignments for limited types, as shown below.
In Ada2012, the following program must be rejected:

   gcc -c -gnat12 derived.adb

derived.adb:10:45: actual for non-limited "T" cannot be a limited type
derived.adb:10:45: instantiation abandoned
derived.adb:21:04: "Store" is undefined

---
procedure Derived is
    type Ifc is limited interface;
    generic
       type T is abstract new Ifc with private; -- T is nonlimited: 7.5(6.1/2)
    procedure Classwide_Store (Target : out T'Class; Source : T'Class);
    procedure Classwide_Store (Target : out T'Class; Source : T'Class) is
    begin
        Target := Source;
    end Classwide_Store;
    procedure Store is new Classwide_Store (Ifc); -- legal? (No.)
    task type Tsk;
    task body Tsk is
    begin
        null;
    end Tsk;
    type Has_Task is limited new Ifc with record
            F : Tsk;
    end record;
    X, Y : Has_Task;
begin
   Store (X, Y);
end;

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

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

	* sem_ch12.adb (Validate_Derived_Type_Instance): If a formal derived
	type is non-limited, an actual for it cannot be limited.

Patch hide | download patch | download mbox

Index: sem_ch12.adb
===================================================================
--- sem_ch12.adb	(revision 165103)
+++ sem_ch12.adb	(working copy)
@@ -9969,12 +9969,13 @@ 
          --  interface then  the generic formal is not unless declared
          --  explicitly so. If not declared limited, the actual cannot be
          --  limited (see AI05-0087).
-         --  Disable check for now, limited interfaces implemented by
-         --  protected types are common, Need to update tests ???
+         --  Even though this AI is a binding interpretation, we enable the
+         --  check only in Ada2012 mode, because this improper construct
+         --  shows up in user code and in existing B-tests.
 
          if Is_Limited_Type (Act_T)
            and then not Is_Limited_Type (A_Gen_T)
-           and then False
+           and then Ada_Version >= Ada_12
          then
             Error_Msg_NE
               ("actual for non-limited & cannot be a limited type", Actual,