2
0
Эх сурвалжийг харах

add implicit null checks to pattern matching (see #2580)

Simon Krajewski 11 жил өмнө
parent
commit
4b08643770

+ 5 - 1
matcher.ml

@@ -1032,7 +1032,11 @@ 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 -> dt
+	| None when is_explicit_null st.st_type ->
+		let econd = mk (TBinop(OpNotEq,e_st,mk (TConst TNull) (mk_mono()) p)) ctx.t.tbool p in
+		DTGuard(econd,dt,!def)
+	| None ->
+		dt
 	| Some dt_null ->
 		let econd = mk (TBinop(OpEq,e_st,mk (TConst TNull) (mk_mono()) p)) ctx.t.tbool p in
 		DTGuard(econd,dt_null,Some dt)

+ 23 - 0
tests/unit/issues/Issue2580.hx

@@ -0,0 +1,23 @@
+package unit.issues;
+
+private enum T {
+    T1(x:Null<T>);
+    T2(x:Int);
+}
+
+class Issue2580 extends Test {
+	function test() {
+		function match(t) {
+			return switch (t) {
+				case T1(T1(_)): 0;
+				case T1(T2(_)): 1;
+				case T1(_): 2;
+				case T2(_): 3;
+			}
+		}
+		eq(0, match(T1(T1(null))));
+		eq(1, match(T1(T2(1))));
+		eq(2, match(T1(null)));
+		eq(3, match(T2(2)));
+	}
+}