diff --git a/src/analysis/env_builder/name_resolver.ml b/src/analysis/env_builder/name_resolver.ml index 1f20c816998..4c18fe05f7d 100644 --- a/src/analysis/env_builder/name_resolver.ml +++ b/src/analysis/env_builder/name_resolver.ml @@ -2927,9 +2927,11 @@ module Make (Context : C) (FlowAPIUtils : F with type cx = Context.t) : bindings (fun () -> ignore @@ this#match_pattern pattern; - Base.Option.iter guard ~f:(fun guard -> ignore @@ this#expression guard); let completion_state = - this#run_to_completion (fun () -> ignore @@ this#expression body) + this#run_to_completion (fun () -> + Base.Option.iter guard ~f:(fun guard -> ignore @@ this#expression guard); + ignore @@ this#expression body + ) in completion_states := completion_state :: !completion_states) () diff --git a/src/typing/statement.ml b/src/typing/statement.ml index 1a7840e4e23..f0b42d6cc37 100644 --- a/src/typing/statement.ml +++ b/src/typing/statement.ml @@ -2821,11 +2821,20 @@ module Make name_loc ) in - let guard = Base.Option.map guard ~f:(expression cx) in - let ((((_, t), _) as body), throws) = + let (guard, guard_throws) = + match guard with + | Some guard -> + let (guard, throws) = + Abnormal.catch_expr_control_flow_exception (fun () -> expression cx guard) + in + (Some guard, throws) + | None -> (None, false) + in + let ((((_, t), _) as body), body_throws) = Abnormal.catch_expr_control_flow_exception (fun () -> expression cx body) in let case_ast = (case_loc, { Match.Case.pattern; body; guard; comments }) in + let throws = guard_throws || body_throws in let all_throws = all_throws && throws in let ts = if throws then diff --git a/tests/match/body.js b/tests/match/body.js index 4682a47dd2d..54d2f2938be 100644 --- a/tests/match/body.js +++ b/tests/match/body.js @@ -50,7 +50,8 @@ function f1() { function f2() { const out = match (x) { 1 if invariant(false): true, - _: true, + _: 's', }; - out; // ERROR: unreachable + out as string; // OK + out as empty; // ERROR } diff --git a/tests/match/match.exp b/tests/match/match.exp index fe03698b2b5..04600de596d 100644 --- a/tests/match/match.exp +++ b/tests/match/match.exp @@ -91,12 +91,21 @@ Unreachable code. [unreachable-code] ^^^^ -Error ----------------------------------------------------------------------------------------------------- body.js:55:3 +Error ----------------------------------------------------------------------------------------------------- body.js:56:3 -Unreachable code. [unreachable-code] +Cannot cast `out` to empty because string [1] is incompatible with empty [2]. [incompatible-cast] - 55| out; // ERROR: unreachable - ^^^^ + body.js:56:3 + 56| out as empty; // ERROR + ^^^ + +References: + body.js:53:8 + 53| _: 's', + ^^^ [1] + body.js:56:10 + 56| out as empty; // ERROR + ^^^^^ [2] Error -------------------------------------------------------------------------------------------------- patterns.js:9:3