瀏覽代碼

fix exhaustiveness check when there are guards and we need a value (closes #2914)

Simon Krajewski 11 年之前
父節點
當前提交
7157322d23
共有 3 個文件被更改,包括 36 次插入3 次删除
  1. 10 2
      matcher.ml
  2. 1 1
      tests/unit/TestMatch.hx
  3. 25 0
      tests/unit/issues/Issue2914.hx

+ 10 - 2
matcher.ml

@@ -898,8 +898,16 @@ let rec compile mctx stl pmat toplevel =
 			out.o_num_paths <- out.o_num_paths + 1;
 			out.o_num_paths <- out.o_num_paths + 1;
 			let bl = bind_remaining out pv stl in
 			let bl = bind_remaining out pv stl in
 			let dt = match (get_guard mctx out.o_id) with
 			let dt = match (get_guard mctx out.o_id) with
-				| None -> expr out.o_id
-				| Some _ -> guard out.o_id (expr out.o_id) (match pl with [] -> None | _ -> Some (compile mctx stl pl false))
+				| None ->
+					expr out.o_id
+				| Some _ ->
+					let dt = match pl,mctx.need_val with
+						| [],false ->
+							None
+						| _ ->
+							Some (compile mctx stl pl false)
+					in
+					guard out.o_id (expr out.o_id) dt
 			in
 			in
 			(if bl = [] then dt else bind bl dt)
 			(if bl = [] then dt else bind bl dt)
 		end else if i > 0 then begin
 		end else if i > 0 then begin

+ 1 - 1
tests/unit/TestMatch.hx

@@ -341,7 +341,7 @@ class TestMatch extends Test {
 			case Node(Leaf("foo"), _):
 			case Node(Leaf("foo"), _):
 			case Leaf(_):
 			case Leaf(_):
 		}));
 		}));
-		eq("Unmatched patterns: Leaf", TestMatchMacro.getErrorMessage(switch(Leaf("foo")) {
+		eq("Unmatched patterns: Leaf(_)", TestMatchMacro.getErrorMessage(switch(Leaf("foo")) {
 			case Node(_, _):
 			case Node(_, _):
 			case Leaf(_) if (false):
 			case Leaf(_) if (false):
 		}));
 		}));

+ 25 - 0
tests/unit/issues/Issue2914.hx

@@ -0,0 +1,25 @@
+package unit.issues;
+
+class Issue2914 extends Test {
+	function test() {
+		var o:Null<Int> = 0;
+
+		t(unit.TestType.typeError({
+			var x = switch o {
+				case x if (x > 1): x;
+			}
+		}));
+
+		var x = switch o {
+			case x if (x > 1): x;
+			case x: 1;
+		}
+		eq(1, x);
+
+		switch (o) {
+			case y if (y < 1):
+				x = 5;
+		}
+		eq(5, x);
+	}
+}