Comments
Patch
===================================================================
@@ -7594,6 +7594,18 @@
-- unless the context of an assignment can provide size information.
-- Don't we have a general routine that does this???
+ function Is_Subtype_Declaration return Boolean;
+ -- The replacement of a discriminant reference by its value is required
+ -- if this is part of the initialization of an temporary generated by
+ -- a change of representation. This shows up as the construction of a
+ -- discriminant constraint for a subtype declared at the same point as
+ -- the entity in the prefix of the selected component.
+ -- We recognize this case when the context of the reference is:
+ --
+ -- subtype ST is T(Obj.D);
+ --
+ -- The entity for Obj comes from source, and ST has the same sloc.
+
-----------------------
-- In_Left_Hand_Side --
-----------------------
@@ -7607,6 +7619,21 @@
and then In_Left_Hand_Side (Parent (Comp)));
end In_Left_Hand_Side;
+ -----------------------------
+ -- Is_Subtype_Declaration --
+ -----------------------------
+
+ function Is_Subtype_Declaration return Boolean is
+ Par : constant Node_Id := Parent (N);
+
+ begin
+ return
+ Nkind (Par) = N_Index_Or_Discriminant_Constraint
+ and then Nkind (Parent (Parent (Par))) = N_Subtype_Declaration
+ and then Comes_From_Source (Entity (Prefix (N)))
+ and then Sloc (Par) = Sloc (Entity (Prefix (N)));
+ end Is_Subtype_Declaration;
+
-- Start of processing for Expand_N_Selected_Component
begin
@@ -7730,9 +7757,19 @@
-- AND THEN was copied, causing problems for coverage
-- analysis tools).
+ -- However, if the reference is part of the initialization
+ -- code generated for an object declaration, we must use
+ -- the discriminant value from the subtype constraint,
+ -- because the selected component may be a reference to the
+ -- object being initialized, whose discriminant is not yet
+ -- set. This only happens in complex cases involving changes
+ -- or representation.
+
if Disc = Entity (Selector_Name (N))
and then (Is_Entity_Name (Dval)
- or else Is_Static_Expression (Dval))
+ or else Nkind (Dval) = N_Integer_Literal
+ or else Is_Subtype_Declaration
+ or else Is_Static_Expression (Dval))
then
-- Here we have the matching discriminant. Check for
-- the case of a discriminant of a component that is