@@ -543,6 +543,14 @@ let simplify_or p =
543543 in
544544 try simpl_rec p with Var p -> p
545545
546+ let bind_record_rest loc arg rest action =
547+ Llet
548+ ( Strict ,
549+ Pgenval ,
550+ rest.rest_ident,
551+ Lprim (Precord_spread_new rest.excluded_labels, [arg], loc),
552+ action )
553+
546554let simplify_cases args cls =
547555 match args with
548556 | [] -> assert false
@@ -560,7 +568,12 @@ let simplify_cases args cls =
560568 | Tpat_record (lbls , closed , rest ) ->
561569 let all_lbls = all_record_args lbls in
562570 let full_pat =
563- {pat with pat_desc = Tpat_record (all_lbls, closed, rest)}
571+ {pat with pat_desc = Tpat_record (all_lbls, closed, None )}
572+ in
573+ let action =
574+ match rest with
575+ | None -> action
576+ | Some rest -> bind_record_rest pat.pat_loc arg rest action
564577 in
565578 (full_pat :: patl, action) :: simplify rem
566579 | Tpat_or _ -> (
@@ -617,8 +630,11 @@ let rec extract_vars r p =
617630 | Tpat_var (id , _ ) -> IdentSet. add id r
618631 | Tpat_alias (p , id , _ ) -> extract_vars (IdentSet. add id r) p
619632 | Tpat_tuple pats -> List. fold_left extract_vars r pats
620- | Tpat_record (lpats , _ , _rest ) ->
621- List. fold_left (fun r (_ , _ , p , _ ) -> extract_vars r p) r lpats
633+ | Tpat_record (lpats , _ , rest ) -> (
634+ let r = List. fold_left (fun r (_ , _ , p , _ ) -> extract_vars r p) r lpats in
635+ match rest with
636+ | None -> r
637+ | Some rest -> IdentSet. add rest.rest_ident r)
622638 | Tpat_construct (_ , _ , pats ) -> List. fold_left extract_vars r pats
623639 | Tpat_array pats -> List. fold_left extract_vars r pats
624640 | Tpat_variant (_ , Some p , _ ) -> extract_vars r p
@@ -2742,32 +2758,7 @@ let partial_function loc () =
27422758 ],
27432759 loc )
27442760
2745- (* For record patterns with rest, inject the rest binding into the action body *)
2746- let inject_record_rest_binding param (pat , action ) =
2747- match pat.pat_desc with
2748- | Tpat_record (_ , _ , Some rest ) ->
2749- let action_with_rest =
2750- Llet
2751- ( Strict ,
2752- Pgenval ,
2753- rest.rest_ident,
2754- Lprim (Precord_spread_new rest.excluded_labels, [param], pat.pat_loc),
2755- action )
2756- in
2757- let pat_without_rest =
2758- {
2759- pat with
2760- pat_desc =
2761- (match pat.pat_desc with
2762- | Tpat_record (fields , closed , _ ) -> Tpat_record (fields, closed, None )
2763- | _ -> pat.pat_desc);
2764- }
2765- in
2766- (pat_without_rest, action_with_rest)
2767- | _ -> (pat, action)
2768-
27692761let for_function loc repr param pat_act_list partial =
2770- let pat_act_list = List. map (inject_record_rest_binding param) pat_act_list in
27712762 compile_matching repr (partial_function loc) param pat_act_list partial
27722763
27732764(* In the following two cases, exhaustiveness info is not available! *)
@@ -2836,28 +2827,6 @@ let for_let loc param pat body =
28362827 | Tpat_var (id , _ ) ->
28372828 (* fast path, and keep track of simple bindings to unboxable numbers *)
28382829 Llet (Strict , Pgenval , id, param, body)
2839- | Tpat_record (_ , _ , Some rest ) ->
2840- (* Record pattern with rest: compile the explicit field bindings normally,
2841- then add a binding for the rest ident using Precord_spread_new *)
2842- let body_with_rest =
2843- Llet
2844- ( Strict ,
2845- Pgenval ,
2846- rest.rest_ident,
2847- Lprim (Precord_spread_new rest.excluded_labels, [param], loc),
2848- body )
2849- in
2850- (* Compile the explicit fields pattern (without rest) into the body *)
2851- let pat_without_rest =
2852- {
2853- pat with
2854- pat_desc =
2855- (match pat.pat_desc with
2856- | Tpat_record (fields , closed , _ ) -> Tpat_record (fields, closed, None )
2857- | _ -> pat.pat_desc);
2858- }
2859- in
2860- simple_for_let loc param pat_without_rest body_with_rest
28612830 | _ -> simple_for_let loc param pat body
28622831
28632832(* Handling of tupled functions and matchings *)
0 commit comments