This prevents the compiler from giving a bogus error on a discriminated
record type derived from an untagged discriminated record type with a
variant part, when the subtype of the discriminant of the former is more
constrained than that of the latter and the variant part contains a
discrete choice violating the constraints of the subtype.
Tested on x86_64-pc-linux-gnu, committed on trunk
2020-06-02 Eric Botcazou <ebotca...@adacore.com>
gcc/ada/
* sem_ch3.adb (Replace_Discriminants): Preserve the Etype of the
Name of N_Variant_Part nodes when rewriting it.
--- gcc/ada/sem_ch3.adb
+++ gcc/ada/sem_ch3.adb
@@ -22334,7 +22334,18 @@ package body Sem_Ch3 is
if Original_Record_Component (Comp) = Entity (Name (N))
or else Chars (Comp) = Chars (Name (N))
then
- Set_Name (N, New_Occurrence_Of (Comp, Sloc (N)));
+ -- Make sure to preserve the type coming from the parent on
+ -- the Name, even if the subtype of the discriminant can be
+ -- constrained, so that discrete choices inherited from the
+ -- parent in the variant part are not flagged as violating
+ -- the constraints of the subtype.
+
+ declare
+ Typ : constant Entity_Id := Etype (Name (N));
+ begin
+ Rewrite (Name (N), New_Occurrence_Of (Comp, Sloc (N)));
+ Set_Etype (Name (N), Typ);
+ end;
exit;
end if;