From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 X-Received: by 2002:a05:620a:140d:: with SMTP id d13mr28835105qkj.450.1582205797441; Thu, 20 Feb 2020 05:36:37 -0800 (PST) X-Received: by 2002:aca:5a04:: with SMTP id o4mr1995591oib.113.1582205797058; Thu, 20 Feb 2020 05:36:37 -0800 (PST) Path: eternal-september.org!reader01.eternal-september.org!feeder.eternal-september.org!news.gegeweb.eu!gegeweb.org!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail Newsgroups: comp.lang.ada Date: Thu, 20 Feb 2020 05:36:36 -0800 (PST) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: google-groups.googlegroups.com; posting-host=136.163.208.2; posting-account=HFCrOQoAAABZD_f-UUbYHm3lJDIrh-UX NNTP-Posting-Host: 136.163.208.2 References: User-Agent: G2/1.0 MIME-Version: 1.0 Message-ID: <3c675051-911e-44b2-9eba-c4712892e6dd@googlegroups.com> Subject: Re: Different aliasing rules for containers? From: joakimds@kth.se Injection-Date: Thu, 20 Feb 2020 13:36:37 +0000 Content-Type: text/plain; charset="UTF-8" Xref: reader01.eternal-september.org comp.lang.ada:58095 Date: 2020-02-20T05:36:36-08:00 List-Id: 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