Browse Source

[macro] calc position for placed_type_path of a macro-created ENew (closes #9342)

Aleksandr Kuzmenko 5 years ago
parent
commit
717c4061d0

+ 20 - 0
src/typing/typer.ml

@@ -1731,6 +1731,26 @@ and type_object_decl ctx fl with_type p =
 	)
 
 and type_new ctx path el with_type force_inline p =
+	let path =
+		if snd path <> null_pos then
+			path
+		(*
+			Since macros don't have placed_type_path structure on Haxe side any ENew will have null_pos in `path`.
+			Try to calculate a better pos.
+		*)
+		else begin
+			match el with
+			| (_,p1) :: _ when p1.pfile = p.pfile && p.pmin < p1.pmin ->
+				let pmin = p.pmin + (String.length "new ")
+				and pmax = p1.pmin - 2 (* Additional "1" for an opening bracket *)
+				in
+				fst path, { p with
+					pmin = if pmin < pmax then pmin else p.pmin;
+					pmax = pmax;
+				}
+			| _ -> fst path, p
+		end
+	in
 	let unify_constructor_call c params f ct = match follow ct with
 		| TFun (args,r) ->
 			(try

+ 15 - 0
tests/misc/projects/Issue9342/Macro.hx

@@ -0,0 +1,15 @@
+import haxe.macro.Context;
+import haxe.macro.Expr;
+
+class Macro {
+	static public macro function foo() {
+		var pos = Context.currentPos();
+		return macro @:pos(pos) new Foo();
+	}
+#if macro
+	static function buildFoo() {
+		Context.warning('check pos', Context.currentPos());
+		return Context.typeof(macro [1]);
+	}
+#end
+}

+ 8 - 0
tests/misc/projects/Issue9342/Main.hx

@@ -0,0 +1,8 @@
+class Main {
+	static function main() {
+		Macro.foo();
+	}
+}
+
+@:genericBuild(Macro.buildFoo())
+class Foo {}

+ 1 - 0
tests/misc/projects/Issue9342/compile.hxml

@@ -0,0 +1 @@
+-main Main

+ 1 - 0
tests/misc/projects/Issue9342/compile.hxml.stderr

@@ -0,0 +1 @@
+Main.hx:3: characters 3-14 : Warning : check pos