]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
ada: Adjust 'Constrained for formal parameters of unchecked union types
authorEric Botcazou <ebotcazou@adacore.com>
Mon, 23 Feb 2026 16:29:44 +0000 (17:29 +0100)
committerMarc Poulhiès <dkm@gcc.gnu.org>
Thu, 28 May 2026 08:52:47 +0000 (10:52 +0200)
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.

gcc/ada/exp_attr.adb

index 64a4559dff71b1703fe497c4ff47608b548ae8bb..403b5899ce5e663c2ece4203bbb53356c2307601 100644 (file)
@@ -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.