Browse Source

[matcher] add list reversals in random places

closes #9938
Simon Krajewski 4 years ago
parent
commit
23e2e2c52c
2 changed files with 30 additions and 11 deletions
  1. 17 11
      src/typing/matcher.ml
  2. 13 0
      tests/unit/src/unit/issues/Issue9938.hx

+ 17 - 11
src/typing/matcher.ml

@@ -1124,7 +1124,7 @@ module Compile = struct
 				error "Invalid match: Too many patterns" p
 				error "Invalid match: Too many patterns" p
 		in
 		in
 		let bindings = loop patterns subjects bindings in
 		let bindings = loop patterns subjects bindings in
-		if bindings = [] then dt else bind mctx bindings dt
+		if bindings = [] then dt else bind mctx (List.rev bindings) dt
 
 
 	and compile_switch mctx subjects cases =
 	and compile_switch mctx subjects cases =
 		let subject,subjects = match subjects with
 		let subject,subjects = match subjects with
@@ -1237,14 +1237,21 @@ module Compile = struct
 			match_pos = p;
 			match_pos = p;
 			dt_count = 0;
 			dt_count = 0;
 		} in
 		} in
-		let subjects,vars = List.fold_left (fun (subjects,vars) e -> match e.eexpr with
-			| TConst _ | TLocal _ ->
-				(e :: subjects,vars)
-			| _ ->
-				let v = gen_local ctx e.etype e.epos in
-				let ev = mk (TLocal v) e.etype e.epos in
-				(ev :: subjects,(v,e.epos,e) :: vars)
-		) ([],[]) subjects in
+		let rec loop (subjects,vars) el = match el with
+			| [] ->
+				List.rev subjects,List.rev vars
+			| e :: el ->
+				let subjects,vars = match e.eexpr with
+				| TConst _ | TLocal _ ->
+					(e :: subjects,vars)
+				| _ ->
+					let v = gen_local ctx e.etype e.epos in
+					let ev = mk (TLocal v) e.etype e.epos in
+					(ev :: subjects,(v,e.epos,e) :: vars)
+				in
+				loop (subjects,vars) el
+		in
+		let subjects,vars = loop ([],[]) subjects in
 		begin match cases,subjects with
 		begin match cases,subjects with
 		| [],(subject :: _) ->
 		| [],(subject :: _) ->
 			let dt_fail = fail mctx subject.epos in
 			let dt_fail = fail mctx subject.epos in
@@ -1574,7 +1581,7 @@ module TexprConverter = struct
 						in
 						in
 						f()
 						f()
 					| Bind(bl,dt) ->
 					| Bind(bl,dt) ->
-						let el = List.rev_map (fun (v,p,e) ->
+						let el = List.map (fun (v,p,e) ->
 							v_lookup := IntMap.add v.v_id e !v_lookup;
 							v_lookup := IntMap.add v.v_id e !v_lookup;
 							mk (TVar(v,Some e)) com.basic.tvoid p
 							mk (TVar(v,Some e)) com.basic.tvoid p
 						) bl in
 						) bl in
@@ -1608,7 +1615,6 @@ module Match = struct
 				e.etype,[e]
 				e.etype,[e]
 		in
 		in
 		let t,subjects = loop e in
 		let t,subjects = loop e in
-		let subjects = List.rev subjects in
 		let cases = match def with
 		let cases = match def with
 			| None -> cases
 			| None -> cases
 			| Some (eo,p) -> cases @ [[EConst (Ident "_"),p],None,eo,p]
 			| Some (eo,p) -> cases @ [[EConst (Ident "_"),p],None,eo,p]

+ 13 - 0
tests/unit/src/unit/issues/Issue9938.hx

@@ -0,0 +1,13 @@
+package unit.issues;
+
+class Issue9938 extends unit.Test {
+	function test() {
+		var arr = [0, 1, 2];
+		switch [arr.shift(), arr.shift(), arr.shift()] {
+			case [a, b, c]:
+				eq(0, a);
+				eq(1, b);
+				eq(2, c);
+		}
+	}
+}