merlin icon indicating copy to clipboard operation
merlin copied to clipboard

correct fields of incorrect record expression are dropped

Open AllanBlanchard opened this issue 2 years ago • 9 comments

In the following example:

type t = C of int
type record = { field : t; other : bool; }

let f () =
  let field = 42 in
  { field (* <--- asking the type for this field *) = C field ; other = true }

We get int instead of t. I get this result both with Emacs and VSCode.

Note that, if we remove field other, then getting a code like this:

type t = C of int
type record = { field : t; }

let f () =
  let field = 42 in
  { field (* <--- asking the type for this field *) = C field }

We correctly get t.

Other behavior, if we remove the local field variable, thus getting the following code:

type t = C of int
type record = { field : t; other : bool; }

let f () =
  { field (* <--- asking the type for this field *) = C 42 ; other = true }

We get the error Unbound value field on Emacs and I can't ask the type of field on VSCode.

I found an issue that seems to be related: https://github.com/ocaml/merlin/issues/864. It was fixed in https://github.com/ocaml/merlin/pull/1108, thus Merlin version 3.3.5. I am currently using 4.2-411 with OCaml 4.11.0.

AllanBlanchard avatar Jul 20 '21 06:07 AllanBlanchard

I do observe the behaviour you are describing when using vscode (and thus ocaml-lsp). But everything works well in Emacs when using merlin-mode, are you sure that is not the case for you ? Or maybe you use OCaml-LSP for emacs too ?

When adding a case to the test suite Merlin gives the correct answer:

  $ cat >test.ml <<EOF
  > type t = C of int
  > type record = { field : t; other : bool; }
  >  
  > let f () =
  >   let field = 42 in
  >    { field = C field ; other = true }
  > EOF

  $ ocamlmerlin single type-enclosing -verbosity 0 \
  > -filename test.ml -position 6:8 < test.ml | jq '.value[0]'
  {
    "start": {
      "line": 6,
      "col": 5
    },
    "end": {
      "line": 6,
      "col": 10
    },
    "type": "t",
    "tail": "no"
  }

My guess would be that this is not a Merlin issue but an OCaml-LSP one... @ulugbekna ?

voodoos avatar Jul 27 '21 14:07 voodoos

image

Seems fine to me with the ocaml-lsp master (and latest release 1.6.1). Since the user reported a specific merlin version, I think they don't use ocaml-lsp.

@voodoos with which ocaml-lsp version do you observe this?

ulugbekna avatar Jul 27 '21 15:07 ulugbekna

I was on your construct branch :-)

But the issue persists after upgrading to 1.7.0: image

(new file, no build)

Since the user reported a specific merlin version, I think they don't use ocaml-lsp.

He also mentioned VSCode so he is probably using ocaml-lsp too ?

voodoos avatar Jul 27 '21 17:07 voodoos

I was on your construct branch :-)

I should've let you know that we merged it into master (along with refactor-open support and other cool things), so you can pin master now :-)

He also mentioned VSCode so he is probably using ocaml-lsp too ?

Sorry, I missed it.


The ocaml-lsp master branch doesn't have this problem, so @AllanBlanchard you can opam pin the master of ocaml-lsp, if you want.

We can probably close this issue.

ulugbekna avatar Jul 27 '21 17:07 ulugbekna

Or maybe you use OCaml-LSP for emacs too ?

I don't. I just created a fresh Ocaml 4.11.0 switch on a virtual machine with:

  • emacs fresh install,
  • merlin 4.2-411
  • tuareg 2.2.0
  • user-setup

There is no Ocaml LSP server on this switch. I get this:

image

I tried your command above:

image

AllanBlanchard avatar Jul 28 '21 09:07 AllanBlanchard

Thanks for the screenshot!

The issue here is that the record fails to type as is (some fields are missing), and our recovery is not smart enough to keep the fields, so it drops the whole record. We hope to improve on this at some point, but I don't know when that will happen.

trefis avatar Jul 28 '21 09:07 trefis

Argh, I took the wrong screenshot ... So with a well-typed record:

image

image

AllanBlanchard avatar Jul 29 '21 08:07 AllanBlanchard

Ah indeed! Well this is fixed on master, since 24fa9a04648df92a1d3770e568762f8cf7db74c0 apparently. The fix is included in the 4.3 release which reached opam 3 days ago, so if you update, you should be good!

trefis avatar Jul 29 '21 10:07 trefis

Good to know! Thank you for your time :)

AllanBlanchard avatar Jul 29 '21 11:07 AllanBlanchard