Search code examples
adaada2012

Type conversions and if expressions


In this page, John Barnes writes:

If the conditional expression is the argument of a type conversion then effectively the conversion is considered pushed down to the dependent expressions. Thus

X := Float(if P then A else B);

is equivalent to

X := (if P then Float(A) else Float(B));

So why can't I compile the following program under GNAT 10.3.0?

procedure Main is
   P : Boolean := True;
   X : Float;
begin
   X := Float (if P then 0.5 else 32);
end Main;
Compile
   [Ada]          main.adb
main.adb:5:35: expected a real type
main.adb:5:35: found type universal integer
gprbuild: *** compilation phase failed

Solution

  • Because you’ve found a long-standing error in the compiler! (same behaviour in GCC 12.1.0).

    John Barnes’ justification is at AARM 4.5.7(10ff).


    thanks for providing real code and the error messages!