Skip to content

Commit

Permalink
occ: do no return PWO for definitions in a mli
Browse files Browse the repository at this point in the history
We don't have a way to identify uids in interfaces with uids in
implementations. This might lead to mixing up results that share
the same uid.
  • Loading branch information
voodoos committed Jun 10, 2024
1 parent 535885b commit ffaf1be
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 25 deletions.
28 changes: 27 additions & 1 deletion src/analysis/occurrences.ml
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ let get_buffer_locs result uid =
(Mtyper.get_index result)
Lid_set.empty

let is_in_interface (config : Mconfig.t) (loc : Warnings.loc) =
let extension = Filename.extension loc.loc_start.pos_fname in
List.exists config.merlin.suffixes
~f:(fun (_impl, intf) -> String.equal extension intf)

let locs_of ~config ~env ~typer_result ~pos ~scope path =
log ~title:"occurrences" "Looking for occurences of %s (pos: %s)"
path
Expand All @@ -135,10 +140,31 @@ let locs_of ~config ~env ~typer_result ~pos ~scope path =
(* We are on a definition / declaration so we look for the node's uid *)
let browse = Mbrowse.of_typedtree local_defs in
let env, node = Mbrowse.leaf_node (Mbrowse.enclosing pos [browse]) in
uid_and_loc_of_node env node, scope
let node_uid_loc = uid_and_loc_of_node env node in
let scope =
match node_uid_loc with
| Some (_, l) when is_in_interface config l ->
(* There is no way to distinguish uids from interfaces from uids of
implementations. We fallback on buffer occurrences in that case.
TODO: we should be able to improve on that situation when we will be
able to distinguish between impl/intf uids and know which declaration
are actually linked. *)
`Buffer
| _ -> scope
in
node_uid_loc, scope
| `Found { uid; location; approximated = false; _ } ->
log ~title:"locs_of" "Found definition uid using locate: %a "
Logger.fmt (fun fmt -> Shape.Uid.print fmt uid);
(* There is no way to distinguish uids from interfaces from uids of
implementations. We fallback on buffer occurrences in that case.
TODO: we should be able to improve on that situation when we will be
able to distinguish between impl/intf uids and know which declaration
are actually linked. *)
let scope =
if is_in_interface config location then `Buffer
else scope
in
Some (uid, location), scope
| `Found { decl_uid; location; approximated = true; _ } ->
log ~title:"locs_of" "Approx. definition: %a "
Expand Down
26 changes: 2 additions & 24 deletions tests/test-dirs/occurrences/project-wide/mli-vs-ml.t
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ The indexer should not mixup uids from mli and ml files:
uid: Main.1; locs: "t": File "main.ml", line 2, characters 5-6 },
0 approx shapes: {}, and shapes for CUS .

FIXME: Merlin should not mixup uids from mli and ml files:
Merlin should not mixup uids from mli and ml files:
$ $MERLIN single occurrences -scope project -identifier-at 2:8 \
> -index-file project.ocaml-index \
> -filename main.mli <main.mli
Expand All @@ -37,17 +37,6 @@ FIXME: Merlin should not mixup uids from mli and ml files:
"col": 6
}
},
{
"file": "$TESTCASE_ROOT/main.ml",
"start": {
"line": 1,
"col": 4
},
"end": {
"line": 1,
"col": 5
}
},
{
"file": "$TESTCASE_ROOT/main.mli",
"start": {
Expand All @@ -63,7 +52,7 @@ FIXME: Merlin should not mixup uids from mli and ml files:
"notifications": []
}

FIXME: same as previous case
Same when the cursor is at the origin:
$ $MERLIN single occurrences -scope project -identifier-at 1:5 \
> -index-file project.ocaml-index \
> -filename main.mli <main.mli
Expand All @@ -81,17 +70,6 @@ FIXME: same as previous case
"col": 6
}
},
{
"file": "$TESTCASE_ROOT/main.ml",
"start": {
"line": 1,
"col": 4
},
"end": {
"line": 1,
"col": 5
}
},
{
"file": "$TESTCASE_ROOT/main.mli",
"start": {
Expand Down

0 comments on commit ffaf1be

Please sign in to comment.