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

[typer] bind monos instead of constraining them

see #9640
closes #10198
closes #10228
closes #10229
Simon Krajewski 4 жил өмнө
parent
commit
77a11c7dac

+ 2 - 2
src/core/tUnification.ml

@@ -175,9 +175,9 @@ module Monomorph = struct
 			m.tm_type <- Some t;
 			m.tm_type <- Some t;
 			let monos,kind = classify_constraints' m in
 			let monos,kind = classify_constraints' m in
 			Std.finally (fun () -> m.tm_type <- None) (fun () -> check_constraints kind t) ();
 			Std.finally (fun () -> m.tm_type <- None) (fun () -> check_constraints kind t) ();
-			(* If the monomorph we're binding to has other yet unbound monomorphs, constrain them to our target type (issue #9640) .*)
+			(* If the monomorph we're binding to has other yet unbound monomorphs, bind them to our target type (issue #9640) .*)
 			List.iter (fun m2 ->
 			List.iter (fun m2 ->
-				constrain_to_type m2 None t;
+				bind m2 t
 			) monos;
 			) monos;
 			do_bind m t
 			do_bind m t
 		end
 		end

+ 7 - 0
tests/misc/projects/Issue10198/Main.hx

@@ -0,0 +1,7 @@
+class Main {
+	static function main()
+		var b:Int = fn(fn('s'));
+
+	static public function fn<T, R:T>(v:R):T
+		return null;
+}

+ 31 - 0
tests/misc/projects/Issue10198/Main2.hx

@@ -0,0 +1,31 @@
+class Main2 {
+	static function main() {
+		var foo:Vector<Foo> = null;
+		var bar:Vector<Bar> = Vector.fromIterable(foo);
+	}
+}
+
+typedef Foo = {
+	final id:String;
+	final ?project:String;
+}
+
+typedef Bar = {
+	final id:String;
+	final createDate:Date;
+}
+
+abstract Vector<T>(Array<T>) {
+
+	inline function new(a)
+	  this = a;
+
+	@:from static function fromVector<T, R:T>(v:Vector<R>):Vector<T>
+	  return cast v;
+
+	static public function fromIterable<T, R:T>(v:Iterable<R>):Vector<T>
+	  return null;
+
+	@:to public function toArray()
+	  return this.copy();
+}

+ 12 - 0
tests/misc/projects/Issue10198/Main3.hx

@@ -0,0 +1,12 @@
+function fn1<T1,R1:T1>(v:R1):T1
+	return null;
+
+function fn2<T2,R2:T2>(v:R2):T2
+	return null;
+
+function fn3<T3,R3:T3>(v:R3):T3
+	return null;
+
+function main() {
+	var b:Array<Int> = fn1(fn2(fn3(['s'])));
+}

+ 1 - 0
tests/misc/projects/Issue10198/compile-fail.hxml

@@ -0,0 +1 @@
+--main Main

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

@@ -0,0 +1 @@
+Main.hx:3: characters 3-6 : String should be Int

+ 1 - 0
tests/misc/projects/Issue10198/compile2-fail.hxml

@@ -0,0 +1 @@
+--main Main2

+ 3 - 0
tests/misc/projects/Issue10198/compile2-fail.hxml.stderr

@@ -0,0 +1,3 @@
+Main2.hx:4: characters 3-50 : error: { id : String, createDate : Date } has no field project
+Main2.hx:4: characters 3-50 : ... have: Vector<Foo>
+Main2.hx:4: characters 3-50 : ... want: Vector<Bar>

+ 1 - 0
tests/misc/projects/Issue10198/compile3-fail.hxml

@@ -0,0 +1 @@
+--main Main3

+ 3 - 0
tests/misc/projects/Issue10198/compile3-fail.hxml.stderr

@@ -0,0 +1,3 @@
+Main3.hx:11: characters 2-42 : error: String should be Int
+Main3.hx:11: characters 2-42 : ... have: Array<String>
+Main3.hx:11: characters 2-42 : ... want: Array<Int>

+ 25 - 0
tests/misc/projects/Issue10229/Main.hx

@@ -0,0 +1,25 @@
+typedef Foo = {
+	final id:String;
+	final ?project:String;
+}
+
+typedef Bar = {
+	final id:String;
+	final createDate:Date;
+	final ?project:String;
+}
+
+class Main {
+	static function constrained<T,R:T>(v:R):T
+		return null;
+
+	static function fn<T>(s:T):T {
+		return s;
+	}
+
+	static function main() {
+		var A:Int = fn('s'); // Error "String should be Int"
+		var B:Int = constrained('s'); //Error "Int should be String" instead of "String should be Int"
+		var C:Bar = constrained((null:Foo)); // compiles, but should error "Foo should be Bar; { ?project : Null<String>, id : String } has no field createDate"
+	}
+}

+ 1 - 0
tests/misc/projects/Issue10229/compile-fail.hxml

@@ -0,0 +1 @@
+--main Main

+ 4 - 0
tests/misc/projects/Issue10229/compile-fail.hxml.stderr

@@ -0,0 +1,4 @@
+Main.hx:21: characters 3-23 : String should be Int
+Main.hx:22: characters 3-32 : String should be Int
+Main.hx:23: characters 3-39 : Foo should be Bar
+Main.hx:23: characters 3-39 : ... { ?project : Null<String>, id : String } has no field createDate

+ 4 - 3
tests/misc/projects/Issue9640/compile-fail.hxml.stderr

@@ -1,3 +1,4 @@
-Main.hx:5: characters 9-13 : Warning : Mono<Unknown<0> : Foo>
-Main.hx:8: characters 3-36 : BarLike should be Foo
-Main.hx:10: characters 9-13 : Warning : Mono<Unknown<0> : Foo>
+Main.hx:5: characters 9-13 : Warning : Mono<Foo>
+Main.hx:8: characters 3-36 : Foo should be BarLike
+Main.hx:8: characters 3-36 : ... Foo has no field bar
+Main.hx:10: characters 9-13 : Warning : Mono<Foo>