Skip to content

Commit

Permalink
Fix letops tuples handling (fixes #1683 and ocaml/ocaml-lsp#1182) (#1684
Browse files Browse the repository at this point in the history
)

from voodoos/letop-tuples-issue-1683
  • Loading branch information
voodoos authored Sep 26, 2023
2 parents a717c89 + d19e1eb commit 9b072a5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 4 deletions.
8 changes: 8 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
merlin 4.12
===========
unreleased

+ merlin binary
- Fix issue with let operators and tuples (#1684, fixes #1683, fixes
ocaml/ocaml-lsp#1182)

merlin 4.11
===========
Thu Sep 24 18:01:42 CEST 2023
Expand Down
12 changes: 8 additions & 4 deletions src/ocaml/merlin_specific/browse_raw.ml
Original file line number Diff line number Diff line change
Expand Up @@ -380,14 +380,18 @@ let of_expression_desc loc = function
| Texp_unreachable | Texp_extension_constructor _ ->
id_fold
| Texp_letop { let_; ands; body; _ } ->
let rec flatten_patterns acc pat =
(* let+ ..pat1 and pat2 and ... are represented as pattern couples:
[pat1; [pat2; ...]]. The following function flattens these couples.
Keeping track of the known size of the tuple prevent wrongly flattening
the patterns patN when they are tuples themselves. *)
let rec flatten_patterns ~size acc pat =
match pat.pat_desc with
| Tpat_tuple [ tuple; pat ] ->
flatten_patterns (pat :: acc) tuple
| Tpat_tuple [ tuple; pat ] when size > 0 ->
flatten_patterns ~size:(size - 1) (pat :: acc) tuple
| _ -> List.rev (pat :: acc)
in
let bindops = let_ :: ands in
let patterns = flatten_patterns [] body.c_lhs in
let patterns = flatten_patterns ~size:(List.length ands) [] body.c_lhs in
let of_letop (pat, bindop) = of_bop bindop ** of_pattern pat in
list_fold of_letop (List.combine patterns bindops) **
of_expression body.c_rhs
Expand Down
35 changes: 35 additions & 0 deletions tests/test-dirs/issue1683-ill-formed-letop.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
$ cat >main.ml <<EOF
> let (let+) x f = f x
> let (and+) x f = f x
>
> let _ =
> let+ x, _ = 42, 43
> and+ y = 36 and+ _ = 37 in
> x + y
> EOF

Calling `holes` on the code fragment was raising an exception as
described in issue #1683.
$ $MERLIN single holes -filename main.ml <main.ml
{
"class": "return",
"value": [],
"notifications": []
}


$ $MERLIN single type-enclosing \
> -position 5:7 \
> -filename main.ml <main.ml | jq '.value[0]'
{
"start": {
"line": 5,
"col": 7
},
"end": {
"line": 5,
"col": 8
},
"type": "int",
"tail": "no"
}

0 comments on commit 9b072a5

Please sign in to comment.