2
0
Эх сурвалжийг харах

[display] don't actually apply implicit casts when displaying

closes #7061
Simon Krajewski 7 жил өмнө
parent
commit
1ab20baecf

+ 16 - 4
src/typing/typerDisplay.ml

@@ -293,11 +293,23 @@ let handle_display ctx e_ast dk with_type =
 		) l in
 		raise_fields l CRNew p b
 	in
+	let is_display_debug = Meta.has (Meta.Custom ":debug.display") ctx.curfield.cf_meta in
+	if is_display_debug then begin
+		print_endline (Printf.sprintf "expected type: %s" (s_with_type with_type));
+		print_endline (Printf.sprintf "typed expr:\n%s" (s_expr_ast true "" (s_type (print_context())) e));
+	end;
 	let p = e.epos in
-	let e = match with_type with
-		| WithType t -> (try AbstractCast.cast_or_unify_raise ctx t e e.epos with Error (Unify l,p) -> e)
-		| _ -> e
-	in
+	begin match with_type with
+		| WithType t ->
+			(* We don't want to actually use the transformed expression which may have inserted implicit cast calls.
+			   It only matters that unification takes place. *)
+			(try ignore(AbstractCast.cast_or_unify_raise ctx t e e.epos) with Error (Unify l,p) -> ());
+		| _ ->
+			()
+	end;
+	if is_display_debug then begin
+		print_endline (Printf.sprintf "cast expr:\n%s" (s_expr_ast true "" (s_type (print_context())) e));
+	end;
 	ctx.in_display <- fst old;
 	ctx.in_call_args <- snd old;
 	display_expr ctx e_ast e dk with_type p

+ 2 - 1
tests/display/.vscode/settings.json

@@ -1,5 +1,6 @@
 {
 	"haxe.displayConfigurations": [
 		{"label": "IssueTemplate", "args": ["build.hxml", "-D", "test=IssueTemplate"]}
-	]
+	],
+	"haxe.enableCompilationServer": false
 }

+ 31 - 0
tests/display/src/cases/Issue7061.hx

@@ -0,0 +1,31 @@
+package cases;
+
+class Issue7061 extends DisplayTestCase {
+	/**
+	abstract Either<A, B>(EitherImpl<A, B>) {
+		function new(value) this = value;
+		@:from static function fromA<A,B>(value:A) return new Either(a(value));
+		@:from static function fromB<A,B>(value:B) return new Either(b(value));
+	}
+
+	enum EitherImpl<A, B> {
+		a(v:A);
+		b(v:B);
+	}
+
+	class Main {
+		static function main() {}
+		function new() f{-6-}oo(b{-1-}ar);
+		function notNew() foo(b{-7-}ar2);
+		function {-2-}foo{-3-}<T>(value:Either<Void->T,Void->Void>) {}
+		function {-4-}bar{-5-}() {}
+		function bar2() return 1;
+	}
+	**/
+	function test() {
+		eq(range(4, 5), position(pos(1)));
+		eq(range(2, 3), position(pos(6)));
+		eq("Void -> Void", type(pos(1)));
+		eq("Void -> Int", type(pos(7)));
+	}
+}