Skip to content

Commit 5bbb09d

Browse files
committed
clean up reported location for let-bound functions
report the entire value binding when not in the lsp-compat regime also move all the lsp-compat tests to a separate file to group them together Signed-off-by: David Vulakh <[email protected]>
1 parent b0124d0 commit 5bbb09d

File tree

5 files changed

+215
-224
lines changed

5 files changed

+215
-224
lines changed

src/analysis/stack_or_heap_enclosing.ml

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,15 @@ let from_nodes ~lsp_compat ~pos ~path =
3333
| ( Pattern { pat_desc = Tpat_var _; _ },
3434
Some
3535
(Value_binding
36-
{ vb_expr = { exp_desc = Texp_function { alloc_mode; _ }; _ }; _ })
37-
) -> ret (Alloc_mode alloc_mode.mode)
36+
{ vb_expr = { exp_desc = Texp_function { alloc_mode; _ }; _ };
37+
vb_loc;
38+
_
39+
}) ) ->
40+
(* The location that most sensibly corresponds to the "allocation" is the entire
41+
value binding. However, the LSP hover at this point will describe just the
42+
pattern, so we don't override the location in the [lsp_compat] regime. *)
43+
let loc = if lsp_compat then None else Some vb_loc in
44+
ret ?loc (Alloc_mode alloc_mode.mode)
3845
| Expression { exp_desc; _ }, _ -> (
3946
match exp_desc with
4047
| Texp_function { alloc_mode; body; _ } -> (
@@ -73,6 +80,10 @@ let from_nodes ~lsp_compat ~pos ~path =
7380
({ loc; txt = _lident }, { cstr_repr; _ }, args, maybe_alloc_mode)
7481
-> (
7582
let loc =
83+
(* The location of the "allocation" here is the entire expression, but the LSP
84+
hover for a constructor reports information just for the constructor (not the
85+
entire [Texp_construct] expression), so we override the location in the
86+
[lsp_compat] regime. *)
7687
if lsp_compat && cursor_is_inside loc then Some loc else None
7788
in
7889
match maybe_alloc_mode with

tests/test-dirs/stack-or-heap.t/closures.ml

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -57,35 +57,3 @@ let f = function
5757
| Some _ -> 1
5858
(* ^ *)
5959

60-
(* Pattern of a [let]-bound function (we treat this case specially to improve LSP
61-
compatibility) *)
62-
63-
let f g x y =
64-
(* ^ *)
65-
let z = x + y in
66-
exclave_ Some (g z)
67-
and h g x y =
68-
(* ^ *)
69-
let z = x + y in
70-
exclave_ Some (g z)
71-
;;
72-
73-
let ignore (local_ _) = ()
74-
75-
let () =
76-
let f g x y =
77-
(* ^ *)
78-
let z = x + y in
79-
exclave_ Some (g z)
80-
and h g x y =
81-
(* ^ *)
82-
let z = x + y in
83-
exclave_ Some (g z)
84-
in
85-
ignore f;
86-
ignore h
87-
88-
(* Ensure other [let]-bound patterns aren't treated this way *)
89-
90-
let x = Some 5
91-
(* ^ *)

tests/test-dirs/stack-or-heap.t/constructors.ml

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -18,27 +18,6 @@ let f g x y =
1818
y
1919
;;
2020

21-
(* Cursor on the constructor itself (we treat this case specially to improve LSP
22-
compatibility) *)
23-
24-
let f g x y =
25-
let z = x + y in
26-
Some (g z)
27-
(* ^ *)
28-
;;
29-
30-
let f g x y =
31-
let z = x + y in
32-
exclave_ Some (g z)
33-
(* ^ *)
34-
;;
35-
36-
let f g x y =
37-
let z = Some (g x) in
38-
(* ^ *)
39-
y
40-
;;
41-
4221
(* Constructors with no arguments *)
4322

4423
let f g x y =
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
(* Cursor on the constructor itself (we treat this case specially to improve LSP
2+
compatibility) *)
3+
4+
let f g x y =
5+
let z = x + y in
6+
Some (g z)
7+
(* ^ *)
8+
;;
9+
10+
let f g x y =
11+
let z = x + y in
12+
exclave_ Some (g z)
13+
(* ^ *)
14+
;;
15+
16+
let f g x y =
17+
let z = Some (g x) in
18+
(* ^ *)
19+
y
20+
;;
21+
22+
(* Pattern of a [let]-bound function (we treat this case specially to improve LSP
23+
compatibility) *)
24+
25+
let f g x y =
26+
(* ^ *)
27+
let z = x + y in
28+
exclave_ Some (g z)
29+
and h g x y =
30+
(* ^ *)
31+
let z = x + y in
32+
exclave_ Some (g z)
33+
;;
34+
35+
let ignore (local_ _) = ()
36+
37+
let () =
38+
let f g x y =
39+
(* ^ *)
40+
let z = x + y in
41+
exclave_ Some (g z)
42+
and h g x y =
43+
(* ^ *)
44+
let z = x + y in
45+
exclave_ Some (g z)
46+
in
47+
ignore f;
48+
ignore h
49+
50+
(* Ensure other [let]-bound patterns aren't treated this way *)
51+
52+
let x = Some 5
53+
(* ^ *)

0 commit comments

Comments
 (0)