Skip to content

Commit 8e96af6

Browse files
authored
flambda-backend: Change [%call_pos] to the function's start position, not the application's (#2765)
This feature adjusts the position that [%call_pos] uses. The motivation is |>: ```ocaml (* f : here:[%call_pos] -> unit -> unit *) () |> f |> f ``` Previously, both calls to f received the same position for `~here`! Both positions were the start of `()`. This is because the AST of the expression is roughly `((() |> f) |> f)` and since call_pos uses the location of the entire function application, both calls to f had the same starting location (despite having different ending locations.) This is change in this commit by making [%call_pos] use the start position of the function instead of the start position of the entire application.
1 parent 2e42371 commit 8e96af6

File tree

4 files changed

+51
-6
lines changed

4 files changed

+51
-6
lines changed

testsuite/tests/typing-implicit-source-positions/let_operators.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ let _ =
4848
val ( >>| ) :
4949
call_pos:[%call_pos] -> 'a -> (lexing_position * 'a -> 'b) -> 'b = <fun>
5050
- : lexing_position =
51-
{pos_fname = ""; pos_lnum = 3; pos_bol = 1128; pos_cnum = 1130}
51+
{pos_fname = ""; pos_lnum = 3; pos_bol = 1128; pos_cnum = 1132}
5252
|}]
5353

5454
(* TEST

testsuite/tests/typing-implicit-source-positions/object_system.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ val c : c = <obj>
9595
val from_method_param : lexing_position =
9696
{pos_fname = ""; pos_lnum = 2; pos_bol = 2216; pos_cnum = 2258}
9797
val from_class_param : lexing_position =
98-
{pos_fname = ""; pos_lnum = 1; pos_bol = 2197; pos_cnum = 2205}
98+
{pos_fname = ""; pos_lnum = 1; pos_bol = 2197; pos_cnum = 2206}
9999
|}]
100100

101101
class parent ~(call_pos : [%call_pos]) () = object
@@ -123,7 +123,7 @@ let position = (o ())#pos
123123
[%%expect{|
124124
val o : call_pos:[%call_pos] -> unit -> parent = <fun>
125125
val position : lexing_position =
126-
{pos_fname = ""; pos_lnum = 4; pos_bol = 2964; pos_cnum = 2979}
126+
{pos_fname = ""; pos_lnum = 4; pos_bol = 2964; pos_cnum = 2980}
127127
|}]
128128

129129
(* Applying an call_pos argument without a label. *)
@@ -140,7 +140,7 @@ Warning 6 [labels-omitted]: label call_pos was omitted in the application of thi
140140

141141
val o : call_pos:[%call_pos] -> unit -> parent = <fun>
142142
val position : lexing_position =
143-
{pos_fname = ""; pos_lnum = 4; pos_bol = 3293; pos_cnum = 3308}
143+
{pos_fname = ""; pos_lnum = 4; pos_bol = 3293; pos_cnum = 3309}
144144
|}]
145145

146146

@@ -216,7 +216,7 @@ let x, y = (new c ~y:pos_a ())#xy
216216

217217
[%%expect{|
218218
val x : lexing_position =
219-
{pos_fname = ""; pos_lnum = 1; pos_bol = 5199; pos_cnum = 5210}
219+
{pos_fname = ""; pos_lnum = 1; pos_bol = 5199; pos_cnum = 5211}
220220
val y : lexing_position =
221221
{pos_fname = "a"; pos_lnum = 0; pos_bol = 0; pos_cnum = -1}
222222
|}]
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
(* TEST
2+
expect;
3+
*)
4+
5+
let f ~(here : [%call_pos]) x = here, x
6+
7+
[%%expect
8+
{|
9+
val f : here:[%call_pos] -> 'a -> lexing_position * 'a = <fun>
10+
|}]
11+
12+
let result = () |> f |> f
13+
14+
(* Importantly, these locations are different. *)
15+
[%%expect
16+
{|
17+
val result : lexing_position * (lexing_position * unit) =
18+
({pos_fname = ""; pos_lnum = 1; pos_bol = 145; pos_cnum = 169},
19+
({pos_fname = ""; pos_lnum = 1; pos_bol = 145; pos_cnum = 164}, ()))
20+
|}]
21+
22+
class ['a] c : here:[%call_pos] -> 'a -> object
23+
method here : lexing_position * 'a
24+
end = fun ~(here : [%call_pos]) a -> object
25+
method here = here, a
26+
end
27+
28+
[%%expect{|
29+
class ['a] c :
30+
here:[%call_pos] -> 'a -> object method here : lexing_position * 'a end
31+
|}]
32+
33+
let obj = (() |> new c |> new c)
34+
35+
let second_here = fst obj#here
36+
let first_here = fst (snd obj#here)#here
37+
38+
39+
[%%expect{|
40+
val obj : unit c c = <obj>
41+
val second_here : lexing_position =
42+
{pos_fname = ""; pos_lnum = 1; pos_bol = 710; pos_cnum = 736}
43+
val first_here : lexing_position =
44+
{pos_fname = ""; pos_lnum = 1; pos_bol = 710; pos_cnum = 727}
45+
|}]

typing/typecore.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7621,7 +7621,7 @@ and type_apply_arg env ~app_loc ~funct ~index ~position_and_mode ~partial_app (l
76217621
let arg = type_option_none env (instance ty_arg) Location.none in
76227622
(lbl, Arg (arg, Mode.Value.legacy, sort_arg))
76237623
| Position _ ->
7624-
let arg = src_pos (Location.ghostify app_loc) [] env in
7624+
let arg = src_pos (Location.ghostify funct.exp_loc) [] env in
76257625
(lbl, Arg (arg, Mode.Value.legacy, sort_arg))
76267626
| Labelled _ | Nolabel -> assert false)
76277627
| Omitted _ as arg -> (lbl, arg)

0 commit comments

Comments
 (0)