浏览代码

disallow catching non-generic type parameters (closes #2907)

Simon Krajewski 11 年之前
父节点
当前提交
adbcb802e7
共有 2 个文件被更改,包括 32 次插入1 次删除
  1. 28 0
      tests/unit/issues/Issue2907.hx
  2. 4 1
      typer.ml

+ 28 - 0
tests/unit/issues/Issue2907.hx

@@ -0,0 +1,28 @@
+package unit.issues;
+
+private class MyTestClass {
+	public function new() { }
+}
+
+class Issue2907 extends Test {
+
+	@:generic function catchGeneric<T>(t:T) {
+		try {
+			throw "foo";
+		} catch(e:T) {
+			return e;
+		} catch(e:Dynamic) {
+			return null;
+		}
+		return null;
+	}
+
+	function test<T>() {
+		t(unit.TestType.typeError(
+			try { }
+			catch(e:T) { }
+		));
+		eq("foo", catchGeneric("foo"));
+		eq(null, catchGeneric(new MyTestClass()));
+	}
+}

+ 4 - 1
typer.ml

@@ -2846,7 +2846,10 @@ and type_expr ctx (e,p) (with_type:with_type) =
 		let catches = List.fold_left (fun acc (v,t,e) ->
 			let t = Typeload.load_complex_type ctx (pos e) t in
 			let rec loop t = match follow t with
-				| TInst ({ cl_path = path },params) | TEnum ({ e_path = path },params) ->
+				| TInst ({ cl_kind = KTypeParameter _} as c,_) when not (Typeload.is_generic_parameter ctx c) ->
+					error "Cannot catch non-generic type parameter" p
+				| TInst ({ cl_path = path },params)
+				| TEnum ({ e_path = path },params) ->
 					List.iter (fun pt ->
 						if pt != t_dynamic then error "Catch class parameter must be Dynamic" p;
 					) params;