comp.lang.ada
 help / color / mirror / Atom feed
* Different aliasing rules for containers?
@ 2020-02-20 12:24 Martin B. B.
  2020-02-20 13:36 ` joakimds
  0 siblings, 1 reply; 3+ messages in thread
From: Martin B. B. @ 2020-02-20 12:24 UTC (permalink / raw)


The following code:

with Ada.Containers.Vectors;
with Ada.Text_IO;

procedure Test is

   package IO renames Ada.Text_IO;

   type Obj is record
      Id : Integer;
   end record;

   package Obj_Vectors is new Ada.Containers.Vectors
     (Index_Type   => Positive,
      Element_Type => Obj);
   
   use type Obj_Vectors.Vector;
      
   Objects : Obj_Vectors.Vector := Obj'(Id => 0) & Obj'(Id => 1) & Obj'(Id => 2);

begin
   for O of Objects loop
      O.Id := 999;
   end loop;
   
   for O of Objects loop
      IO.Put_Line (Integer'Image (O.Id));
   end loop;
end Test;

Using GNAT Community 2019 (20190517-83) produces the following output:
 999
 999
 999

When looking at the Ada.Containers.Vectors package specification, I see the following:
   type Vector is tagged private
   with
      Constant_Indexing => Constant_Reference,
      Variable_Indexing => Reference,
      Default_Iterator  => Iterate,
      Iterator_Element  => Element_Type;

However, both the `Rerefence` and `Constant_Reference` functions takes aliased parameters:
   function Reference
     (Container : aliased in out Vector;
      Position  : Cursor) return Reference_Type;

My question is then: How is the code above possible then, when my `Objects` vector is not aliased? Shouldn't the compiler complain that I haven't declared `Objects` as being aliased?

Even creating my own procedure that takes a `V : aliased Obj_Vectors.Vector` still produces no aliasing errors when passing it a non-aliased vector.

This code:
with Ada.Text_IO;

procedure Test is

   package IO renames Ada.Text_IO;
   
   procedure Takes_Aliased_Integer (X : aliased Integer);

   procedure Takes_Aliased_Integer (X : aliased Integer) is
   begin
      null;
   end Takes_Aliased_Integer;

   My_Int : Integer := 123;

begin
   Takes_Aliased_Integer (My_Int);
end Test;

However, correctly(?) generates the following compiler error: "actual for aliased formal "X" must be aliased object".

So, what exactly is going on here? Am I missing something?

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Different aliasing rules for containers?
  2020-02-20 12:24 Different aliasing rules for containers? Martin B. B.
@ 2020-02-20 13:36 ` joakimds
  2020-02-20 13:47   ` Martin B. B.
  0 siblings, 1 reply; 3+ messages in thread
From: joakimds @ 2020-02-20 13:36 UTC (permalink / raw)


Den torsdag 20 februari 2020 kl. 13:24:37 UTC+1 skrev Martin B. B.:
> The following code:
> 
> with Ada.Containers.Vectors;
> with Ada.Text_IO;
> 
> procedure Test is
> 
>    package IO renames Ada.Text_IO;
> 
>    type Obj is record
>       Id : Integer;
>    end record;
> 
>    package Obj_Vectors is new Ada.Containers.Vectors
>      (Index_Type   => Positive,
>       Element_Type => Obj);
>    
>    use type Obj_Vectors.Vector;
>       
>    Objects : Obj_Vectors.Vector := Obj'(Id => 0) & Obj'(Id => 1) & Obj'(Id => 2);
> 
> begin
>    for O of Objects loop
>       O.Id := 999;
>    end loop;
>    
>    for O of Objects loop
>       IO.Put_Line (Integer'Image (O.Id));
>    end loop;
> end Test;
> 
> Using GNAT Community 2019 (20190517-83) produces the following output:
>  999
>  999
>  999
> 
> When looking at the Ada.Containers.Vectors package specification, I see the following:
>    type Vector is tagged private
>    with
>       Constant_Indexing => Constant_Reference,
>       Variable_Indexing => Reference,
>       Default_Iterator  => Iterate,
>       Iterator_Element  => Element_Type;
> 
> However, both the `Rerefence` and `Constant_Reference` functions takes aliased parameters:
>    function Reference
>      (Container : aliased in out Vector;
>       Position  : Cursor) return Reference_Type;
> 
> My question is then: How is the code above possible then, when my `Objects` vector is not aliased? Shouldn't the compiler complain that I haven't declared `Objects` as being aliased?
> 
> Even creating my own procedure that takes a `V : aliased Obj_Vectors.Vector` still produces no aliasing errors when passing it a non-aliased vector.
> 
> This code:
> with Ada.Text_IO;
> 
> procedure Test is
> 
>    package IO renames Ada.Text_IO;
>    
>    procedure Takes_Aliased_Integer (X : aliased Integer);
> 
>    procedure Takes_Aliased_Integer (X : aliased Integer) is
>    begin
>       null;
>    end Takes_Aliased_Integer;
> 
>    My_Int : Integer := 123;
> 
> begin
>    Takes_Aliased_Integer (My_Int);
> end Test;
> 
> However, correctly(?) generates the following compiler error: "actual for aliased formal "X" must be aliased object".
> 
> So, what exactly is going on here? Am I missing something?

Hi Martin,

Could it be because Obj_Vectors.Vector is a tagged type and is therefore always passed by reference in subprogram calls and that implies it is aliased?

Best regards,
Joakim

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Different aliasing rules for containers?
  2020-02-20 13:36 ` joakimds
@ 2020-02-20 13:47   ` Martin B. B.
  0 siblings, 0 replies; 3+ messages in thread
From: Martin B. B. @ 2020-02-20 13:47 UTC (permalink / raw)


On Thursday, 20 February 2020 14:36:38 UTC+1, joak...@kth.se  wrote:
> Den torsdag 20 februari 2020 kl. 13:24:37 UTC+1 skrev Martin B. B.:
> > The following code:
> > 
> > with Ada.Containers.Vectors;
> > with Ada.Text_IO;
> > 
> > procedure Test is
> > 
> >    package IO renames Ada.Text_IO;
> > 
> >    type Obj is record
> >       Id : Integer;
> >    end record;
> > 
> >    package Obj_Vectors is new Ada.Containers.Vectors
> >      (Index_Type   => Positive,
> >       Element_Type => Obj);
> >    
> >    use type Obj_Vectors.Vector;
> >       
> >    Objects : Obj_Vectors.Vector := Obj'(Id => 0) & Obj'(Id => 1) & Obj'(Id => 2);
> > 
> > begin
> >    for O of Objects loop
> >       O.Id := 999;
> >    end loop;
> >    
> >    for O of Objects loop
> >       IO.Put_Line (Integer'Image (O.Id));
> >    end loop;
> > end Test;
> > 
> > Using GNAT Community 2019 (20190517-83) produces the following output:
> >  999
> >  999
> >  999
> > 
> > When looking at the Ada.Containers.Vectors package specification, I see the following:
> >    type Vector is tagged private
> >    with
> >       Constant_Indexing => Constant_Reference,
> >       Variable_Indexing => Reference,
> >       Default_Iterator  => Iterate,
> >       Iterator_Element  => Element_Type;
> > 
> > However, both the `Rerefence` and `Constant_Reference` functions takes aliased parameters:
> >    function Reference
> >      (Container : aliased in out Vector;
> >       Position  : Cursor) return Reference_Type;
> > 
> > My question is then: How is the code above possible then, when my `Objects` vector is not aliased? Shouldn't the compiler complain that I haven't declared `Objects` as being aliased?
> > 
> > Even creating my own procedure that takes a `V : aliased Obj_Vectors.Vector` still produces no aliasing errors when passing it a non-aliased vector.
> > 
> > This code:
> > with Ada.Text_IO;
> > 
> > procedure Test is
> > 
> >    package IO renames Ada.Text_IO;
> >    
> >    procedure Takes_Aliased_Integer (X : aliased Integer);
> > 
> >    procedure Takes_Aliased_Integer (X : aliased Integer) is
> >    begin
> >       null;
> >    end Takes_Aliased_Integer;
> > 
> >    My_Int : Integer := 123;
> > 
> > begin
> >    Takes_Aliased_Integer (My_Int);
> > end Test;
> > 
> > However, correctly(?) generates the following compiler error: "actual for aliased formal "X" must be aliased object".
> > 
> > So, what exactly is going on here? Am I missing something?
> 
> Hi Martin,
> 
> Could it be because Obj_Vectors.Vector is a tagged type and is therefore always passed by reference in subprogram calls and that implies it is aliased?
> 
> Best regards,
> Joakim

Ah, it seems you are correct! I was not aware of this difference between tagged and non-tagged types.

It seems to be what is mentioned here in the reference manual: "Finally, a formal parameter or generic formal object of a tagged type is defined to be aliased" (http://www.ada-auth.org/standards/rm12_w_tc1/html/RM-3-10.html#p9)

Thank you for the help :)

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-02-20 13:47 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-20 12:24 Different aliasing rules for containers? Martin B. B.
2020-02-20 13:36 ` joakimds
2020-02-20 13:47   ` Martin B. B.

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox