Explorar o código

check caught error position when recovering from match typing failure

closes #12098
Simon Krajewski hai 3 meses
pai
achega
d68d3beb14

+ 11 - 4
src/typing/matcher/exprToPattern.ml

@@ -156,10 +156,17 @@ let rec make pctx toplevel t e =
 	let display_mode () =
 		if pctx.is_postfix_match then DKMarked else DKPattern toplevel
 	in
-	let catch_errors () =
+	let catch_errors p =
 		let old = ctx.com.error_ext in
 		let restore_report_mode = disable_report_mode ctx.com in
-		ctx.com.error_ext <- (fun _ -> raise Exit);
+		ctx.com.error_ext <- (fun err ->
+			let ep = err.err_pos 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 () ->
 			restore_report_mode();
 			ctx.com.error_ext <- old
@@ -168,7 +175,7 @@ let rec make pctx toplevel t e =
 	let try_typing e =
 		let old = ctx.f.untyped in
 		ctx.f.untyped <- true;
-		let restore = catch_errors () in
+		let restore = catch_errors (pos e) in
 		let e = try
 			type_expr ctx e (WithType.with_type t)
 		with exc ->
@@ -190,7 +197,7 @@ let rec make pctx toplevel t e =
 			try_typing (EConst (Ident s),p)
 		with
 		| Exit | Bad_pattern _ ->
-			let restore = catch_errors () in
+			let restore = catch_errors p in
 			begin try
 				let mt = module_type_of_type t in
 				let e_mt = TyperBase.type_module_type ctx mt 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