diff mbox series

[Ada] Fix PR ada/101970

Message ID 22354165.6Emhk5qWAg@fomalhaut
State Committed
Headers show
Series [Ada] Fix PR ada/101970 | expand

Commit Message

Eric Botcazou Sept. 14, 2021, 9:36 a.m. UTC
This is a regression present on the mainline and 11 branch in the form of an 
ICE for an enumeration type with a full signed representation for its size.

Tested on x86-64/Linux, applied on the mainline and 11 branch.


2021-09-14  Eric Botcazou  <ebotcazou@adacore.com>

	PR ada/101970
	* exp_attr.adb (Expand_N_Attribute_Reference) <Attribute_Enum_Rep>:
	Use an unchecked conversion instead of a regular conversion in the
	enumeration case and remove Conversion_OK flag in the integer case.
	<Attribute_Pos>: Remove superfluous test.


2021-09-14  Eric Botcazou  <ebotcazou@adacore.com>
	
	* gnat.dg/enum_rep2.adb: New test.
diff mbox series

Patch

diff --git a/gcc/ada/exp_attr.adb b/gcc/ada/exp_attr.adb
index f074521e5f7..fc6b0ef8097 100644
--- a/gcc/ada/exp_attr.adb
+++ b/gcc/ada/exp_attr.adb
@@ -3252,14 +3252,15 @@  package body Exp_Attr is
          --  If not constant-folded, Enum_Type'Enum_Rep (X) or X'Enum_Rep
          --  expands to
 
-         --    target-type (X)
+         --    target-type!(X)
 
-         --  This is simply a direct conversion from the enumeration type to
-         --  the target integer type, which is treated by the back end as a
-         --  normal integer conversion, treating the enumeration type as an
-         --  integer, which is exactly what we want. We set Conversion_OK to
-         --  make sure that the analyzer does not complain about what otherwise
-         --  might be an illegal conversion.
+         --  This is an unchecked conversion from the enumeration type to the
+         --  target integer type, which is treated by the back end as a normal
+         --  integer conversion, treating the enumeration type as an integer,
+         --  which is exactly what we want. Unlike for the Pos attribute, we
+         --  cannot use a regular conversion since the associated check would
+         --  involve comparing the converted bounds, i.e. would involve the use
+         --  of 'Pos instead 'Enum_Rep for these bounds.
 
          --  However the target type is universal integer in most cases, which
          --  is a very large type, so in the case of an enumeration type, we
@@ -3267,11 +3268,13 @@  package body Exp_Attr is
          --  the size information.
 
          if Is_Enumeration_Type (Ptyp) then
-            Rewrite (N, OK_Convert_To (Get_Integer_Type (Ptyp), Expr));
+            Rewrite (N, Unchecked_Convert_To (Get_Integer_Type (Ptyp), Expr));
             Convert_To_And_Rewrite (Typ, N);
 
+         --  Deal with integer types (replace by conversion)
+
          else
-            Rewrite (N, OK_Convert_To (Typ, Expr));
+            Rewrite (N, Convert_To (Typ, Expr));
          end if;
 
          Analyze_And_Resolve (N, Typ);
@@ -5420,7 +5423,7 @@  package body Exp_Attr is
 
          --  Deal with integer types (replace by conversion)
 
-         elsif Is_Integer_Type (Etyp) then
+         else
             Rewrite (N, Convert_To (Typ, Expr));
          end if;