Browse Source

only unify function call's return type with expected enum when it's an actual enum, not enum abstract (closes #6656)

still this code gives me headaches...
Daniil Korostelev 7 years ago
parent
commit
f102c8a349
2 changed files with 22 additions and 4 deletions
  1. 4 4
      src/typing/typer.ml
  2. 18 0
      tests/unit/src/unit/issues/Issue6656.hx

+ 4 - 4
src/typing/typer.ml

@@ -3810,12 +3810,12 @@ and maybe_type_against_enum ctx f with_type iscall p =
 		| WithType t ->
 			let rec loop stack t = match follow t with
 				| TEnum (en,_) ->
-					en.e_path,en.e_names,TEnumDecl en
+					true,en.e_path,en.e_names,TEnumDecl en
 				| TAbstract ({a_impl = Some c} as a,_) when has_meta Meta.Enum a.a_meta ->
 					let fields = ExtList.List.filter_map (fun cf ->
 						if Meta.has Meta.Enum cf.cf_meta then Some cf.cf_name else None
 					) c.cl_ordered_statics in
-					a.a_path,fields,TAbstractDecl a
+					false,a.a_path,fields,TAbstractDecl a
 				| TAbstract (a,pl) when not (Meta.has Meta.CoreType a.a_meta) ->
 					begin match get_abstract_froms a pl with
 						| [t2] ->
@@ -3826,7 +3826,7 @@ and maybe_type_against_enum ctx f with_type iscall p =
 				| _ ->
 					raise Exit
 			in
-			let path,fields,mt = loop [] t in
+			let is_enum,path,fields,mt = loop [] t in
 			let old = ctx.m.curmod.m_types in
 			let restore () = ctx.m.curmod.m_types <- old in
 			ctx.m.curmod.m_types <- ctx.m.curmod.m_types @ [mt];
@@ -3845,7 +3845,7 @@ and maybe_type_against_enum ctx f with_type iscall p =
 			begin match e with
 				| AKExpr e ->
 					begin match follow e.etype with
-						| TFun(_,t') ->
+						| TFun(_,t') when is_enum ->
 							unify ctx t' t e.epos;
 							AKExpr e
 						| _ ->

+ 18 - 0
tests/unit/src/unit/issues/Issue6656.hx

@@ -0,0 +1,18 @@
+package unit.issues;
+
+@:enum // comment to make it work
+private abstract TextFormatAlign(String) {
+	var Center = "center";
+	@:from static function fromString(value:String):TextFormatAlign {
+		return cast value;
+	}
+}
+
+class Issue6656 extends unit.Test {
+	function test() {
+		function getAlign() return "center";
+		var a = (getAlign() : TextFormatAlign); // String should be TextFormatAlign
+		eq(a, Center);
+	}
+}
+