Browse Source

[display] type constructor args if we can't find the type

closes #7650
Simon Krajewski 6 years ago
parent
commit
ecfb6ec1d1
2 changed files with 34 additions and 5 deletions
  1. 15 5
      src/typing/typer.ml
  2. 19 0
      tests/display/src/cases/Issue7650.hx

+ 15 - 5
src/typing/typer.ml

@@ -1735,9 +1735,14 @@ and type_new ctx path el with_type force_inline p =
 		| _ ->
 			error "Constructor is not a function" p
 	in
-	let t = if (fst path).tparams <> [] then
-		Typeload.load_instance ctx path false
-	else try
+	let t = if (fst path).tparams <> [] then begin
+		try
+			Typeload.load_instance ctx path false
+		with Error _ as exc when ctx.com.display.dms_display ->
+			(* If we fail for some reason, process the arguments in case we want to display them (#7650). *)
+			List.iter (fun e -> ignore(type_expr ctx e WithType.value)) el;
+			raise exc
+	end else try
 		ctx.call_argument_stack <- el :: ctx.call_argument_stack;
 		let t = Typeload.load_instance ctx path true in
 		let t_follow = follow t in
@@ -1747,9 +1752,10 @@ and type_new ctx path el with_type force_inline p =
 			| TInst({cl_kind = KGeneric } as c,tl) -> follow (Generic.build_generic ctx c p tl)
 			| _ -> t
 		end
-	with Generic.Generic_Exception _ ->
+	with
+	| Generic.Generic_Exception _ ->
 		(* Try to infer generic parameters from the argument list (issue #2044) *)
-		match resolve_typedef (Typeload.load_type_def ctx p (fst path)) with
+		begin match resolve_typedef (Typeload.load_type_def ctx p (fst path)) with
 		| TClassDecl ({cl_constructor = Some cf} as c) ->
 			let monos = List.map (fun _ -> mk_mono()) c.cl_params in
 			let ct, f = get_constructor ctx c monos p in
@@ -1773,6 +1779,10 @@ and type_new ctx path el with_type force_inline p =
 			end
 		| mt ->
 			error ((s_type_path (t_infos mt).mt_path) ^ " cannot be constructed") p
+		end
+	| Error _ as exc when ctx.com.display.dms_display ->
+		List.iter (fun e -> ignore(type_expr ctx e WithType.value)) el;
+		raise exc
 	in
 	DisplayEmitter.check_display_type ctx t (pos path);
 	let t = follow t in

+ 19 - 0
tests/display/src/cases/Issue7650.hx

@@ -0,0 +1,19 @@
+package cases;
+
+class Issue7650 extends DisplayTestCase {
+	/**
+		new MyClasss("".{-1-}
+	**/
+	@:funcCode
+	function test1() {
+		eq(true, hasField(fields(pos(1)), "length", "Int"));
+	}
+
+	/**
+		new MyClasss<MyClassss>("".{-1-}
+	**/
+	@:funcCode
+	function test2() {
+		eq(true, hasField(fields(pos(1)), "length", "Int"));
+	}
+}