Browse Source

allow constant anon covariance

Simon Krajewski 13 years ago
parent
commit
1c039a1aee
2 changed files with 19 additions and 0 deletions
  1. 1 0
      tests/unit/TestType.hx
  2. 18 0
      typer.ml

+ 1 - 0
tests/unit/TestType.hx

@@ -275,6 +275,7 @@ class TestType extends Test {
 		var b:Dynamic = "bar";
 		f(typeError(a = { v:0.2 } ));
 		f(typeError(a = { v:0 } ));
+		typedAs(a = { v: 0 }, a);
 		typedAs(a = { v: 0.2 }, a);
 		t(typeError(a = { v: "foo" } ));
 		f(typeError(a = { v: untyped "foo" } ));

+ 18 - 0
typer.ml

@@ -1349,6 +1349,24 @@ and type_expr_with_type ~unify ctx e t =
 					mk (TArrayDecl el) t p)
 			| _ ->
 				type_expr ctx e)
+	| (EObjectDecl el,p) ->
+		(match t with
+		| None -> type_expr ctx e
+		| Some t ->
+			match follow t with
+			| TAnon a ->
+				(try 
+					let el = List.map (fun (n, e) ->
+						let t = (PMap.find n a.a_fields).cf_type in
+						let e = type_expr_with_type ~unify ctx e (Some t) in
+						unify ctx e.etype t e.epos;
+						(n,e)
+					) el in
+					mk (TObjectDecl el) t p
+				with Not_found ->
+					type_expr ctx e)
+			| _ ->
+				type_expr ctx e)
 	| _ ->
 		type_expr ctx e