ada

Error message: Object in prefixed call to "..." must be aliased


I am currently reading this document: Ada for the C++ or Java Developer. Chapter 8 (page 33) contains an introduction to object oriented programming in Ada. The chapter starts with the following example:

type T is tagged record
   V, W : Integer;
end record;

type T_Access is access all T;

function F (V : T) return Integer;

procedure P1 (V : access T);

procedure P2 (V : T_Access);

The following page describes an example on how to call the subprogram P1:

declare
   V : T;
begin
   V.P1;
end;

This results into the following error: object in prefixed call to "P1" must be aliased (RM 4.1.3 (13 1/2)). If I replace procedure P1 (V : access T); with procedure P1 (V : in out T); the example is successfully compiled. Is this a typo in the document?


Solution

  • ARM 4.1.3(13.1) was introduced in the 2005 revision in AI95-00252 and AI95-00407 (I get the impression, as part of a cleanup).

    I’d say that the document is wrong. Perhaps this part of the material was developed before Ada 2005.


    It is true that, under some circumstances, a tagged object is automatically aliased: ARM 3.10(9) says

    [...] a formal parameter or generic formal object of a tagged type is defined to be aliased.

    So, this is legal:

    declare
       procedure Proc (Param : in out T) is
       begin
          Param.P1;
       end Proc;
       V : T;
    begin
       Proc (V);
    end;