From: Eric Botcazou <[email protected]>

GNAT has historically never added extra formal parameters alongside formal
parameters of unchecked union types, even when they have convention Ada,
so it cannot compute the 'Constrained attribute for In Out or Out formal
parameters. This changes the compiler to raise Program_Error in this case.

gcc/ada/ChangeLog:

        * exp_attr.adb (Expand_N_Attribute_Reference) <Constrained>: If the
        prefix is a non-In formal parameter of an unchecked union type, give
        a warning and insert a raise statement for Program_Error.

Tested on x86_64-pc-linux-gnu, committed on master.

---
 gcc/ada/exp_attr.adb | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/gcc/ada/exp_attr.adb b/gcc/ada/exp_attr.adb
index 64a4559dff7..403b5899ce5 100644
--- a/gcc/ada/exp_attr.adb
+++ b/gcc/ada/exp_attr.adb
@@ -3554,6 +3554,25 @@ package body Exp_Attr is
               New_Occurrence_Of
                 (Extra_Constrained (Formal_Ent), Loc));
 
+         --  Extra formals are not created for Unchecked_Union parameters
+
+         elsif Present (Formal_Ent)
+           and then Ekind (Formal_Ent) /= E_In_Parameter
+           and then Is_Unchecked_Union (Base_Type (Etype (Formal_Ent)))
+         then
+            if Comes_From_Source (N) then
+               Error_Msg_N
+                 ("constraints cannot be determined for Unchecked_Union??", N);
+               Error_Msg_N
+                 ("\Program_Error will be raised for ''Constrained??", N);
+            end if;
+
+            Insert_Action (N,
+              Make_Raise_Program_Error (Loc,
+                Reason => PE_Unchecked_Union_Restriction));
+
+            Rewrite (N, New_Occurrence_Of (Boolean_Literals (False), Loc));
+
          --  If the prefix is an access to object, the attribute applies to
          --  the designated object, so rewrite with an explicit dereference.
 
-- 
2.53.0

Reply via email to