Bladeren bron

support top-down inference for enum constructors when typed against abstracts that have a single `from` (closes #3528)

Simon Krajewski 9 jaren geleden
bovenliggende
commit
4c7e6ff6b2
2 gewijzigde bestanden met toevoegingen van 28 en 0 verwijderingen
  1. 23 0
      tests/unit/src/unit/issues/Issue3528.hx
  2. 5 0
      typer.ml

+ 23 - 0
tests/unit/src/unit/issues/Issue3528.hx

@@ -0,0 +1,23 @@
+package unit.issues;
+
+private abstract Optional<T>(haxe.ds.Option<T>) from haxe.ds.Option<T> {}
+
+class Issue3528 extends Test {
+    function test() {
+		var x = test1(Some(4));
+
+		test2(Some(4));
+		switch test2(x) {
+			case Some(v):
+			case None:
+		}
+    }
+
+	static function test1(v:haxe.ds.Option<Int>) {
+		return v;
+	}
+
+	static function test2(v:Optional<Int>) {
+		return v;
+	}
+}

+ 5 - 0
typer.ml

@@ -4027,6 +4027,11 @@ and maybe_type_against_enum ctx f with_type p =
 					en.e_path,en.e_names,TEnumDecl en
 				| TAbstract ({a_impl = Some c} as a,_) when has_meta Meta.Enum a.a_meta ->
 					a.a_path,List.map (fun cf -> cf.cf_name) c.cl_ordered_fields,TAbstractDecl a
+				| TAbstract (a,pl) when not (Meta.has Meta.CoreType a.a_meta) ->
+					begin match get_abstract_froms a pl with
+						| [t] -> loop t
+						| _ -> raise Exit
+					end
 				| _ ->
 					raise Exit
 			in