This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Ada] Crash on unconstrained unchecked union declaration


When an object declaration as an indefinite type, the actual subtype of the
object is constructed from the expression itself. If the type is an unchecked
union such a subtype cannot be constructed because discriminants cannot be
retrieved from the expression. In this case, rewrite declaration as a renaming
declaration, where the back-end does not require a definite subtype.

Compiling and executing foo.adb must yield::


   TRUE
   FALSE
   TRUE
   'A'
    11
    100

---
with Text_IO; use Text_IO;
procedure Foo is
   type Rec_Type (I : Integer ) is record
      B : Boolean;
      case I is
         when 0 =>
            null;
         when 1 .. 10 =>
            C : Character;
         when others =>
            N : Natural;
      end case;
   end record;
   pragma Unchecked_Union (Rec_Type);

   function Get (I : Integer) return Rec_Type is
   begin
     case I is
        when 0       =>  return (I => 0, B => True);
        when 1 .. 10 =>  return (I => 1, B => False, C => 'A');
        when others  =>  return (I => 11, B => True, N => abs I);
     end case;
   end Get;

   procedure Nop (R : Rec_Type) is
   begin
      Put_Line (Boolean'Image (R.B));
   end Nop;

   R0 : constant Rec_Type  := Get (0);
   R1 : constant Rec_Type  := Get (1);
   R11 : constant Rec_Type  := Get (11);
   R100 : Rec_Type := Get (100);

begin
   Nop (R0);
   Nop (R1);
   Nop (R11);
   Put_Line (Character'Image (R1.C));
   Put_Line (Integer'Image (R11.N));
   Put_Line (Integer'Image (R100.N));
end Foo;

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

2014-10-20  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch3.adb (Analyze_Object_Declaration): If the type is
	an unconstrained unchecked_union type, rewrite declaration
	as a renaming to prevent attempt to retrieve non- existent
	discriminants from expression.

Attachment: difs
Description: Text document


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]