This change makes sure the compiler produces a proper error (rather
than crash) when compiling an (illegal) IF-expression where THEN-expression
is overloaded, and none of its interpretation is compatible with
the ELSE-expression.

The following compilation must display:

$ gcc -c badelse.adb
badelse.adb:4:50: type incompatible with that of "then" expression

package Badelse is
   type K is (Unknown, Blue, Red);
   type Tristate is (False, True, Unknown);
   Boo : Boolean;
   procedure P (X : K);
end Badelse;
package body Badelse is
   procedure P (X : K) is
   begin
      Boo := (if X = Unknown then Unknown else X = Blue);
   end P;
end Badelse;

Tested on x86_64-pc-linux-gnu, committed on trunk

2015-11-12  Thomas Quinot  <qui...@adacore.com>

        * sem_ch4.adb (analyze_If_Expression): Reject IF-expression where
        THEN-expression is overloaded and none of its interpretation is
        compatible with the ELSE-expression.

Index: sem_ch4.adb
===================================================================
--- sem_ch4.adb (revision 230239)
+++ sem_ch4.adb (working copy)
@@ -2191,6 +2191,17 @@
 
                Get_Next_Interp (I, It);
             end loop;
+
+            --  If no valid interpretation has been found, then the type of
+            --  the ELSE expression does not match any interpretation of
+            --  the THEN expression.
+
+            if Etype (N) = Any_Type then
+               Error_Msg_N
+                 ("type incompatible with that of `THEN` expression",
+                  Else_Expr);
+               return;
+            end if;
          end;
       end if;
    end Analyze_If_Expression;

Reply via email to