[gcc r16-9486] Ada: Fix crash on Unchecked_Union component with -gnateV

Eric Botcazou via Gcc-cvs <[email protected]> Sun, 2 Aug 2026 12:13:34 +0000 (GMT)
Newsgroups gmane.comp.gcc.cvs
Message-ID <[email protected]>
https://gcc.gnu.org/g:84e762dc5cdb266ee52cc0ce444c8aa9c458bbee

commit r16-9486-g84e762dc5cdb266ee52cc0ce444c8aa9c458bbee
Author: Eric Botcazou <[email protected]>
Date:   Sat Aug 1 18:44:12 2026 +0200

    Ada: Fix crash on Unchecked_Union component with -gnateV
    
    This is the same bug as PR ada/123857, but at a nested level, and the fix
    is the same 1-liner but at a different place.
    
    gcc/ada/
            PR ada/126553
            * exp_attr.adb (Build_Record_VS_Func.Validate_Field): Do nothing
            if the field is of an Unchecked_Union type.
    
    gcc/testsuite/
            * gnat.dg/unchecked_union5.adb: New test.

Diff:
---
 gcc/ada/exp_attr.adb                       |  7 +++++--
 gcc/testsuite/gnat.dg/unchecked_union5.adb | 23 +++++++++++++++++++++++
 2 files changed, 28 insertions(+), 2 deletions(-)

diff --git a/gcc/ada/exp_attr.adb b/gcc/ada/exp_attr.adb
index 8e72a9030732..04c6700a4538 100644
--- a/gcc/ada/exp_attr.adb
+++ b/gcc/ada/exp_attr.adb
@@ -781,9 +781,12 @@ package body Exp_Attr is
          if Field_Nam in Name_uObject | Name_uParent | Name_uTag then
             null;
 
-         --  Do not process fields without any scalar components
+         --  Do not process fields without any scalar components, or whose type
+         --  is an unchecked union since we cannot know where they are.
 
-         elsif not Scalar_Part_Present (Field_Typ) then
+         elsif not Scalar_Part_Present (Field_Typ)
+           or else Is_Unchecked_Union (Field_Typ)
+         then
             null;
 
          --  Otherwise the field needs to be validated. Use Make_Identifier
diff --git a/gcc/testsuite/gnat.dg/unchecked_union5.adb b/gcc/testsuite/gnat.dg/unchecked_union5.adb
new file mode 100644
index 000000000000..746ed19e03c6
--- /dev/null
+++ b/gcc/testsuite/gnat.dg/unchecked_union5.adb
@@ -0,0 +1,23 @@
+--  { dg-do compile }
+--  { dg-options "-gnateV" }
+
+procedure Unchecked_Union5 is
+
+   type R (Bytes_Mode : Boolean := False) is record
+      case Bytes_Mode is
+         when True =>
+            A : Boolean;
+         when False =>
+            B : Boolean;
+      end case;
+   end record with Unchecked_Union;
+
+   type T is record
+      Data : R;
+   end record;
+
+   function F (Message : T) return Integer is (0);
+
+begin
+   null;
+end;