https://gcc.gnu.org/bugzilla/show_bug.cgi?id=127466
Bug ID: 127466
Summary: [ada] GNAT rejects legal Unchecked_Union derived-type
discriminant access with "sorry - unable to generate
discriminant check" (regression from GNAT 12)
Product: gcc
Version: 16.1.0
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: ada
Assignee: unassigned at gcc dot gnu.org
Reporter: a.yerkesh at gmail dot com
CC: dkm at gcc dot gnu.org
Target Milestone: ---
Created attachment 65624
--> https://gcc.gnu.org/bugzilla/attachment.cgi?id=65624&action=edit
Try to compile to reproduce bug
GNAT 16.1.0 fails to compile the code below with following error:
repro.adb:28:06: error: sorry - unable to generate discriminant check for
reference to variant component "Field_B"
GNAT 12.4.0 compiles the same code without issues.
==========CODE==========
procedure Repro is
type Mode is (A, B);
-- A variant record with two shapes, selected by Disc.
type Reg (Disc : Mode := A) is record
case Disc is
when A => Field_A : Integer;
when B => Field_B : Integer;
end case;
end record
with Unchecked_Union;
-- Derive a new type with Disc permanently fixed to B, right in the
-- type declaration. Every object of Derived_Reg is guaranteed to be
-- the B shape.
-- By the Ada standard (RM B.3.3), this makes the discriminant
-- "inferable": the compiler can tell which shape applies just from
-- the type itself, without runtime check.
type Derived_Reg is new Reg (Disc => B);
-- subtype Derived_Reg is Reg (Disc => B);
X : Derived_Reg;
begin
-- This access should compile with no check at all, since X's type
-- already guarantees Disc = B. Instead, GNAT 16.1.0 rejects it.
X.Field_B := 1;
end Repro;
========END_CODE========
-----Steps to reproduce-----
1. Save the code above as repro.adb (or download the attachment)
2. gcc -c -gnat2022 repro.adb
3. Observe compilation fail with "repro.adb:28:06: error: sorry - unable..."
4. (Optional) Compile with GNAT 12 (gcc-12 -c -gnat2022 repro.adb) - observe
clean compilation
5. (Optional) Uncomment line 21 and comment line 20 - observe subtype compile
successfully
----------------------------
Tested on Ubuntu 24.04
Link to Ada reference manual:
https://www.adaic.org/resources/add_content/standards/05rm/html/RM-B-3-3.html