This patch fixes and issue whereby applying 'Img to a constant
enumerated character type would result in a compiler crash when
assertions are enabled and infinite recursion when they are not.

Tested on x86_64-pc-linux-gnu, committed on trunk

2019-07-09  Justin Squirek  <squi...@adacore.com>

gcc/ada/

        * sem_eval.adb (Expr_Value_E): Add conditional to correctly
        handle constant enumerated character types.

gcc/testsuite/

        * gnat.dg/image1.adb: New testcase.
--- gcc/ada/sem_eval.adb
+++ gcc/ada/sem_eval.adb
@@ -4281,7 +4281,15 @@ package body Sem_Eval is
          return Ent;
       else
          pragma Assert (Ekind (Ent) = E_Constant);
-         return Expr_Value_E (Constant_Value (Ent));
+
+         --  We may be dealing with a enumerated character type constant, so
+         --  handle that case here.
+
+         if Nkind (Constant_Value (Ent)) = N_Character_Literal then
+            return Ent;
+         else
+            return Expr_Value_E (Constant_Value (Ent));
+         end if;
       end if;
    end Expr_Value_E;
 

--- /dev/null
new file mode 100644
+++ gcc/testsuite/gnat.dg/image1.adb
@@ -0,0 +1,12 @@
+--  { dg-do run }
+
+with Ada.Text_IO; use Ada.Text_IO;
+with Ada.Characters.Latin_1;
+
+procedure Image1 is
+   Str : String := Ada.Characters.Latin_1.LF'Img;
+begin
+   if Str /= "LF" then
+      raise Program_Error;
+   end if;
+end Image1;

Reply via email to