Explorar o código

only generate null checks in pattern matching when we do not need a value or there is an else case (closes #3061)

Simon Krajewski %!s(int64=10) %!d(string=hai) anos
pai
achega
9a11c57c62
Modificáronse 2 ficheiros con 34 adicións e 1 borrados
  1. 1 1
      matcher.ml
  2. 33 0
      tests/unit/src/unit/issues/Issue3061.hx

+ 1 - 1
matcher.ml

@@ -1059,7 +1059,7 @@ let convert_switch mctx st cases loop =
 		| _ -> DTSwitch(e, List.map (fun (c,dt) -> convert_con ctx c, loop dt) cases, !def)
 	in
 	match !null with
-	| None when is_explicit_null st.st_type ->
+	| None when is_explicit_null st.st_type && (!def <> None || not mctx.need_val) ->
 		let econd = mk (TBinop(OpNotEq,e_st,mk (TConst TNull) (mk_mono()) p)) ctx.t.tbool p in
 		DTGuard(econd,dt,!def)
 	| None ->

+ 33 - 0
tests/unit/src/unit/issues/Issue3061.hx

@@ -0,0 +1,33 @@
+package unit.issues;
+
+private enum MyEnum1 {
+    A;
+}
+
+private enum MyEnum2 {
+    A;
+	B;
+}
+
+class Issue3061 extends Test {
+	function test() {
+		var e:Null<MyEnum1> = A;
+		var r = foo(
+			switch (e) {
+				case A: 1;
+			}
+		);
+		eq(1, r);
+
+		var e2:Null<MyEnum2> = A;
+		var r = foo(
+			switch (e2) {
+				case A: 1;
+				case B: 2;
+			}
+		);
+		eq(1, r);
+	}
+
+	static function foo( v : Int ) return v;
+}