Prechádzať zdrojové kódy

unify return type of closures with expected type (closes #2622)

Simon Krajewski 11 rokov pred
rodič
commit
8204f5ff4b
2 zmenil súbory, kde vykonal 32 pridanie a 1 odobranie
  1. 26 0
      tests/unit/issues/Issue2622.hx
  2. 6 1
      typer.ml

+ 26 - 0
tests/unit/issues/Issue2622.hx

@@ -0,0 +1,26 @@
+package unit.issues;
+import unit.Test;
+
+private abstract A(Int) from Int to Int {
+	@:to public function toString():String {
+		return Std.string(this);
+	}
+}
+
+class Issue2622 extends Test {
+	function test() {
+		var a:A = 2;
+		var b = foo(a, function(v) return v);
+		eq("2", b);
+		
+		bar(function() return 1); // allow assigning any return type to Void
+	}
+	
+	static function foo(v:A, test:A->String) {
+		return test(v);
+	}
+	
+	static function bar(test:Void->Void) {
+		
+	}
+}

+ 6 - 1
typer.ml

@@ -2906,12 +2906,17 @@ and type_expr ctx (e,p) (with_type:with_type) =
 		| WithType t | WithTypeResume t ->
 			let rec loop t =
 				(match follow t with
-				| TFun (args2,_) when List.length args2 = List.length args ->
+				| TFun (args2,tr) when List.length args2 = List.length args ->
 					List.iter2 (fun (_,_,t1) (_,_,t2) ->
 						match follow t1 with
 						| TMono _ -> unify ctx t2 t1 p
 						| _ -> ()
 					) args args2;
+					(* unify for top-down inference unless we are expecting Void *)
+					begin match follow tr with
+						| TAbstract({a_path = [],"Void"},_) -> ()
+						| _ -> unify ctx rt tr p
+					end
 				| TAbstract(a,tl) ->
 					loop (Codegen.Abstract.get_underlying_type a tl)
 				| _ -> ())