From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!newspeer1.nac.net!newsfeed.xs4all.nl!newsfeed1a.news.xs4all.nl!xs4all!news.stack.nl!eternal-september.org!feeder.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Type_Invariant and instance creation (was: Type_Invariant and Finalize) Date: Thu, 17 Jul 2014 22:30:29 +0100 Organization: A noiseless patient Spider Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain Injection-Info: mx05.eternal-september.org; posting-host="5cbec41b82bafb9ba55107f02585e18b"; logging-data="18970"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19xgebuVGmYwSRvu3LqomcwFbKRbev4X1c=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (darwin) Cancel-Lock: sha1:H/vkeeal5mC1hXfL66EoHikF5Aw= sha1:1pRUA48pNeblzDRb4I8/9+PzwBA= Xref: number.nntp.dca.giganews.com comp.lang.ada:187679 Date: 2014-07-17T22:30:29+01:00 List-Id: 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;