Selaa lähdekoodia

check constraints when looking for @:from cast fields (closes #2639)

Simon Krajewski 11 vuotta sitten
vanhempi
commit
fd35d84ccf
2 muutettua tiedostoa jossa 31 lisäystä ja 0 poistoa
  1. 26 0
      tests/unit/issues/Issue2639.hx
  2. 5 0
      type.ml

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

@@ -0,0 +1,26 @@
+package unit.issues;
+import unit.Test;
+
+typedef Measurable = {
+    public var length(default, null):Int;
+}
+
+private abstract A<T>(Int) {
+    public function new(i) this = i;
+    @:from static public inline function fromT<T:Measurable>(t:T) {
+        return new A(t.length);
+    }
+	
+	public function get() {
+		return this;
+	}
+}
+
+class Issue2639 extends Test {
+	public function test() {
+		var a:A<Int>;
+		t(unit.TestType.typeError(a = 0));
+		var b:A<String> = "foo";
+		eq(3, b.get());
+	}
+}

+ 5 - 0
type.ml

@@ -1233,6 +1233,11 @@ and unify_from_field ab tl a b ?(allow_transitive_cast=true) (t,cfo) =
 				let monos = List.map (fun _ -> mk_mono()) cf.cf_params in
 				let map t = apply_params ab.a_types tl (apply_params cf.cf_params monos t) in
 				unify_func a (map t);
+				List.iter2 (fun m (name,t) -> match follow t with
+					| TInst ({ cl_kind = KTypeParameter constr },_) when constr <> [] ->
+						List.iter (fun tc -> match follow m with TMono _ -> raise (Unify_error []) | _ -> unify m (map tc) ) constr
+					| _ -> ()
+				) monos cf.cf_params;
 				unify (map r) b;
 			| _ -> assert false)
 		| _ ->