comp.lang.ada
 help / color / mirror / Atom feed
* Incompatibility involving universal expressions
@ 1998-10-08  0:00 jhassett
  1998-10-12  0:00 ` jhassett
  0 siblings, 1 reply; 3+ messages in thread
From: jhassett @ 1998-10-08  0:00 UTC (permalink / raw)




I've run into an apparent Ada 83/Ada 95 incompatibility that
surprised me because it seems that it would be rather common, and
I haven't seen it documented (e.g., in Bill Taylor's Ada
Compatibility Guide).  The changes that cause the incompatibility
are apparently motivated by a desire to eliminate Beaujolais
effects (http://www.adahome.com/FAQ/programming.html#beaujolais).
I'm wondering if I've misunderstood the situation, and hoping
someone can set me straight if I have.

Here is an example:

with Text_IO;
package P is
  N : constant := 10;
  type T is new Text_IO.Count range 2 .. N - 1;
end P;

This compiles under the Rational VADS Ada 83 compiler, but not
under the Green Hills Ada 95 compiler ("Error: line 4 col 39
LRM:8.6(28), Expression has no possible interpretation as an
expression of the type Count, Continuing").  Maybe Green Hills (or
Rational) and I have both missed something, but here's my
understanding of the incompatibility:

In Ada 83, there are operators defined for type universal_integer
(RM83-4.10), so the expression "N - 1" is a universal expression,
yielding a universal_integer value.  Both the literal "2" and "N -
1" are implicitly converted (RM83-4.6(15)) to type Text_IO.Count,
which is the type required for the range constraint (RM83-3.5(4)).
Thus, the declaration of T is valid even though the "-" operator
for type Text_IO.Count is not directly visible.

In Ada 95, there are no operators defined for universal types
(RM95-3.4.1(7)), so the "universal expressions" of Ada 83 become
expressions of the corresponding root types, and "N - 1" is
interpreted as having type root_integer.  The Ada 83 rules about
implicit conversion of universal types are replaced by rules
allowing a construct to have a universal type when the expected
type is covered by the universal type (RM95-8.6(24) and
AARM95-8.6(34.b)).  But this doesn't allow an expression of
a root type to be accepted where a descendant is expected (see
AARM95-3.4.1(14a)), so when "N - 1" is interpreted as having type
root_integer, it is not acceptable.

There are several ways to fix the illegal declaration in Ada 95.
One is to make the "-" operator for type Text_IO.Count directly
visible, so that "N - 1" can be interpreted as having type
Text_IO.Count:

with Text_IO;
package P is
  use type Text_IO.Count;
  N : constant := 10;
  type T is new Text_IO.Count range 2 .. N - 1;
end P;


Another fix is to explicitly convert "N - 1":

  type T is new Text_IO.Count range 2 .. Text_IO.Count(N - 1);


The rules for number declarations specifically allow expressions
of any numeric type, though the named numbers themselves have
universal types, so this is also legal:

with Text_IO;
package P is
  N : constant := 10;
  K : constant := N - 1;
  type T is new Text_IO.Count range 2 .. K;
end P;


If I've got all this right, it seems that there would be lots of
contexts where Ada 83 universal expressions involving operators
become expressions having root types in Ada 95, and are not
accepted.  Is this as common as I think it is?


- Jim Hassett

-----== Posted via Deja News, The Leader in Internet Discussion ==-----
http://www.dejanews.com/rg_mkgrp.xp   Create Your Own Free Member Forum




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

end of thread, other threads:[~1998-10-12  0:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
1998-10-08  0:00 Incompatibility involving universal expressions jhassett
1998-10-12  0:00 ` jhassett
1998-10-12  0:00   ` Tucker Taft

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