Prechádzať zdrojové kódy

use underlying structure type as with_type for constant structure initialization (closes #2933)

Simon Krajewski 11 rokov pred
rodič
commit
bf27fd98ea
2 zmenil súbory, kde vykonal 24 pridanie a 1 odobranie
  1. 17 0
      tests/unit/issues/Issue2933.hx
  2. 7 1
      typer.ml

+ 17 - 0
tests/unit/issues/Issue2933.hx

@@ -0,0 +1,17 @@
+package unit.issues;
+
+private abstract A(Int) from Int to Int {}
+
+private abstract B({a:A}) from {a:A} {
+	public function getA() {
+		return this.a;
+	}
+}
+
+class Issue2933 extends Test {
+	function test() {
+		var a:A = 1;
+		var b:B = {a: 1};
+		eq(1, b.getA());
+	}
+}

+ 7 - 1
typer.ml

@@ -2542,6 +2542,11 @@ and type_expr ctx (e,p) (with_type:with_type) =
 		| WithType t | WithTypeResume t ->
 			(match follow t with
 			| TAnon a when not (PMap.is_empty a.a_fields) -> Some a
+			| TAbstract (a,tl) when not (Meta.has Meta.CoreType a.a_meta) ->
+				begin match Codegen.Abstract.get_underlying_type a tl with
+					| TAnon a -> Some a
+					| _ -> None
+				end
 			| _ -> None)
 		| _ -> None
 		) in
@@ -2570,7 +2575,8 @@ and type_expr ctx (e,p) (with_type:with_type) =
 				if PMap.mem n !fields then error ("Duplicate field in object declaration : " ^ n) p;
 				let e = try
 					let t = (PMap.find n a.a_fields).cf_type in
-					let e = Codegen.Abstract.check_cast ctx t (type_expr ctx e (match with_type with WithTypeResume _ -> WithTypeResume t | _ -> WithType t)) p in
+					let e = type_expr ctx e (match with_type with WithTypeResume _ -> WithTypeResume t | _ -> WithType t) in
+					let e = Codegen.Abstract.check_cast ctx t e p in
 					unify ctx e.etype t e.epos;
 					(try type_eq EqStrict e.etype t; e with Unify_error _ -> mk (TCast (e,None)) t e.epos)
 				with Not_found ->