Browse Source

improved macro error reporting (close #3566)

Nicolas Cannasse 10 years ago
parent
commit
47d6e44d8d
2 changed files with 11 additions and 3 deletions
  1. 4 1
      typeload.ml
  2. 7 2
      typer.ml

+ 4 - 1
typeload.ml

@@ -25,6 +25,8 @@ open Type
 open Common
 open Typecore
 
+let locate_macro_error = ref true
+
 (*
 	Build module structure : should be atomic - no type loading is possible
 *)
@@ -1665,9 +1667,10 @@ let init_class ctx c p context_init herits fields =
 		on_error = (fun ctx msg ep ->
 			ctx.com.error msg ep;
 			(* macros expressions might reference other code, let's recall which class we are actually compiling *)
-			if ep.pfile <> c.cl_pos.pfile then ctx.com.error "Defined in this class" c.cl_pos
+			if !locate_macro_error && (ep.pfile <> c.cl_pos.pfile || ep.pmax < c.cl_pos.pmin || ep.pmin > c.cl_pos.pmax) then ctx.com.error "Defined in this class" c.cl_pos
 		);
 	} in
+	locate_macro_error := true;
 	incr stats.s_classes_built;
 	let fields = patch_class ctx c fields in
 	let fields = ref fields in

+ 7 - 2
typer.ml

@@ -3941,9 +3941,14 @@ and build_call ctx acc el (with_type:with_type) p =
 		ctx.with_type_stack <- List.tl ctx.with_type_stack;
 		let old = ctx.on_error in
 		ctx.on_error <- (fun ctx msg ep ->
-			old ctx msg ep;
 			(* display additional info in the case the error is not part of our original call *)
-			if ep.pfile <> p.pfile || ep.pmax < p.pmin || ep.pmin > p.pmax then old ctx "Called from macro here" p
+			if ep.pfile <> p.pfile || ep.pmax < p.pmin || ep.pmin > p.pmax then begin
+				Typeload.locate_macro_error := false;
+				old ctx msg ep;
+				Typeload.locate_macro_error := true;
+				ctx.com.error "Called from macro here" p;
+			end else
+				old ctx msg ep;
 		);
 		let e = try
 			f()