瀏覽代碼

check caught error position when recovering from match typing failure

closes #12098
Simon Krajewski 3 月之前
父節點
當前提交
e7c547fb00

+ 10 - 5
src/typing/matcher.ml

@@ -269,11 +269,16 @@ module Pattern = struct
 		let display_mode () =
 		let display_mode () =
 			if pctx.is_postfix_match then DKMarked else DKPattern toplevel
 			if pctx.is_postfix_match then DKMarked else DKPattern toplevel
 		in
 		in
-		let catch_errors () =
+		let catch_errors p =
 			let old = ctx.com.located_error in
 			let old = ctx.com.located_error in
 			let restore_report_mode = disable_report_mode ctx.com in
 			let restore_report_mode = disable_report_mode ctx.com in
-			ctx.com.located_error <- (fun ?depth _ ->
-				raise Exit
+			ctx.com.located_error <- (fun ?depth err ->
+				let ep = extract_located_pos err in
+				(* The error might not actually come from here, let's check the position (issue #12098). *)
+				if ep.pfile <> p.pfile || ep.pmax < p.pmin || ep.pmin > p.pmax then
+					old err
+				else
+					raise Exit
 			);
 			);
 			(fun () ->
 			(fun () ->
 				restore_report_mode();
 				restore_report_mode();
@@ -283,7 +288,7 @@ module Pattern = struct
 		let try_typing e =
 		let try_typing e =
 			let old = ctx.untyped in
 			let old = ctx.untyped in
 			ctx.untyped <- true;
 			ctx.untyped <- true;
-			let restore = catch_errors () in
+			let restore = catch_errors (pos e) in
 			let e = try
 			let e = try
 				type_expr ctx e (WithType.with_type t)
 				type_expr ctx e (WithType.with_type t)
 			with exc ->
 			with exc ->
@@ -305,7 +310,7 @@ module Pattern = struct
 				try_typing (EConst (Ident s),p)
 				try_typing (EConst (Ident s),p)
 			with
 			with
 			| Exit | Bad_pattern _ ->
 			| Exit | Bad_pattern _ ->
-				let restore = catch_errors () in
+				let restore = catch_errors p in
 				begin try
 				begin try
 					let mt = module_type_of_type t in
 					let mt = module_type_of_type t in
 					let e_mt = TyperBase.type_module_type ctx mt None p in
 					let e_mt = TyperBase.type_module_type ctx mt None p in

+ 21 - 0
tests/misc/projects/Issue12098/Main.hx

@@ -0,0 +1,21 @@
+class Outcome {
+	static function fromEither(ei:haxe.ds.Option<Err>) {
+		switch ei {
+			case Some(o):
+			case _:
+		}
+	}
+}
+
+abstract Err(Bool) {
+	@:from static public function fromInt(n:Int) {
+		return new Err(here);
+	}
+}
+
+class Main {
+	static function main() {
+		var err:Err = 5;
+		trace(err);
+	}
+}

+ 2 - 0
tests/misc/projects/Issue12098/compile-fail.hxml

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

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

@@ -0,0 +1 @@
+Main.hx:12: characters 10-23 : Err does not have a constructor