Sfoglia il codice sorgente

[typer] unify with expected type when typing against enum (closes #6606)

Simon Krajewski 8 anni fa
parent
commit
f26b2d2040
3 ha cambiato i file con 37 aggiunte e 6 eliminazioni
  1. 12 3
      src/typing/typer.ml
  2. 3 3
      std/neko/vm/Module.hx
  3. 22 0
      tests/unit/src/unit/issues/Issue6606.hx

+ 12 - 3
src/typing/typer.ml

@@ -4091,12 +4091,12 @@ and maybe_type_against_enum ctx f with_type p =
 		| WithType t ->
 			let rec loop stack t = match follow t with
 				| TEnum (en,_) ->
-					en.e_path,en.e_names,TEnumDecl en
+					en.e_path,en.e_names,TEnumDecl en,t
 				| 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
+					a.a_path,fields,TAbstractDecl a,t
 				| TAbstract (a,pl) when not (Meta.has Meta.CoreType a.a_meta) ->
 					begin match get_abstract_froms a pl with
 						| [t2] ->
@@ -4110,7 +4110,7 @@ and maybe_type_against_enum ctx f with_type p =
 				| _ ->
 					raise Exit
 			in
-			let path,fields,mt = loop [] t in
+			let path,fields,mt,t = 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];
@@ -4126,6 +4126,15 @@ and maybe_type_against_enum ctx f with_type p =
 				raise exc;
 			in
 			restore();
+			begin match e with
+				| AKExpr e ->
+					let rec loop t' = match follow t' with
+						| TFun(_,t) -> loop t
+						| _ -> unify ctx t' t e.epos
+					in
+					loop e.etype
+				| _ -> () (* ??? *)
+			end;
 			e
 		| _ ->
 			raise Exit

+ 3 - 3
std/neko/vm/Module.hx

@@ -28,7 +28,7 @@ enum ModuleHandle {
 }
 
 /**
-	A Neko Module represent a execution unit for the Neko Virtual Machine. 
+	A Neko Module represent a execution unit for the Neko Virtual Machine.
 	Each compiled `.n` bytecode file is a module once loaded by the NekoVM.
 **/
 class Module {
@@ -211,12 +211,12 @@ class Module {
 	static var _module_exec = neko.Lib.load("std","module_exec",1);
 	static var _module_name = neko.Lib.load("std","module_name",1);
 	static var _module_exports = neko.Lib.load("std","module_exports",1);
-	static var _module_loader = neko.Lib.load("std","module_loader",1);
+	static var _module_loader : Dynamic = neko.Lib.load("std","module_loader",1);
 	static var _module_code_size = neko.Lib.load("std","module_code_size",1);
 	static var _module_nglobals = neko.Lib.load("std","module_nglobals",1);
 	static var _module_global_get = neko.Lib.load("std","module_global_get",2);
 	static var _module_global_set = neko.Lib.load("std","module_global_set",3);
-	static var _module_read_string = neko.Lib.loadLazy("std","module_read_string",2);
+	static var _module_read_string : Dynamic = neko.Lib.loadLazy("std","module_read_string",2);
 	static var _module_set_name = neko.Lib.loadLazy("std","module_set_name",2);
 
 }

+ 22 - 0
tests/unit/src/unit/issues/Issue6606.hx

@@ -0,0 +1,22 @@
+package unit.issues;
+
+import haxe.ds.Option;
+
+private interface ITest {
+	function foo():Int;
+}
+
+private class CTest implements ITest {
+    public function new() {}
+	public function foo() { return 1; }
+}
+
+class Issue6606 extends unit.Test {
+	function test() {
+		function callMe(o:Option<ITest>) { }
+ 		var o:Option<ITest> = Some(new CTest());
+		callMe(Some(new CTest()));
+		var c = Some(new CTest());
+		t(unit.HelperMacros.typeError(callMe(c)));
+	}
+}