Browse Source

Disallow null condition in ternary (#10494)

* Disallow null condition in ternary

* type

* use type_if
RblSb 3 years ago
parent
commit
5e0a0299e3

+ 7 - 3
src/typing/typer.ml

@@ -1444,8 +1444,12 @@ and type_cast ctx e t p =
 	let texpr = loop t in
 	mk (TCast (type_expr ctx e WithType.value,Some texpr)) t p
 
-and type_if ctx e e1 e2 with_type p =
+and type_if ctx e e1 e2 with_type is_ternary p =
 	let e = type_expr ctx e WithType.value in
+	if is_ternary then begin match e.eexpr with
+		| TConst TNull -> typing_error "Cannot use null as ternary condition" e.epos
+		| _ -> ()
+	end;
 	let e = AbstractCast.cast_or_unify ctx ctx.t.tbool e p in
 	let e1 = type_expr ctx (Expr.ensure_block e1) with_type in
 	(match e2 with
@@ -1722,9 +1726,9 @@ and type_expr ?(mode=MGet) ctx (e,p) (with_type:WithType.t) =
 	| EFor (it,e2) ->
 		ForLoop.type_for_loop ctx TyperDisplay.handle_display it e2 p
 	| ETernary (e1,e2,e3) ->
-		type_expr ctx (EIf (e1,e2,Some e3),p) with_type
+		type_if ctx e1 e2 (Some e3) with_type true p
 	| EIf (e,e1,e2) ->
-		type_if ctx e e1 e2 with_type p
+		type_if ctx e e1 e2 with_type false p
 	| EWhile (cond,e,NormalWhile) ->
 		let old_loop = ctx.in_loop in
 		let cond = type_expr ctx cond WithType.value in

+ 6 - 0
tests/misc/projects/Issue8634/Main.hx

@@ -0,0 +1,6 @@
+class Main {
+	static function main() {
+		var s:Null<String> = null;
+		trace(s = null ? "null" : "not null");
+	}
+}

+ 2 - 0
tests/misc/projects/Issue8634/compile-fail.hxml

@@ -0,0 +1,2 @@
+--main Main
+--interp

+ 2 - 0
tests/misc/projects/Issue8634/compile-fail.hxml.stderr

@@ -0,0 +1,2 @@
+Main.hx:4: characters 13-17 : Cannot use null as ternary condition
+Main.hx:4: characters 13-17 : ... For function argument 'v'