Browse Source

if a class containing a macro is defined through Context.defineType add it to the macro context as well (closes #1497)

Simon Krajewski 11 years ago
parent
commit
1fda495a49
3 changed files with 43 additions and 13 deletions
  1. 8 0
      tests/unit/issues/Issue1497.hx
  2. 13 0
      tests/unit/issues/misc/Issue1497Macro.hx
  3. 22 13
      typer.ml

+ 8 - 0
tests/unit/issues/Issue1497.hx

@@ -0,0 +1,8 @@
+package unit.issues;
+
+class Issue1497 extends Test {
+	function test() {
+        unit.issues.misc.Issue1497Macro.run();
+        eq(1, Issue1497DefinedClass.test());
+	}
+}

+ 13 - 0
tests/unit/issues/misc/Issue1497Macro.hx

@@ -0,0 +1,13 @@
+package unit.issues.misc;
+
+class Issue1497Macro {
+    macro public static function run() {
+        var cl = macro class Issue1497DefinedClass {
+            macro public static function test() {
+                return macro 1;
+            }
+        };
+        haxe.macro.Context.defineType(cl);
+        return macro null;
+    }
+}

+ 22 - 13
typer.ml

@@ -4231,19 +4231,28 @@ let make_macro_api ctx p =
 		);
 		Interp.define_type = (fun v ->
 			let m, tdef, pos = (try Interp.decode_type_def v with Interp.Invalid_expr -> Interp.exc (Interp.VString "Invalid type definition")) in
-			let prev = (try Some (Hashtbl.find ctx.g.modules m) with Not_found -> None) in
-			let mnew = Typeload.type_module ctx m ctx.m.curmod.m_extra.m_file [tdef,pos] pos in
-			add_dependency mnew ctx.m.curmod;
-			(* if we defined a type in an existing module, let's move the types here *)
-			(match prev with
-			| None ->
-				mnew.m_extra.m_kind <- MFake;
-			| Some mold ->
-				Hashtbl.replace ctx.g.modules mnew.m_path mold;
-				mold.m_types <- mold.m_types @ mnew.m_types;
-				mnew.m_extra.m_kind <- MSub;
-				add_dependency mold mnew;
-			);
+			let add ctx =
+				let prev = (try Some (Hashtbl.find ctx.g.modules m) with Not_found -> None) in
+				let mnew = Typeload.type_module ctx m ctx.m.curmod.m_extra.m_file [tdef,pos] pos in
+				add_dependency mnew ctx.m.curmod;
+				(* if we defined a type in an existing module, let's move the types here *)
+				(match prev with
+				| None ->
+					mnew.m_extra.m_kind <- MFake;
+				| Some mold ->
+					Hashtbl.replace ctx.g.modules mnew.m_path mold;
+					mold.m_types <- mold.m_types @ mnew.m_types;
+					mnew.m_extra.m_kind <- MSub;
+					add_dependency mold mnew;
+				);
+			in
+			add ctx;
+			(* if we are adding a class which has a macro field, we also have to add it to the macro context (issue #1497) *)
+			if not ctx.in_macro then match tdef,ctx.g.macros with
+			| EClass c,Some (_,mctx) when List.exists (fun cff -> (Meta.has Meta.Macro cff.cff_meta || List.mem AMacro cff.cff_access)) c.d_data ->
+				add mctx
+			| _ ->
+				()
 		);
 		Interp.define_module = (fun m types ->
 			let types = List.map (fun v ->