comp.lang.ada
 help / color / mirror / Atom feed
From: Simon Wright <simon@pushface.org>
Subject: Type_Invariant and instance creation (was: Type_Invariant and Finalize)
Date: Thu, 17 Jul 2014 22:30:29 +0100
Date: 2014-07-17T22:30:29+01:00	[thread overview]
Message-ID: <lyy4vrwu4q.fsf@pushface.org> (raw)
In-Reply-To: slrnlsg4lu.i0l.lithiumcat@nat.rebma.instinctive.eu

I wrote this to play with some of the concepts Natasha discussed, since
I haven't used the contract aspects before. It's a bit more complicated
than I'd have hoped because of the need for Adjust.

As it stands, it executes as expected with GNAT GPL 2014 (that is, the
finalizations occur OK and don't trigger assertion failures, but the
creation of an empty object does).

With FSF GCC 4.9.0, however, the assertion fails in Create at

      return (Ada.Finalization.Controlled with V => new Integer'(Value));

I tried inserting Put_Lines in the test program to get an idea of the
flow:

   declare
      Tmp : Releasing_Not_Null.R := Releasing_Not_Null.Create (42);
   begin
      Put_Line ("in first declare block");
   end;
   Put_Line ("first declare block done");

and now blow me down if GNAT GPL 2014 doesn't fail just like FSF GCC!

What am I doing wrong? Which compiler (if either) is right?

-- gnatchop from here --
with Ada.Finalization;
package Releasing_Not_Null is
   type R is new Ada.Finalization.Controlled with private;
   function Create (Value : Integer) return R;
private
   type P is access Integer;
   type R is new Ada.Finalization.Controlled with record
      V : P;
   end record
      with Type_Invariant => R.V /= null;
   overriding
   procedure Adjust (This : in out R);
   overriding
   procedure Finalize (This : in out R);
end Releasing_Not_Null;
with Ada.Unchecked_Deallocation;
package body Releasing_Not_Null is
   function Create (Value : Integer) return R is
   begin
      return (Ada.Finalization.Controlled with V => new Integer'(Value));
   end Create;
   procedure Adjust (This : in out R) is
   begin
      This.V := new Integer'(This.V.all);
   end Adjust;
   procedure Finalize (This : in out R) is
      procedure Free is new Ada.Unchecked_Deallocation (Integer, P);
   begin
      if This.V /= null then
         Free (This.V);
      end if;
   end Finalize;
end Releasing_Not_Null;
with Releasing_Not_Null;
procedure Test_Releasing_Not_Null is
begin
   declare  -- this block is OK
      Tmp : Releasing_Not_Null.R := Releasing_Not_Null.Create (42);
   begin
      null;
   end;
   declare
      Tmp : Releasing_Not_Null.R;  -- Failed invariant here
   begin
      null;
   end;
end Test_Releasing_Not_Null;


  parent reply	other threads:[~2014-07-17 21:30 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-07-17 18:15 Type_Invariant and Finalize Natasha Kerensikova
2014-07-17 20:49 ` Simon Wright
2014-07-18  6:56   ` Natasha Kerensikova
2014-07-18 21:48     ` Robert A Duff
2014-07-17 21:30 ` Simon Wright [this message]
2014-07-21 23:29   ` Type_Invariant and instance creation (was: Type_Invariant and Finalize) Randy Brukardt
2014-07-22  1:13     ` Type_Invariant and instance creation Shark8
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox