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

don't allow to remove nullability of function parameter or return value (fixed issue #794)

Nicolas Cannasse 13 жил өмнө
parent
commit
83ba2a8217

+ 1 - 1
std/cpp/_std/IntHash.hx

@@ -49,7 +49,7 @@
 
 
 	public function keys() : Iterator<Int> {
 	public function keys() : Iterator<Int> {
 		var a:Array<Int> = untyped __global__.__int_hash_keys(h);
 		var a:Array<Int> = untyped __global__.__int_hash_keys(h);
-		return a.iterator();
+		return cast a.iterator();
 	}
 	}
 
 
 	public function iterator() : Iterator<T> {
 	public function iterator() : Iterator<T> {

+ 1 - 1
std/flash8/_std/IntHash.hx

@@ -53,7 +53,7 @@
 		var l : Array<Int> = untyped __keys__(h);
 		var l : Array<Int> = untyped __keys__(h);
 		for( x in 0...l.length )
 		for( x in 0...l.length )
 			l[x] = Std.int(l[x]);
 			l[x] = Std.int(l[x]);
-		return l.iterator();
+		return cast l.iterator();
 	}
 	}
 
 
 	public function iterator() : Iterator<T> {
 	public function iterator() : Iterator<T> {

+ 1 - 1
std/js/_std/Hash.hx

@@ -58,7 +58,7 @@
 					a.push(key.substr(1));
 					a.push(key.substr(1));
 			__js__("}");
 			__js__("}");
 		}
 		}
-		return a.iterator();
+		return cast a.iterator();
 	}
 	}
 
 
 	public function iterator() : Iterator<T> {
 	public function iterator() : Iterator<T> {

+ 1 - 1
std/js/_std/IntHash.hx

@@ -57,7 +57,7 @@
 					a.push(key|0);
 					a.push(key|0);
 			__js__("}");
 			__js__("}");
 		}
 		}
-		return a.iterator();
+		return cast a.iterator();
 	}
 	}
 
 
 	public function iterator() : Iterator<T> {
 	public function iterator() : Iterator<T> {

+ 20 - 0
tests/unit/TestMisc.hx

@@ -179,6 +179,10 @@ class TestMisc extends Test {
 	function opt3( ?x : Null<Int> = 5, ?y : Null<Float> = 6 ) {
 	function opt3( ?x : Null<Int> = 5, ?y : Null<Float> = 6 ) {
 		return { x : x, y : y };
 		return { x : x, y : y };
 	}
 	}
+	
+	function opt4( x = 10 ) : Null<Int> {
+		return x + 1;
+	}
 
 
 	function testOptionalParams() {
 	function testOptionalParams() {
 		eq( opt1().x, null );
 		eq( opt1().x, null );
@@ -214,6 +218,22 @@ class TestMisc extends Test {
 		// skipping
 		// skipping
 		eq( opt3(7.4).x, 5 );
 		eq( opt3(7.4).x, 5 );
 		eq( opt3(7.4).y, 7.4 );
 		eq( opt3(7.4).y, 7.4 );
+		
+		eq( opt4(), 11 );
+		#if !(flash9 || cpp || cs || java)
+		eq( opt4(null), 11 );
+		#end
+		
+		var opt4b : ?Int -> Null<Int> = opt4;
+		eq( opt4b(), 11 );
+		eq( opt4b(3), 4 );
+		#if !(flash9 || cpp || cs || java)
+		eq( opt4b(null), 11 );
+		#end
+
+		// don't compile because we restrict nullability of function param or return type
+		// var opt4c : ?Null<Int> -> Null<Int> = opt4;
+		// var opt4c : ?Int -> Int = opt4;
 	}
 	}
 
 
 	function testIncr() {
 	function testIncr() {

+ 2 - 0
type.ml

@@ -858,9 +858,11 @@ let rec unify a b =
 		if not (loop c1 tl1) then error [cannot_unify a b]
 		if not (loop c1 tl1) then error [cannot_unify a b]
 	| TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
 	| TFun (l1,r1) , TFun (l2,r2) when List.length l1 = List.length l2 ->
 		(try
 		(try
+			if not (is_null r2) && is_null r1 then (match follow r2 with TMono _ | TDynamic _ -> () | _ -> error [cannot_unify r2 r1]);
 			unify r1 r2;
 			unify r1 r2;
 			List.iter2 (fun (_,o1,t1) (_,o2,t2) ->
 			List.iter2 (fun (_,o1,t1) (_,o2,t2) ->
 				if o1 && not o2 then error [Cant_force_optional];
 				if o1 && not o2 then error [Cant_force_optional];
+				if not (is_null t2) && is_null t1 then (match follow t2 with TMono _ | TDynamic _ -> () | _ -> error [cannot_unify t1 t2]);
 				unify t1 t2
 				unify t1 t2
 			) l2 l1 (* contravariance *)
 			) l2 l1 (* contravariance *)
 		with
 		with