Browse Source

if all else fails, use expected type (closes #3804)

Simon Krajewski 10 years ago
parent
commit
1d41525228
2 changed files with 26 additions and 1 deletions
  1. 12 0
      tests/unit/src/unit/issues/Issue3804.hx
  2. 14 1
      typer.ml

+ 12 - 0
tests/unit/src/unit/issues/Issue3804.hx

@@ -0,0 +1,12 @@
+package unit.issues;
+
+@:generic
+private class C<T> {
+	public function new() {}
+}
+
+class Issue3804 extends Test {
+	function test() {
+		var v:C<Int> = new C();
+	}
+}

+ 14 - 1
typer.ml

@@ -3337,7 +3337,20 @@ and type_expr ctx (e,p) (with_type:with_type) =
 				let monos = List.map (fun _ -> mk_mono()) c.cl_params in
 				let monos = List.map (fun _ -> mk_mono()) c.cl_params in
 				let ct, f = get_constructor ctx c monos p in
 				let ct, f = get_constructor ctx c monos p in
 				ignore (unify_constructor_call c monos f ct);
 				ignore (unify_constructor_call c monos f ct);
-				Codegen.build_generic ctx c p monos
+				begin try
+					Codegen.build_generic ctx c p monos
+				with Codegen.Generic_Exception _ as exc ->
+					(* If we have an expected type, just use that (issue #3804) *)
+					begin match with_type with
+						| WithType t | WithTypeResume t ->
+							begin match follow t with
+								| TMono _ -> raise exc
+								| t -> t
+							end
+						| _ ->
+							raise exc
+					end
+				end
 			| mt ->
 			| mt ->
 				error ((s_type_path (t_infos mt).mt_path) ^ " cannot be constructed") p
 				error ((s_type_path (t_infos mt).mt_path) ^ " cannot be constructed") p
 		in
 		in