Skip to content

Commit

Permalink
Merge pull request ocaml#1353 from ocaml/construct-funs
Browse files Browse the repository at this point in the history
[Construct] eagerly build functions
  • Loading branch information
trefis authored Jun 18, 2021
2 parents 3eafee3 + 11b7c21 commit 444f6e0
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
7 changes: 6 additions & 1 deletion src/analysis/construct.ml
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,11 @@ module Gen = struct
(* else we return a hole *)
[ Ast_helper.Exp.hole () ]
in
let arrow_rhs env typ =
match (Ctype.repr typ).desc with
| Tarrow _ -> expression ~idents_table values_scope ~depth env typ
| _ -> exp_or_hole env typ
in

(* [make_arg] tries to provide a nice default name for function args *)
let make_arg =
Expand Down Expand Up @@ -442,7 +447,7 @@ module Gen = struct
}
in
let env = Env.add_value (Ident.create_local name) value_description env in
let exps = exp_or_hole env tyright in
let exps = arrow_rhs env tyright in
List.map exps ~f:(Ast_helper.Exp.fun_ label None argument)
| Ttuple types ->
let choices = List.map types ~f:(exp_or_hole env)
Expand Down
11 changes: 9 additions & 2 deletions src/ocaml/parsing/pprintast.ml
Original file line number Diff line number Diff line change
Expand Up @@ -600,6 +600,12 @@ and sugar_expr ctxt f e =
end
| _ -> false

and uncurry params e =
match e.pexp_desc with
| Pexp_fun (l, e0, p, e) ->
uncurry ((l, e0, p) :: params) e
| _ -> List.rev params, e

and expression ctxt f x =
if x.pexp_attributes <> [] then
pp f "((%a)@,%a)" (expression ctxt) {x with pexp_attributes=[]}
Expand All @@ -616,9 +622,10 @@ and expression ctxt f x =
when ctxt.semi ->
paren true (expression reset_ctxt) f x
| Pexp_fun (l, e0, p, e) ->
let params, body = uncurry [l, e0, p] e in
pp f "@[<2>fun@;%a->@;%a@]"
(label_exp ctxt) (l, e0, p)
(expression ctxt) e
(pp_print_list (label_exp ctxt)) params
(expression ctxt) body
| Pexp_newtype (lid, e) ->
pp f "@[<2>fun@;(type@;%s)@;->@;%a@]" lid.txt
(expression ctxt) e
Expand Down
20 changes: 19 additions & 1 deletion tests/test-dirs/construct/c-simple.t
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,24 @@ Test 3.2
> let x : v:string -> float -> mytype -> mytype -> int = _
> EOF
$ $MERLIN single construct -position 2:55 \
> -filename c32.ml <c32.ml | jq ".value"
[
{
"start": {
"line": 2,
"col": 55
},
"end": {
"line": 2,
"col": 56
}
},
[
"(fun ~v float mytype mytype_1 -> _)"
]
]
$ $MERLIN single construct -depth 4 -position 2:55 \
> -filename c32.ml <c32.ml | jq ".value"
[
Expand All @@ -256,7 +274,7 @@ Test 3.2
}
},
[
"(fun ~v -> fun float -> fun mytype -> fun mytype_1 -> _)"
"(fun ~v float mytype mytype_1 -> 0)"
]
]
Expand Down

0 comments on commit 444f6e0

Please sign in to comment.