@@ -480,8 +480,19 @@ let rec resolve_binding cx def_scope_kind reason loc b =
480480 t
481481 | Root (Value { hints = _ ; expr; decl_kind; as_const } ) ->
482482 expression cx ?decl:decl_kind ~as_const expr
483- | Root (MatchCaseRoot { case_match_root_loc; prev_pattern_locs_rev = _ } ) ->
484- Type_env. var_ref cx (OrdinaryName Flow_ast_utils. match_root_name) case_match_root_loc
483+ | Root (MatchCaseRoot { case_match_root_loc; root_pattern_loc; prev_pattern_locs_rev } ) ->
484+ let unfiltered_t =
485+ Type_env. var_ref cx (OrdinaryName Flow_ast_utils. match_root_name) case_match_root_loc
486+ in
487+ let node_cache = Context. node_cache cx in
488+ let patterns =
489+ Base.List. rev_map prev_pattern_locs_rev ~f: (fun l ->
490+ Base.Option. value_exn (Node_cache. get_match_pattern node_cache l)
491+ )
492+ in
493+ let value_left = Exhaustive. partial_leftover_value_union cx patterns unfiltered_t in
494+ Node_cache. set_match_pattern_value_union (Context. node_cache cx) root_pattern_loc value_left;
495+ Match_pattern_ir.ValueUnion. to_type (TypeUtil. reason_of_t unfiltered_t) value_left
485496 | Root (ObjectValue { obj_loc = loc ; obj; synthesizable = ObjectSynthesizable _ } ) ->
486497 let open Ast.Expression.Object in
487498 let resolve_prop ~bind_this ~prop_loc ~fn_loc fn =
@@ -832,44 +843,56 @@ let rec resolve_binding cx def_scope_kind reason loc b =
832843 let t = resolve_binding cx def_scope_kind reason loc binding in
833844 make_hooklike cx t
834845 | Select { selector; parent = (parent_loc , binding ) } ->
835- let refined_type =
836- match selector with
837- | Selector. Prop { prop; prop_loc; _ } ->
838- let desc = RProperty (Some (OrdinaryName prop)) in
839- Type_env. get_refinement cx desc prop_loc
840- | _ -> None
846+ let node_cache = Context. node_cache cx in
847+ let filtered_pattern_type =
848+ Node_cache. get_match_pattern_value_union node_cache parent_loc
849+ |> Base.Option. bind ~f: (Match_pattern_ir.ValueUnion. select ~selector )
850+ |> Base.Option. map ~f: (fun value_left ->
851+ Node_cache. set_match_pattern_value_union node_cache loc value_left;
852+ Match_pattern_ir.ValueUnion. to_type reason value_left
853+ )
841854 in
842- (match refined_type with
843- | Some t ->
844- (* When we can get a refined value on a destructured property,
845- we must be in an assignment position and the type must have been resolved. *)
846- t
855+ (match filtered_pattern_type with
856+ | Some t -> t
847857 | None ->
848- let t = Type_env. checked_find_loc_env_write cx Env_api. PatternLoc parent_loc in
849- let has_anno = binding_has_annot binding in
850- let (selector, reason, has_default) = mk_selector_reason_has_default cx loc selector in
851- let kind =
852- if has_anno then
853- DestructAnnot
854- else
855- DestructInfer
858+ let refined_type =
859+ match selector with
860+ | Selector. Prop { prop; prop_loc; _ } ->
861+ let desc = RProperty (Some (OrdinaryName prop)) in
862+ Type_env. get_refinement cx desc prop_loc
863+ | _ -> None
856864 in
857- let t =
858- Flow_js_utils. map_on_resolved_type cx reason t (fun t ->
859- Tvar_resolver. mk_tvar_and_fully_resolve_no_wrap_where cx reason (fun tout ->
860- Flow_js. flow cx (t, DestructuringT (reason, kind, selector, tout, Reason. mk_id () ))
861- )
862- )
863- in
864- if has_default then
865- let (selector, reason, _) = mk_selector_reason_has_default cx loc Selector. Default in
866- Flow_js_utils. map_on_resolved_type cx reason t (fun t ->
867- Tvar_resolver. mk_tvar_and_fully_resolve_no_wrap_where cx reason (fun tout ->
868- Flow_js. flow cx (t, DestructuringT (reason, kind, selector, tout, Reason. mk_id () ))
869- )
870- )
871- else
872- t)
865+ (match refined_type with
866+ | Some t ->
867+ (* When we can get a refined value on a destructured property,
868+ we must be in an assignment position and the type must have been resolved. *)
869+ t
870+ | None ->
871+ let t = Type_env. checked_find_loc_env_write cx Env_api. PatternLoc parent_loc in
872+ let has_anno = binding_has_annot binding in
873+ let (selector, reason, has_default) = mk_selector_reason_has_default cx loc selector in
874+ let kind =
875+ if has_anno then
876+ DestructAnnot
877+ else
878+ DestructInfer
879+ in
880+ let t =
881+ Flow_js_utils. map_on_resolved_type cx reason t (fun t ->
882+ Tvar_resolver. mk_tvar_and_fully_resolve_no_wrap_where cx reason (fun tout ->
883+ Flow_js. flow cx (t, DestructuringT (reason, kind, selector, tout, Reason. mk_id () ))
884+ )
885+ )
886+ in
887+ if has_default then
888+ let (selector, reason, _) = mk_selector_reason_has_default cx loc Selector. Default in
889+ Flow_js_utils. map_on_resolved_type cx reason t (fun t ->
890+ Tvar_resolver. mk_tvar_and_fully_resolve_no_wrap_where cx reason (fun tout ->
891+ Flow_js. flow cx (t, DestructuringT (reason, kind, selector, tout, Reason. mk_id () ))
892+ )
893+ )
894+ else
895+ t))
873896
874897let resolve_inferred_function
875898 cx ~scope_kind ~statics ~needs_this_param id_loc reason function_loc function_ =
0 commit comments