comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: Assignment access type with discriminants
Date: Sat, 25 Mar 2023 03:51:07 -0500	[thread overview]
Message-ID: <tvmcls$24506$1@dont-email.me> (raw)
In-Reply-To: tvjr7l$1js79$1@dont-email.me

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #1: Type: text/plain, Size: 2961 bytes --]

The rule is question is 4.1(9/3):

 If the type of the name in a dereference is some access-to-object type T, 
then the dereference denotes a view of an object, the nominal subtype of the 
view being the designated subtype of T. If the designated subtype has 
unconstrained discriminants, the (actual) subtype of the view is constrained 
by the values of the discriminants of the designated object, except when 
there is a partial view of the type of the designated subtype that does not 
have discriminants, in which case the dereference is not constrained by its 
discriminant values.

We have to do that so as otherwise the access value would have to carry a 
designation as to whether the object was allocated or not.

-------

This rule was inherited from Ada 83.

IMHO, this rule is stupid. It's even more stupid with the hole for types 
that have partial views without discriminants. The *proper* solution is to 
get rid of the rarely used and mostly useless access constraints, and then 
have no extra restrictions on access values. But that's considered too 
incompatible.

                Randy.


"J-P. Rosen" <rosen@adalog.fr> wrote in message 
news:tvjr7l$1js79$1@dont-email.me...
> Le 23/03/2023 à 19:55, Niklas Holsti a écrit :
> >
> >> Here is a slightly modified version of your example:
> >>
> >> procedure Test is
> >>     type F is (F1, F2, F3);
> >>
> >>     type Foo (K : F := F1) is record
> >>        case K is
> >>           when F1 =>
> >>              X1 : Integer;
> >>           when F2 =>
> >>              X2 : Float;
> >>           when F3 =>
> >>              X3 : String (1..2);
> >>        end case;
> >>     end record;
> >>     type Foo_Ptr is access all Foo;
> >>     type Foo_Ptr2 is access Foo;
> >>     X : aliased Foo;
> >>     P : Foo_Ptr := X'Access;
> >>     PF2: Foo_PTR2 (F2);
> >> begin
> >>     X := (F2, 1.0);   -- OK
> >>     PF2 := new Foo (F2);
> >>     P := PF2.all'Access;
> >>     P.all := (F1, 3); -- Error!
> >> end Test;
> >>
> >> Without this rule, PF2.all would now designate a value whose
> >> discriminant is F1!
> >
> >
> > This error is understandable and valid, because now P.all is PF2.all
> > which is an allocated object and therefore constrained by its initial
> > value with K = F2.
> >
> > But why should the same apply when P designates X, which is
> > unconstrained? Is it just an optimization (in the RM) so that a
> > general access value does not have to carry around a flag showing
> > whether its designated object is constrained or unconstrained?
>
> I didn't dig in the RM in all details, but I think this comes from the 
> fact that being constrained (always) is a property of the pointer (more 
> precisely, its subtype), not of the pointed-at object.
>
> -- 
> J-P. Rosen
> Adalog
> 2 rue du Docteur Lombard, 92441 Issy-les-Moulineaux CEDEX
> https://www.adalog.fr https://www.adacontrol.fr
> 


      reply	other threads:[~2023-03-25  8:51 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-03-22  9:19 Assignment access type with discriminants Dmitry A. Kazakov
2023-03-22  9:31 ` Björn Lundin
2023-03-22 14:10 ` G.B.
2023-03-23 11:51   ` Dmitry A. Kazakov
2023-03-23 16:53     ` AdaMagica
2023-03-23 18:09       ` Niklas Holsti
2023-03-23 17:04 ` J-P. Rosen
2023-03-23 18:55   ` Niklas Holsti
2023-03-23 19:53     ` Dmitry A. Kazakov
2023-03-24  9:41     ` J-P. Rosen
2023-03-25  8:51       ` Randy Brukardt [this message]
replies disabled

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