Browse Source

disallow extractors in or-patterns (closes #2625)

Simon Krajewski 11 years ago
parent
commit
8d79993699
2 changed files with 40 additions and 0 deletions
  1. 10 0
      matcher.ml
  2. 30 0
      tests/unit/issues/Issue2625.hx

+ 10 - 0
matcher.ml

@@ -1012,7 +1012,10 @@ let transform_extractors mctx stl cases =
 		| (epat,eg,e) :: cl ->
 		| (epat,eg,e) :: cl ->
 			let ex = ref [] in
 			let ex = ref [] in
 			let exc = ref 0 in
 			let exc = ref 0 in
+			let in_or = ref false in
 			let rec find_ex e = match fst e with
 			let rec find_ex e = match fst e with
+				| EBinop(OpArrow,_,_) when !in_or ->
+					error "Extractors in or patterns are not allowed" (pos e)
 				| EBinop(OpArrow, e1, e2) ->
 				| EBinop(OpArrow, e1, e2) ->
 					let p = pos e in
 					let p = pos e in
 					let ec = EConst (Ident ("__ex" ^ string_of_int (!exc))),snd e in
 					let ec = EConst (Ident ("__ex" ^ string_of_int (!exc))),snd e in
@@ -1023,6 +1026,13 @@ let transform_extractors mctx stl cases =
 					ex := (ecall,e2) :: !ex;
 					ex := (ecall,e2) :: !ex;
 					incr exc;
 					incr exc;
 					ec
 					ec
+				| EBinop(OpOr,e1,e2) ->
+					let old = !in_or in
+					in_or := true;
+					let e1 = find_ex e1 in
+					let e2 = find_ex e2 in
+					in_or := old;
+					(EBinop(OpOr,e1,e2)),(pos e)
 				| _ ->
 				| _ ->
 					Ast.map_expr find_ex e
 					Ast.map_expr find_ex e
 			in
 			in

+ 30 - 0
tests/unit/issues/Issue2625.hx

@@ -0,0 +1,30 @@
+package unit.issues;
+import unit.Test;
+
+using unit.issues.Issue2625;
+
+class Issue2625 extends Test {
+	function test() {
+		t(unit.TestType.typeError(
+			switch(1) {
+				case (_.equals(2) => true) | (_.equals(3) => false):
+			}
+		));
+		
+		t(unit.TestType.typeError(
+			switch(1) {
+				case (_.equals(2) => true), (_.equals(3) => false):
+			}
+		));
+		
+		var s = switch(1) {
+			case _.equals(1) => true: "1";
+			case _: "2";
+		}
+		eq("1", s);
+	}
+	
+	static function equals(i1:Int, i2:Int) {
+		return i1 == i2;
+	}
+}