comp.lang.ada
 help / color / mirror / Atom feed
* Re: Static assertions
@ 2001-05-18  5:34 Christoph Grein
  2001-05-18 13:38 ` Robert A Duff
  0 siblings, 1 reply; 18+ messages in thread
From: Christoph Grein @ 2001-05-18  5:34 UTC (permalink / raw)
  To: comp.lang.ada

Robert A Duff wrote:

> Interesting.  But shouldn't it be Assert'(...)?
>                                         ^ ie a qualified expression

  package Verifier is
    subtype Assert is Boolean range True .. True;

    Assert_1 : constant := Boolean'Pos (Assert (Integer'Size = 16));
    Assert_2 : constant := Boolean'Pos
				  (Assert (Integer'Size = 2 * Character'Size));
  end Verifier;

With a type conversion this compiles on my Apex Ada 95 Compiler 3.0.0b on 
Solaris and produces "0 0", with a qualified expression, it gives me the error 
message:

Boolean'Pos (Assert'(Integer'Size = 16)) is disallowed since it is static and 
will raise Constraint_Error [RM_95 4.9(34)]

It seems like the type conversion is ignored.

ARM 4.6(28)
For the evaluation of a type_conversion that is a value conversion, the operand 
is evaluated, and then the value of the operand is converted to a corresponding 
value of the target type, if any. If there is no value of the target type that 
corresponds to the operand value, Constraint_Error is raised[; this can only 
happen on conversion to a modular type, and only when the operand value is 
outside the base range of the modular type.] Additional rules follow:

ARM 4.6(34) Enumeration Type Conversion

ARM 4.6(35)
The result is the value of the target type with the same position number as that 
of the operand value.

Thus there is nowhere specified that the subtype range is checked.
   Put_Line (Integer'Image (Natural (-2.3)));
compiles just fine and produces -2.

ARM 4.7(4)
The evaluation of a qualified_expression evaluates the operand (and if of a 
universal type, converts it to the type determined by the subtype_mark) and 
checks that its value belongs to the subtype denoted by the subtype_mark. The 
exception Constraint_Error is raised if this check fails. 

So thanx to Robert Duff for hinting.

Christoph Grein





^ permalink raw reply	[flat|nested] 18+ messages in thread
* Static assertions
@ 2001-05-17 15:07 Jean-Pierre Rosen
  2001-05-17 17:44 ` Jeffrey Carter
                   ` (3 more replies)
  0 siblings, 4 replies; 18+ messages in thread
From: Jean-Pierre Rosen @ 2001-05-17 15:07 UTC (permalink / raw)


Still discovering how powerful Ada is...

Imagine your program relies on Integer having a certain size, or being sure
that the size of Integer is four times the size of character, or things like
that, and you want to make sure that the program does not even compile if
the assertions do not hold (because you hate fighting with a debugger). This
is possible, since a static expression that raises constraint_error will not
compile. Just include something like the following package:

package Verifier is
   subtype Assert is Boolean range True..True;

   Assert_1 : constant := Boolean'Pos( Assert (Integer'Size=32) );
   Assert_2 : constant := Boolean'Pos( Assert
(Integer'Size=4*Character'Size) );
end Verifier;

(The Boolean'Pos serves to have a value used in a named number declaration,
which requires a static context).
--
---------------------------------------------------------
           J-P. Rosen (rosen@adalog.fr)
Visit Adalog's web site at http://www.adalog.fr





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

end of thread, other threads:[~2001-06-12  3:59 UTC | newest]

Thread overview: 18+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2001-05-18  5:34 Static assertions Christoph Grein
2001-05-18 13:38 ` Robert A Duff
2001-05-18 16:15   ` Jeffrey Carter
2001-05-18 17:25     ` Ted Dennison
2001-05-18 21:27     ` Robert A Duff
2001-05-18 22:46       ` Jeffrey Carter
2001-05-19 22:53         ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
2001-05-17 15:07 Jean-Pierre Rosen
2001-05-17 17:44 ` Jeffrey Carter
2001-05-17 19:09   ` Marin David Condic
2001-05-17 22:34     ` Jeffrey Carter
2001-05-18 13:39       ` Marin David Condic
2001-05-18  1:23 ` Robert A Duff
2001-05-19  7:40 ` David Kristola
2001-05-19 22:56   ` Robert A Duff
2001-05-19  8:36 ` Aaro Koskinen
2001-05-21  7:50   ` Jean-Pierre Rosen
2001-06-12  3:59     ` David Thompson

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