comp.lang.ada
 help / color / mirror / Atom feed
* GNAT bug with assertions
@ 2018-08-10 10:29 AdaMagica
  2018-08-10 14:23 ` Anh Vo
  2018-08-10 15:16 ` Jacob Sparre Andersen
  0 siblings, 2 replies; 12+ messages in thread
From: AdaMagica @ 2018-08-10 10:29 UTC (permalink / raw)


I would have expected that predicates are also checked on return values as they are checked on parameters.
In the following test, no exception is raised on call of funcction Wrong although it returns a value not fulfilling the predicate.

Is this a GNAT bug or my wrong expectation?

package Pack is

  type Priv is private;
  C: constant Priv;

  function Test (X: Priv) return Boolean;
  subtype Subt is Priv with Dynamic_Predicate => (Test (Subt));

  function Wrong return Subt;
  function Good (X: Subt) return Boolean;

private

  type Priv is new Integer;
  C: constant Priv := -1;

  function Test (X: Priv) return Boolean is (X > 0);

  function Wrong return Subt is (-1);
  function Good (X: Subt) return Boolean is (True);

end Pack; 

with Ada.Assertions, Ada.Text_IO;
use  Ada.Assertions, Ada.Text_IO;

with Pack;
use  Pack;

procedure Test_Pack is
begin

  begin
    Put_Line (Good (C)'Image);
  exception
    when Assertion_Error =>
      Put_Line ("Good (C) raises Assertion_Error => OK");
  end;

  declare
    X: Priv;
  begin
    X := Wrong;
    Put_Line ("Assertion_Error expected by calling Wrong => KO");
  exception
    when Assertion_Error =>
      Put_Line ("Wrong raises Assertion_Error => OK");
  end;

end Test_Pack;

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-10 10:29 GNAT bug with assertions AdaMagica
@ 2018-08-10 14:23 ` Anh Vo
  2018-08-10 15:16 ` Jacob Sparre Andersen
  1 sibling, 0 replies; 12+ messages in thread
From: Anh Vo @ 2018-08-10 14:23 UTC (permalink / raw)


On Friday, August 10, 2018 at 3:29:34 AM UTC-7, AdaMagica wrote:
> I would have expected that predicates are also checked on return values as they are checked on parameters.
> In the following test, no exception is raised on call of funcction Wrong although it returns a value not fulfilling the predicate.
> 
> Is this a GNAT bug or my wrong expectation?
> 
> package Pack is
> 
>   type Priv is private;
>   C: constant Priv;
> 
>   function Test (X: Priv) return Boolean;
>   subtype Subt is Priv with Dynamic_Predicate => (Test (Subt));
> 
>   function Wrong return Subt;
>   function Good (X: Subt) return Boolean;
> 
> private
> 
>   type Priv is new Integer;
>   C: constant Priv := -1;
> 
>   function Test (X: Priv) return Boolean is (X > 0);
> 
>   function Wrong return Subt is (-1);
>   function Good (X: Subt) return Boolean is (True);
> 
> end Pack; 
> 
> with Ada.Assertions, Ada.Text_IO;
> use  Ada.Assertions, Ada.Text_IO;
> 
> with Pack;
> use  Pack;
> 
> procedure Test_Pack is
> begin
> 
>   begin
>     Put_Line (Good (C)'Image);
>   exception
>     when Assertion_Error =>
>       Put_Line ("Good (C) raises Assertion_Error => OK");
>   end;
> 
>   declare
>     X: Priv;
>   begin
>     X := Wrong;
>     Put_Line ("Assertion_Error expected by calling Wrong => KO");
>   exception
>     when Assertion_Error =>
>       Put_Line ("Wrong raises Assertion_Error => OK");
>   end;
> 
> end Test_Pack;

I assume you used -gnata switch for your GNAT compilation. Otherwise, you will not see the effect.

Anh Vo


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-10 10:29 GNAT bug with assertions AdaMagica
  2018-08-10 14:23 ` Anh Vo
@ 2018-08-10 15:16 ` Jacob Sparre Andersen
  2018-08-10 16:16   ` Simon Wright
  1 sibling, 1 reply; 12+ messages in thread
From: Jacob Sparre Andersen @ 2018-08-10 15:16 UTC (permalink / raw)


AdaMagica wrote:

> I would have expected that predicates are also checked on return
> values as they are checked on parameters.  In the following test, no
> exception is raised on call of funcction Wrong although it returns a
> value not fulfilling the predicate.
>
> Is this a GNAT bug or my wrong expectation?

It seems that your expectations are slightly off.

Contracts are only checked if the assertion policy is set to "Check".
You can do that, either by configuring your compiler to have the
assertion policy set to "Check", or by explicitly inserting:

   pragma Assertion_Policy (Check);

at the beginning of the units, which should have this assertion policy.

If I remember correctly, setting the assertion policy of a package
specification to "Check" means that the contracts of the subprograms
specified in the package are checked, even if they are called from a
unit with assertion policy "Ignore".

Greetings,

Jacob
-- 
Computers are not intelligent.  They only think they are.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-10 15:16 ` Jacob Sparre Andersen
@ 2018-08-10 16:16   ` Simon Wright
  2018-08-10 20:42     ` Randy Brukardt
  0 siblings, 1 reply; 12+ messages in thread
From: Simon Wright @ 2018-08-10 16:16 UTC (permalink / raw)


Jacob Sparre Andersen <jacob@jacob-sparre.dk> writes:

> AdaMagica wrote:
>
>> I would have expected that predicates are also checked on return
>> values as they are checked on parameters.  In the following test, no
>> exception is raised on call of funcction Wrong although it returns a
>> value not fulfilling the predicate.
>>
>> Is this a GNAT bug or my wrong expectation?
>
> It seems that your expectations are slightly off.

Or perhaps you, Jacob, have a later release of the compiler which has
this bug fixed?

> Contracts are only checked if the assertion policy is set to "Check".
> You can do that, either by configuring your compiler to have the
> assertion policy set to "Check", or by explicitly inserting:
>
>    pragma Assertion_Policy (Check);
>
> at the beginning of the units, which should have this assertion policy.

Or, in the case of GNAT, by giving -gnata: see
http://docs.adacore.com/gnat_ugn-docs/html/gnat_ugn/gnat_ugn/building_executable_programs_with_gnat.html#debugging-and-assertion-control

If Cristoph hadn't been using -gnata, the first check would have failed.

IMO this is a bug in GNAT, present in CE2018 and FSF GCC 8 (and maybe
earlier, didn't check).


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-10 16:16   ` Simon Wright
@ 2018-08-10 20:42     ` Randy Brukardt
  2018-08-12 18:35       ` AdaMagica
  0 siblings, 1 reply; 12+ messages in thread
From: Randy Brukardt @ 2018-08-10 20:42 UTC (permalink / raw)


"Simon Wright" <simon@pushface.org> wrote in message 
news:lyd0uqyz76.fsf@pushface.org...
...
> IMO this is a bug in GNAT, present in CE2018 and FSF GCC 8 (and maybe
> earlier, didn't check).

Looks likely (I didn't check the wording to be certain). Predicate checks 
should occur any time there is a conversion of an entire object to the 
appropriate subtype, and that should happen in a return statement.

                                        Randy.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-10 20:42     ` Randy Brukardt
@ 2018-08-12 18:35       ` AdaMagica
  2018-08-12 18:51         ` Simon Wright
  0 siblings, 1 reply; 12+ messages in thread
From: AdaMagica @ 2018-08-12 18:35 UTC (permalink / raw)


Thank you all for your time to think about this.

I'll send it in to AdaCore. Hopefully it will be correted next year in the public version.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-12 18:35       ` AdaMagica
@ 2018-08-12 18:51         ` Simon Wright
  2018-08-13  9:03           ` AdaMagica
  0 siblings, 1 reply; 12+ messages in thread
From: Simon Wright @ 2018-08-12 18:51 UTC (permalink / raw)


AdaMagica <christ-usch.grein@t-online.de> writes:

> Thank you all for your time to think about this.
>
> I'll send it in to AdaCore. Hopefully it will be correted next year in
> the public version.

This is a somewhat artificial error, isn't it?

   function Maybe (X : Priv) return Subt;
   ...
   function Maybe (X : Priv) return Subt is (X);
   ...
   X := Maybe (C);   -- C is -1

works as we would have expected.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-12 18:51         ` Simon Wright
@ 2018-08-13  9:03           ` AdaMagica
  2018-08-13  9:07             ` Simon Wright
  0 siblings, 1 reply; 12+ messages in thread
From: AdaMagica @ 2018-08-13  9:03 UTC (permalink / raw)


Am Sonntag, 12. August 2018 20:51:51 UTC+2 schrieb Simon Wright:
> AdaMagica writes:
> 
> > Thank you all for your time to think about this.
> >
> > I'll send it in to AdaCore. Hopefully it will be correted next year in
> > the public version.
> 
> This is a somewhat artificial error, isn't it?
> 
>    function Maybe (X : Priv) return Subt;
>    ...
>    function Maybe (X : Priv) return Subt is (X);
>    ...
>    X := Maybe (C);   -- C is -1
> 
> works as we would have expected.

What is it that you expect here? What type is X? I didn't try with GNAT, but I would expect Maybe (C) to raise Assertion_Error independently of X.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-13  9:03           ` AdaMagica
@ 2018-08-13  9:07             ` Simon Wright
  2018-08-13 15:54               ` Simon Wright
  0 siblings, 1 reply; 12+ messages in thread
From: Simon Wright @ 2018-08-13  9:07 UTC (permalink / raw)


AdaMagica <christ-usch.grein@t-online.de> writes:

> Am Sonntag, 12. August 2018 20:51:51 UTC+2 schrieb Simon Wright:
>> AdaMagica writes:
>> 
>> > Thank you all for your time to think about this.
>> >
>> > I'll send it in to AdaCore. Hopefully it will be correted next year
>> > in the public version.
>> 
>> This is a somewhat artificial error, isn't it?
>> 
>>    function Maybe (X : Priv) return Subt;
>>    ...
>>    function Maybe (X : Priv) return Subt is (X);
>>    ...
>>    X := Maybe (C);   -- C is -1
>> 
>> works as we would have expected.
>
> What is it that you expect here? What type is X? I didn't try with
> GNAT, but I would expect Maybe (C) to raise Assertion_Error
> independently of X.

Oh come on! these are additions to the example that *you* posted upthread.


^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-13  9:07             ` Simon Wright
@ 2018-08-13 15:54               ` Simon Wright
  2018-08-13 16:55                 ` Simon Wright
  2018-08-14  2:37                 ` AdaMagica
  0 siblings, 2 replies; 12+ messages in thread
From: Simon Wright @ 2018-08-13 15:54 UTC (permalink / raw)


Simon Wright <simon@pushface.org> writes:

> AdaMagica <christ-usch.grein@t-online.de> writes:
>
>> Am Sonntag, 12. August 2018 20:51:51 UTC+2 schrieb Simon Wright:
>>> AdaMagica writes:
>>>
>>> > Thank you all for your time to think about this.
>>> >
>>> > I'll send it in to AdaCore. Hopefully it will be correted next year
>>> > in the public version.
>>>
>>> This is a somewhat artificial error, isn't it?
>>>
>>>    function Maybe (X : Priv) return Subt;
>>>    ...
>>>    function Maybe (X : Priv) return Subt is (X);
>>>    ...
>>>    X := Maybe (C);   -- C is -1
>>>
>>> works as we would have expected.
>>
>> What is it that you expect here? What type is X? I didn't try with
>> GNAT, but I would expect Maybe (C) to raise Assertion_Error
>> independently of X.
>
> Oh come on! these are additions to the example that *you* posted upthread.

Oops.

X is of type Subt.

It can get quite misleading to use expression functions when trying to
use the debugger to work out where an exception is raised.

Given

   function Maybe (X : Priv) return Subt is
   begin
      return X;
   end Maybe;

called as

   S : Subt := Maybe (-);
   
Maybe happily returns -1 and leaves it up to the assignment to raise the
necessary exception.

And if the assignment is

   P : Priv := Maybe (-1);

no exception is raised.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-13 15:54               ` Simon Wright
@ 2018-08-13 16:55                 ` Simon Wright
  2018-08-14  2:37                 ` AdaMagica
  1 sibling, 0 replies; 12+ messages in thread
From: Simon Wright @ 2018-08-13 16:55 UTC (permalink / raw)


Simon Wright <simon@pushface.org> writes:

>    S : Subt := Maybe (-);

   S : Subt := Maybe (-1);

Not doing well today.

^ permalink raw reply	[flat|nested] 12+ messages in thread

* Re: GNAT bug with assertions
  2018-08-13 15:54               ` Simon Wright
  2018-08-13 16:55                 ` Simon Wright
@ 2018-08-14  2:37                 ` AdaMagica
  1 sibling, 0 replies; 12+ messages in thread
From: AdaMagica @ 2018-08-14  2:37 UTC (permalink / raw)


Am Montag, 13. August 2018 17:54:28 UTC+2 schrieb Simon Wright:
>    function Maybe (X : Priv) return Subt is
>    begin
>       return X;
>    end Maybe;
> 
> called as
> 
>    S : Subt := Maybe (-1);
>    
> Maybe happily returns -1 and leaves it up to the assignment to raise the
> necessary exception.
> 
> And if the assignment is
> 
>    P : Priv := Maybe (-1);
> 
> no exception is raised.

Yes, this is what currently happens. Assertion_Error should instead be raised in both cases by Maybe.

^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2018-08-14  2:37 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-08-10 10:29 GNAT bug with assertions AdaMagica
2018-08-10 14:23 ` Anh Vo
2018-08-10 15:16 ` Jacob Sparre Andersen
2018-08-10 16:16   ` Simon Wright
2018-08-10 20:42     ` Randy Brukardt
2018-08-12 18:35       ` AdaMagica
2018-08-12 18:51         ` Simon Wright
2018-08-13  9:03           ` AdaMagica
2018-08-13  9:07             ` Simon Wright
2018-08-13 15:54               ` Simon Wright
2018-08-13 16:55                 ` Simon Wright
2018-08-14  2:37                 ` AdaMagica

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