Browse Source

[optimizer] don't choke on `EnumValue(arg) == null`

closes #9200
Simon Krajewski 5 years ago
parent
commit
b5b9d1e0f6
2 changed files with 23 additions and 4 deletions
  1. 9 4
      src/optimization/optimizerTexpr.ml
  2. 14 0
      tests/unit/src/unit/issues/Issue9200.hx

+ 9 - 4
src/optimization/optimizerTexpr.ml

@@ -206,11 +206,16 @@ let optimize_binop e op e1 e2 =
 		| OpEq -> { e with eexpr = TConst (TBool (f1 == f2)) }
 		| OpNotEq -> { e with eexpr = TConst (TBool (f1 != f2)) }
 		| _ -> e)
-	| _, TCall ({ eexpr = TField (_,FEnum _) },_) | TCall ({ eexpr = TField (_,FEnum _) },_), _ ->
-		(match op with
-		| OpAssign -> e
+	| e1, TCall ({ eexpr = TField (_,FEnum _) },el) | TCall ({ eexpr = TField (_,FEnum _) },el),e1 ->
+		begin match op,e1 with
+		| (OpEq | OpNotEq),TConst TNull ->
+			let e0 = {e with eexpr = TConst (TBool (op = OpNotEq))} in
+			{e with eexpr = TBlock (el @ [e0])}
+		| OpAssign,_ ->
+			e
 		| _ ->
-			error "You cannot directly compare enums with arguments. Use either `switch`, `match` or `Type.enumEq`" e.epos)
+			error "You cannot directly compare enums with arguments. Use either `switch`, `match` or `Type.enumEq`" e.epos
+		end
 	| _ ->
 		e)
 

+ 14 - 0
tests/unit/src/unit/issues/Issue9200.hx

@@ -0,0 +1,14 @@
+package unit.issues;
+
+private enum Foo {
+	Bar(_:Int);
+}
+
+class Issue9200 extends unit.Test {
+	public function test () {
+		switch Bar(4) {
+			case null:
+			case Bar(s): eq(4, s);
+		}
+	}
+}