فهرست منبع

change extractor syntax (closes #2676)

Simon Krajewski 11 سال پیش
والد
کامیت
2ca2d11006
3فایلهای تغییر یافته به همراه55 افزوده شده و 16 حذف شده
  1. 4 4
      matcher.ml
  2. 12 12
      tests/unit/TestMatch.hx
  3. 39 0
      tests/unit/issues/Issue2676.hx

+ 4 - 4
matcher.ml

@@ -1017,12 +1017,12 @@ let transform_extractors mctx stl cases =
 				| EBinop(OpArrow,_,_) when !in_or ->
 					error "Extractors in or patterns are not allowed" (pos e)
 				| EBinop(OpArrow, e1, e2) ->
-					let p = pos e in
 					let ec = EConst (Ident ("__ex" ^ string_of_int (!exc))),snd e in
-					let ecall = match fst e1 with
-						| ECall((EField((EConst(Ident "_"),_),s),_), el) -> ECall((EField(ec,s),p),el),p
-						| _ -> ECall(e1,[ec]),p
+					let rec map_left e = match fst e with
+						| EConst(Ident "_") -> ec
+						| _ -> Ast.map_expr map_left e
 					in
+					let ecall = map_left e1 in
 					ex := (ecall,e2) :: !ex;
 					incr exc;
 					ec

+ 12 - 12
tests/unit/TestMatch.hx

@@ -462,7 +462,7 @@ class TestMatch extends Test {
 		function f(i) {
 			return switch(i) {
 				case 1,2,3: 1;
-				case even => true: 2;
+				case _.even() => true: 2;
 				case 4: throw "unreachable";
 				case _: 3;
 			}
@@ -484,9 +484,9 @@ class TestMatch extends Test {
 		
 		function f(t:MiniType) {
 			return switch (t) {
-				case MTString(deref => "Foo", []): "Foo";
-				case MTString(deref => "Bar" | "Baz", _): "BarBaz";
-				case MTInt(deref => i, []): 'Int:$i';
+				case MTString(_.deref() => "Foo", []): "Foo";
+				case MTString(_.deref() => "Bar" | "Baz", _): "BarBaz";
+				case MTInt(_.deref() => i, []): 'Int:$i';
 				case MTString(_): "OtherString";
 				case _: "Other";
 			}
@@ -503,7 +503,7 @@ class TestMatch extends Test {
 		function g(i : Array<Int>) {
 			return switch(i) {
 				case [x]: 1;
-				case isPair => Some(p) : p.a+p.b;
+				case isPair(_) => Some(p) : p.a+p.b;
 				case arr: 3;
 			}
 		}
@@ -520,7 +520,7 @@ class TestMatch extends Test {
 		var i = 9;
 		var r = switch(i) {
 			case 1: 1;
-			case anon.odd => true: 2;
+			case anon.odd(_) => true: 2;
 			case 9: 3;
 			case _: 4;
 		}
@@ -531,8 +531,8 @@ class TestMatch extends Test {
 		function check(i) {
 			return switch(i) {
 				case 1: 1;
-				case mul.bind(4) => 8: 2;
-				case mul.bind(5) => 15: 3;
+				case mul(_, 4) => 8: 2;
+				case mul(_, 5) => 15: 3;
 				case _: 4;
 			}
 		}
@@ -556,10 +556,10 @@ class TestMatch extends Test {
 		function h(i : Array<Int>) {
 			return switch(i) {
 				case [x]: 1;
-				case isPair => Some({ a : a, b : b }) if (a < 0): 42;
-				case isPair => Some({ a : is(even) => Some(a), b : b }) : a+b;
-				case isPair => Some({ a : isNot(even) => Some(a), b : b }) : a*b;
-				case testArgs.bind(1, "foo") => "[99,98,97]": 99;
+				case isPair(_) => Some({ a : a, b : b }) if (a < 0): 42;
+				case isPair(_) => Some({ a : is(even)(_) => Some(a), b : b }) : a+b;
+				case isPair(_) => Some({ a : isNot(even)(_) => Some(a), b : b }) : a*b;
+				case testArgs(1, "foo", _) => "[99,98,97]": 99;
 				case arr: 3;
 			}
 		}

+ 39 - 0
tests/unit/issues/Issue2676.hx

@@ -0,0 +1,39 @@
+package unit.issues;
+import unit.Test;
+
+
+class Issue2676 extends Test {
+	function test() {
+		function match1(s:String) {
+			return switch(s) {
+				case "foo": 1;
+				case _.toUpperCase() => "BAR": 2;
+				case _ + "," + _ => "abc,abc": 3;
+				case _: 4;
+			}
+		}
+		
+		eq(1, match1("foo"));
+		eq(4, match1("foo2"));
+		eq(2, match1("bAr"));
+		eq(2, match1("BAR"));
+		eq(2, match1("bar"));
+		eq(4, match1("barz"));
+		eq(3, match1("abc"));
+		eq(4, match1("ab"));
+		
+		// check side effect handling
+		function func(i1:Int, i2:Int, i3:Int) {
+			return '$i1;$i2;$i3';
+		}
+		
+		var i = 0;
+		
+		var s = switch(9) {
+			case func(i++, i++, i++) => "0;1;2": "ok";
+			case _: "not ok";
+		}
+		
+		eq("ok", s);
+	}
+}