diff mbox

[Ada] Crash on illegal use of limited view of type

Message ID 20160427124813.GA125622@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet April 27, 2016, 12:48 p.m. UTC
This patch fixes a compiler abort on an illegal program that attempts to
make use of the non-limited view of a type in the private part of a unit that
has a limited_private with_clause on the unit that declared the type.

Compiling unit_test05.adb must yield:

   unit_test05.ads:46:23: invalid use of type before its full declaration

---
limited private with with_private;   
package unit_test05 is

   type private_type is private;

   function public_fn( x : integer )  return private_type;
   --function public_fn( x : integer )  return integer;

PRIVATE
   function  private_fn( x : BOOLEAN := true )
             return with_private.small;

   type private_type is record
      f : with_private.small;
   end record;

end unit_test05;
--
private with  with_private;

package body unit_test05 is

   function public_fn( x : integer )  return private_type is
   --function public_fn( x : integer )  return integer is

      value : private_type;

   begin
      -- the body can see public declarations in with_private
      -- the initialization of W is private.
      --
      value.f := 5;
      IF with_private.W  THEN RETURN value; END IF;

      return value;      
      --return  x + 5;
   end public_fn;

   function private_fn( x : BOOLEAN := true )
            return with_private.small is
   begin
      -- the body can see public declarations in with_private
      --
      IF with_private.z THEN RETURN 5; ELSE RETURN 7; END IF;
      --return  x + 5;
   end private_fn;
end unit_test05;
---
package with_private is

   package T1_Pkg is
      type T1 is tagged null record;
      procedure Prim_Proc (X : T1);

      T1_Var : T1;
      procedure Cw_Operand (X : T1'Class := T1_Var);
   end T1_Pkg;

   W : constant Boolean;
   Z : constant Boolean := TRUE;

   type small is new integer;

private
   W : constant Boolean := true;

end with_private;

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

2016-04-27  Ed Schonberg  <schonberg@adacore.com>

	* sem_ch10.adb (Build_Limited_View, Decorate_Type): If this
	is a limited view of a type, initialize the Limited_Dependents
	field to catch misuses of the type in a client unit.
diff mbox

Patch

Index: sem_ch10.adb
===================================================================
--- sem_ch10.adb	(revision 235482)
+++ sem_ch10.adb	(working copy)
@@ -84,6 +84,13 @@ 
    --  required in order to avoid passing non-decorated entities to the
    --  back-end. Implements Ada 2005 (AI-50217).
 
+   procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
+   --  Common processing for all stubs (subprograms, tasks, packages, and
+   --  protected cases). N is the stub to be analyzed. Once the subunit name
+   --  is established, load and analyze. Nam is the non-overloadable entity
+   --  for which the proper body provides a completion. Subprogram stubs are
+   --  handled differently because they can be declarations.
+
    procedure Check_Body_Needed_For_SAL (Unit_Name : Entity_Id);
    --  Check whether the source for the body of a compilation unit must be
    --  included in a standalone library.
@@ -203,13 +210,6 @@ 
    procedure Unchain (E : Entity_Id);
    --  Remove single entity from visibility list
 
-   procedure Analyze_Proper_Body (N : Node_Id; Nam : Entity_Id);
-   --  Common processing for all stubs (subprograms, tasks, packages, and
-   --  protected cases). N is the stub to be analyzed. Once the subunit name
-   --  is established, load and analyze. Nam is the non-overloadable entity
-   --  for which the proper body provides a completion. Subprogram stubs are
-   --  handled differently because they can be declarations.
-
    procedure sm;
    --  A dummy procedure, for debugging use, called just before analyzing the
    --  main unit (after dealing with any context clauses).
@@ -1489,7 +1489,7 @@ 
 
                            --  Check if the named package (or some ancestor)
                            --  leaves visible the full-view of the unit given
-                           --  in the limited-with clause
+                           --  in the limited-with clause.
 
                            loop
                               if Designate_Same_Unit (Lim_Unit_Name,
@@ -5633,16 +5633,20 @@ 
 
       begin
          --  An unanalyzed type or a shadow entity of a type is treated as an
-         --  incomplete type.
+         --  incomplete type, and carries the corresponding attributes.
 
-         Set_Ekind             (Ent, E_Incomplete_Type);
-         Set_Etype             (Ent, Ent);
-         Set_Full_View         (Ent, Empty);
-         Set_Is_First_Subtype  (Ent);
-         Set_Scope             (Ent, Scop);
-         Set_Stored_Constraint (Ent, No_Elist);
-         Init_Size_Align       (Ent);
+         Set_Ekind              (Ent, E_Incomplete_Type);
+         Set_Etype              (Ent, Ent);
+         Set_Full_View          (Ent, Empty);
+         Set_Is_First_Subtype   (Ent);
+         Set_Scope              (Ent, Scop);
+         Set_Stored_Constraint  (Ent, No_Elist);
+         Init_Size_Align        (Ent);
 
+         if From_Limited_With (Ent) then
+            Set_Private_Dependents (Ent, New_Elmt_List);
+         end if;
+
          --  A tagged type and its corresponding shadow entity share one common
          --  class-wide type. The list of primitive operations for the shadow
          --  entity is empty.