Browse Source

absorb field monomorphs when cloning ctx.e

closes #11381
Simon Krajewski 7 months ago
parent
commit
f2638f68f9

+ 12 - 0
src/context/typecore.ml

@@ -278,6 +278,18 @@ module TyperManager = struct
 
 
 	let clone_for_expr ctx curfun in_function =
 	let clone_for_expr ctx curfun in_function =
 		let e = create_ctx_e curfun in_function in
 		let e = create_ctx_e curfun in_function in
+		begin match curfun with
+		| FunMember | FunMemberAbstract | FunStatic | FunConstructor ->
+			(* Monomorphs from field arguments and return types are created before
+			   ctx.e is cloned, so they have to be absorbed here. A better fix might
+			   be to clone ctx.e earlier, but that comes with its own challenges. *)
+			e.monomorphs <- ctx.e.monomorphs;
+			ctx.e.monomorphs <- []
+		| FunMemberAbstractLocal | FunMemberClassLocal ->
+			(* We don't need to do this for local functions because the cloning happens
+			   earlier there. *)
+			()
+		end;
 		create ctx ctx.m ctx.c ctx.f e PTypeField ctx.type_params
 		create ctx ctx.m ctx.c ctx.f e PTypeField ctx.type_params
 
 
 	let clone_for_type_params ctx params =
 	let clone_for_type_params ctx params =

+ 0 - 1
src/typing/typeloadFunction.ml

@@ -39,7 +39,6 @@ let type_function_params ctx fd host fname =
 let type_function ctx (args : function_arguments) ret e do_display p =
 let type_function ctx (args : function_arguments) ret e do_display p =
 	ctx.e.ret <- ret;
 	ctx.e.ret <- ret;
 	ctx.e.opened <- [];
 	ctx.e.opened <- [];
-	ctx.e.monomorphs <- [];
 	enter_field_typing_pass ctx.g ("type_function",fst ctx.c.curclass.cl_path @ [snd ctx.c.curclass.cl_path;ctx.f.curfield.cf_name]);
 	enter_field_typing_pass ctx.g ("type_function",fst ctx.c.curclass.cl_path @ [snd ctx.c.curclass.cl_path;ctx.f.curfield.cf_name]);
 	args#bring_into_context ctx;
 	args#bring_into_context ctx;
 	let e = match e with
 	let e = match e with

+ 1 - 1
tests/misc/projects/Issue6790/compile-fail.hxml.stderr

@@ -1,2 +1,2 @@
-Mismatch.hx:6: characters 19-26 : (e : Unknown<0>) -> String should be Null<js.lib.PromiseHandler<Dynamic, Int>>
+Mismatch.hx:6: characters 19-26 : (e : Dynamic) -> String should be Null<js.lib.PromiseHandler<Dynamic, Int>>
 Mismatch.hx:6: characters 19-26 : ... For optional function argument 'onRejected'
 Mismatch.hx:6: characters 19-26 : ... For optional function argument 'onRejected'

+ 1 - 1
tests/misc/projects/Issue6790/pretty-fail.hxml.stderr

@@ -2,6 +2,6 @@
 
 
  6 |   p.then(x -> 10, e -> "");
  6 |   p.then(x -> 10, e -> "");
    |                   ^^^^^^^
    |                   ^^^^^^^
-   | (e : Unknown<0>) -> String should be Null<js.lib.PromiseHandler<Dynamic, Int>>
+   | (e : Dynamic) -> String should be Null<js.lib.PromiseHandler<Dynamic, Int>>
    | For optional function argument 'onRejected'
    | For optional function argument 'onRejected'
 
 

+ 1 - 1
tests/misc/projects/Issue7655/compile-fail.hxml.stderr

@@ -1,2 +1,2 @@
-Mismatch.hx:6: characters 19-26 : (e : Unknown<0>) -> String should be js.lib.PromiseHandler<Dynamic, Int>
+Mismatch.hx:6: characters 19-26 : (e : Dynamic) -> String should be js.lib.PromiseHandler<Dynamic, Int>
 Mismatch.hx:6: characters 19-26 : ... For optional function argument 'onRejected'
 Mismatch.hx:6: characters 19-26 : ... For optional function argument 'onRejected'

+ 15 - 0
tests/unit/src/unit/issues/Issue11381.hx

@@ -0,0 +1,15 @@
+package unit.issues;
+
+import unit.Test;
+
+class Issue11381 extends Test {
+	function test() {
+		var a:Int = func1(1);
+		var s:String = func1("foo");
+		eq("foo", s);
+	}
+
+	function func1(a:Dynamic) {
+		return a;
+	}
+}