Browse Source

allow abstract type parameter variance if the underlying types are compatible (fixed issue #1726)

Simon Krajewski 12 years ago
parent
commit
e29e834edf
2 changed files with 13 additions and 1 deletions
  1. 5 0
      tests/unit/TestType.hx
  2. 8 1
      type.ml

+ 5 - 0
tests/unit/TestType.hx

@@ -855,4 +855,9 @@ class TestType extends Test {
 		eq(func1(), "foo");
 		eq(func1(), "foo");
 		eq(s.test()(), "bar");
 		eq(s.test()(), "bar");
 	}
 	}
+	
+	function testAbstractTypeParameterVariance() {
+		var a:Array<unit.MyAbstract.MyInt> = [1, 2, 3];
+		var b:Array<unit.MyAbstract.MyInt2> = a;
+	}
 }
 }

+ 8 - 1
type.ml

@@ -1245,7 +1245,14 @@ and unify_types a b tl1 tl2 =
 			type_eq EqRightDynamic t1 t2
 			type_eq EqRightDynamic t1 t2
 		with Unify_error l ->
 		with Unify_error l ->
 			let err = cannot_unify a b in
 			let err = cannot_unify a b in
-			error (try unify t1 t2; (err :: (Invariant_parameter (t1,t2)) :: l) with _ -> err :: l)
+			(try (match follow t1, follow t2 with
+				| TAbstract({a_impl = Some _} as a1,pl1),TAbstract({a_impl = Some _ } as a2,pl2) ->
+					type_eq EqStrict (apply_params a1.a_types pl1 a1.a_this) (apply_params a2.a_types pl2 a2.a_this)
+				| TAbstract({a_impl = Some _} as a,pl),t -> type_eq EqStrict (apply_params a.a_types pl a.a_this) t
+				| t,TAbstract({a_impl = Some _ } as a,pl) -> type_eq EqStrict t (apply_params a.a_types pl a.a_this)
+				| _ -> raise (Unify_error l))
+			with Unify_error _ ->
+				error (err :: (Invariant_parameter (t1,t2)) :: l))
 	) tl1 tl2
 	) tl1 tl2
 
 
 and unify_with_access t1 f2 =
 and unify_with_access t1 f2 =