Răsfoiți Sursa

handle CFields after extracting null patterns (closes #1950)

Simon Krajewski 12 ani în urmă
părinte
comite
4ddb49a213
2 a modificat fișierele cu 18 adăugiri și 3 ștergeri
  1. 6 3
      matcher.ml
  2. 12 0
      tests/unit/TestMatch.hx

+ 6 - 3
matcher.ml

@@ -845,8 +845,8 @@ let rec compile mctx stl pmat =
 			) sigma in
 			let def = default mctx pmat in
 			let dt = match def,cases with
- 			| _,[{c_def = CFields _},dt] ->
-				dt
+			| _ when List.exists (fun (c,_) -> match c.c_def with CFields _ -> true | _ -> false) cases ->
+				switch st_head cases
 			| _ when not inf && PMap.is_empty !all ->
 				switch st_head cases
 			| [],_ when inf && not mctx.need_val ->
@@ -934,7 +934,10 @@ let convert_switch ctx st cases loop =
 		| _ ->
 			true
 	) cases in
-	let dt = DTSwitch(e, List.map (fun (c,dt) -> convert_con ctx c, loop dt) cases) in
+	let dt = match cases with
+		| [{c_def = CFields _},dt] -> loop dt
+		| _ -> DTSwitch(e, List.map (fun (c,dt) -> convert_con ctx c, loop dt) cases)
+	in
 	match !null with
 	| None -> dt
 	| Some dt_null ->

+ 12 - 0
tests/unit/TestMatch.hx

@@ -414,6 +414,18 @@ class TestMatch extends Test {
 			case Node(_): "default";
 		}
 		eq(f(t), "foo");
+		
+		function f(a) {
+			return switch(a:{a: Int}) {
+				case {a: 1}: 1;
+				case null: 2;
+				default: 3;
+			}
+		}
+		
+		eq(f(null), 2);
+		eq(f({a: 1}), 1);
+		eq(f({a: 2}), 3);
 	}
 
 	#if false