Skip to content

Commit

Permalink
Cleanup Let_binding.split_annot
Browse files Browse the repository at this point in the history
  • Loading branch information
gpetiot committed Jan 23, 2024
1 parent 4826c40 commit 0e5cea0
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions lib/Sugar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -168,25 +168,21 @@ module Let_binding = struct
in
Exp (Ast_helper.Exp.fun_ param exp)
in
( xargs
, Some (Pvc_constraint {locally_abstract_univars= []; typ})
( Some (Pvc_constraint {locally_abstract_univars= []; typ})
, sub_exp ~ctx:exp_ctx exp )
(* The type constraint is always printed before the declaration for
functions, for other value bindings we preserve its position. *)
| Pexp_constraint (exp, typ) when not (List.is_empty xargs) ->
Cmts.relocate cmts ~src:body.pexp_loc ~before:exp.pexp_loc
~after:exp.pexp_loc ;
( xargs
, Some (Pvc_constraint {locally_abstract_univars= []; typ})
( Some (Pvc_constraint {locally_abstract_univars= []; typ})
, sub_exp ~ctx exp )
| Pexp_coerce (exp, typ1, typ2)
when Source.type_constraint_is_first typ2 exp.pexp_loc ->
Cmts.relocate cmts ~src:body.pexp_loc ~before:exp.pexp_loc
~after:exp.pexp_loc ;
( xargs
, Some (Pvc_coercion {ground= typ1; coercion= typ2})
, sub_exp ~ctx exp )
| _ -> (xargs, None, xbody)
(Some (Pvc_coercion {ground= typ1; coercion= typ2}), sub_exp ~ctx exp)
| _ -> (None, xbody)

let split_fun_args cmts xpat xbody =
let xargs, xbody =
Expand All @@ -195,9 +191,12 @@ module Let_binding = struct
fun_ cmts ~will_keep_first_ast_node:false xbody
| _ -> ([], xbody)
in
match (xbody.ast.pexp_desc, xpat.ast.ppat_desc) with
| Pexp_constraint _, Ppat_constraint _ -> (xargs, None, xbody)
| _ -> split_annot cmts xargs xbody
let annot =
match (xbody.ast.pexp_desc, xpat.ast.ppat_desc) with
| Pexp_constraint _, Ppat_constraint _ -> (None, xbody)
| _ -> split_annot cmts xargs xbody
in
(xargs, annot)

let should_desugar_args pat typ =
match (pat.ast, typ) with
Expand All @@ -210,10 +209,10 @@ module Let_binding = struct
let lb_exp = sub_exp ~ctx pvb_expr
and lb_pat = sub_pat ~ctx pvb_pat
and lb_typ = pvb_constraint in
let lb_args, lb_typ, lb_exp =
let lb_args, (lb_typ, lb_exp) =
if should_desugar_args lb_pat lb_typ then
split_fun_args cmts lb_pat lb_exp
else ([], lb_typ, lb_exp)
else ([], (lb_typ, lb_exp))
in
{ lb_op= Location.{txt= (if first then "let" else "and"); loc= none}
; lb_pat
Expand Down

0 comments on commit 0e5cea0

Please sign in to comment.