diff mbox

[Ada] Crash on inconsistent IF-expression

Message ID 20151112114205.GA72284@adacore.com
State New
Headers show

Commit Message

Arnaud Charlet Nov. 12, 2015, 11:42 a.m. UTC
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  <quinot@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.
diff mbox

Patch

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;