comp.lang.ada
 help / color / mirror / Atom feed
* Why no constraint error?
@ 1997-03-20  0:00 Samuel Mize
  1997-03-20  0:00 ` Samuel Mize
  0 siblings, 1 reply; 12+ messages in thread
From: Samuel Mize @ 1997-03-20  0:00 UTC (permalink / raw)




Greetings, all,

The following question has come up on chat@gnat.com, but it's really
a general Ada question and I'm throwing it out to comp.lang.ada.

It's probably been asked and answered before, but I can't find a
good explanation on DejaNews.

The question is, why doesn't this code raise an exception when run
under GNAT?

    pragma Normalize_Scalars;
    with Ada.Text_Io;
    procedure Test_Subrange_Checks is
        type T_Source  is new Integer range 7 .. 10;
        type T_Target is new Integer range 7 .. 10; -- identical ranges

        Source: T_Source; -- initialized out of range by Normalize_Scalars
        Target: T_Target := 10;
    begin
        Target := T_Target (Source);  -- no range check occurs!!!!!!!!!!
        Ada.Text_Io.Put_Line (T_Target'Image (Target));
    end Test_Subrange_Checks;


Per 13.9.1(9-11), evaluating the uninitialized variable quite properly
returns an invalid value.  However, in the assignment, this value
"is converted to the subtype of the target" per 5.2(11).  In type
conversion, "[a]fter conversion of the value to the target type, if
the target subtype is constrained, a check is performed that the value
satisfies this constraint" per 4.6(51).

11.6(5) provides permission to skip language-defined checks if this
would not "have some effect on the external interactions of the
program."  Since program output is an external file, this is an
external effect per 1.1.3(8-9).

If your reply is the following, please consider it already sent:

* Trust me, GNAT is right.

* Informally, it makes sense to omit this check, since the ranges
  of the types statically match.

I'm looking for an ARM reference that makes it legal to omit the
check named in 4.6(51) in this case.  I want to understand the
specific language rule, so I can tell when it will or won't apply.

I'd appreciate email, but will also check this group for replies.

Thanks very much for your time and attention.

Samuel Mize

-- 
Samuel Mize -- smize@imagin.net -- Team Ada
(personal net account)




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

end of thread, other threads:[~1997-03-24  0:00 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1997-03-20  0:00 Why no constraint error? Samuel Mize
1997-03-20  0:00 ` Samuel Mize
1997-03-21  0:00   ` Robert A Duff
1997-03-22  0:00     ` Tucker Taft
1997-03-22  0:00       ` Robert A Duff
1997-03-24  0:00         ` Tucker Taft
1997-03-24  0:00       ` Samuel A. Mize
1997-03-24  0:00         ` Robert Dewar
1997-03-24  0:00         ` Robert A Duff
1997-03-24  0:00           ` Robert Dewar
1997-03-22  0:00     ` Robert Dewar
1997-03-22  0:00       ` Robert A Duff

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