Hello everyone,

[Christoph wrote]
>
> Peter Amey schrieb:
> > The only surprise to me is that the error occurs at run-time; I would expect
> >  a compiler to spot the static out-of-range value during compilation.
>
> That is because it is a legal program, which must be accepted by the compiler. A
> helpful compiler would spot the problem and output a warning that
> Constraint_Error will be raised.
> >
> > Peter
>
> Christoph Grein

Indeed, GNAT will do just such a friendy thing:

Compiling: test_array.adb (source file time stamp: 2003-07-09 18:46:40)

     1. procedure Test_Array is
     2.
     3.   A: array (1..10) of integer;
     4.
     5. begin
     6.
     7.   A(10..11) := A(7..8);
                1      3
        >>> warning: static value out of range of subtype of "Standard.Integer"
defined at line 3
        >>> warning: "Constraint_Error" will be raised at run time
        >>> warning: "A" may be referenced before it has a value

     8.
     9. end Test_Array;
    10.

Note that all these "errors" are runtime things; the program itself
is legal and so must be accepted. One reason for this is to allow the
programmer to write clear tests of his exception-handling strategy.
If the compiler rejected every program whose execution it "knew"
would raise an exception, the only way to force exceptions would be
to write tricky code to hide it from the compiler.

BTW - GNAT also has a flag (one of many, many flags) that will treat
warnings as errors, i.e. not generate code. I use this flag with my
introductory courses, because for most beginners, this kind of
"error" was not intended, but they also ignore warnings unless
you force them to react to them.

Since GNAT is so accessible and easy to download for just about
every platform, I think every Ada programmer ought to have a copy
installed, just to get a "second opinion" on compilation issues.
If two compilers disagree, it's a good idea to find out why.:-)

Yours truly,

Michael Feldman
------------------------------------------------------------------------
Michael B. Feldman -  chair, ACM SIGAda Education Working Group
Professor, Department of Computer Science
The George Washington University -  Washington, DC 20052 USA
NOTE new e-mail address: [log in to unmask]
202-994-5919 (voice) - 202-994-4875 (fax)
http://www.seas.gwu.edu/~mfeldman
------------------------------------------------------------------------
"Teach me to be an engineer. I don't care if it takes all day."
from a Dilbert comic strip
------------------------------------------------------------------------
Visit http://www.acm.org/sigada/education or http://www.adapower.com
------------------------------------------------------------------------