Skip to content

Commit 28ce921

Browse files
committed
Apply position arguments when expected type is nothing, refactor creating src_pos exprs
1 parent aa3cb65 commit 28ce921

File tree

2 files changed

+41
-20
lines changed

2 files changed

+41
-20
lines changed

ocaml/testsuite/tests/typing-implicit-source-positions/implicit_argument.ml

+25-3
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ let _ = f ();;
1313
{pos_fname = ""; pos_lnum = 1; pos_bol = 151; pos_cnum = 159}
1414
|}]
1515

16+
let j = (f : unit -> lexing_position);;
17+
[%%expect{|
18+
val j : unit -> lexing_position = <fun>
19+
|}]
20+
1621
let g = fun ~(a:[%src_pos]) ?(c = 0) ~(b:[%src_pos]) () -> a, b
1722
[%%expect{|
1823
val g :
@@ -23,8 +28,8 @@ val g :
2328
let _ = g () ;;
2429
[%%expect{|
2530
- : lexing_position * lexing_position =
26-
({pos_fname = ""; pos_lnum = 1; pos_bol = 452; pos_cnum = 460},
27-
{pos_fname = ""; pos_lnum = 1; pos_bol = 452; pos_cnum = 460})
31+
({pos_fname = ""; pos_lnum = 1; pos_bol = 549; pos_cnum = 557},
32+
{pos_fname = ""; pos_lnum = 1; pos_bol = 549; pos_cnum = 557})
2833
|}]
2934

3035
let h ~(a:[%src_pos]) ~(b:[%src_pos]) () : lexing_position * lexing_position
@@ -44,6 +49,23 @@ val x : a:[%src_pos] -> unit -> lexing_position * lexing_position = <fun>
4449
let y = x ();;
4550
[%%expect{|
4651
val y : lexing_position * lexing_position =
47-
({pos_fname = ""; pos_lnum = 1; pos_bol = 1022; pos_cnum = 1030},
52+
({pos_fname = ""; pos_lnum = 1; pos_bol = 1119; pos_cnum = 1127},
4853
{pos_fname = "b"; pos_lnum = 0; pos_bol = 0; pos_cnum = -1})
4954
|}]
55+
56+
let k = (f : unit -> lexing_position);;
57+
[%%expect{|
58+
val k : unit -> lexing_position = <fun>
59+
|}]
60+
61+
let _ = j ();;
62+
[%%expect{|
63+
- : lexing_position =
64+
{pos_fname = ""; pos_lnum = 1; pos_bol = 267; pos_cnum = 276}
65+
|}]
66+
67+
let _ = k ();;
68+
[%%expect{|
69+
- : lexing_position =
70+
{pos_fname = ""; pos_lnum = 1; pos_bol = 1327; pos_cnum = 1336}
71+
|}]

ocaml/typing/typecore.ml

+16-17
Original file line numberDiff line numberDiff line change
@@ -611,6 +611,14 @@ let extract_option_type env ty =
611611
Tconstr(path, [ty], _) when Path.same path Predef.path_option -> ty
612612
| _ -> assert false
613613

614+
let src_pos loc attrs env =
615+
{ exp_desc = Texp_src_pos
616+
; exp_loc = loc
617+
; exp_extra = []
618+
; exp_type = instance Predef.type_lexing_position
619+
; exp_attributes = attrs
620+
; exp_env = env }
621+
614622
type record_extraction_result =
615623
| Record_type of Path.t * Path.t * Types.label_declaration list * record_representation
616624
| Not_a_record_type
@@ -5720,12 +5728,7 @@ and type_expect_
57205728
| _ -> raise (Error (loc, env, Probe_is_enabled_format))
57215729
end
57225730
| Pexp_extension ({ txt = "src_pos"; _ }, _) ->
5723-
rue {
5724-
exp_desc = Texp_src_pos;
5725-
exp_loc = loc; exp_extra = [];
5726-
exp_type = instance Predef.type_lexing_position;
5727-
exp_attributes = sexp.pexp_attributes;
5728-
exp_env = env }
5731+
rue (src_pos loc sexp.pexp_attributes env)
57295732
| Pexp_extension ext ->
57305733
raise (Error_forward (Builtin_attributes.error_of_extension ext))
57315734

@@ -6353,7 +6356,7 @@ and type_argument ?explanation ?recarg env (mode : expected_mode) sarg
63536356
in
63546357
match may_coerce with
63556358
Some (safe_expect, lv) ->
6356-
(* apply optional arguments when expected type is "" *)
6359+
(* apply omittable arguments when expected type is "" *)
63576360
(* we must be very careful about not breaking the semantics *)
63586361
if !Clflags.principal then begin_def ();
63596362
let exp_mode = Value_mode.newvar_below mode.mode in
@@ -6372,7 +6375,9 @@ and type_argument ?explanation ?recarg env (mode : expected_mode) sarg
63726375
(* CR layouts v5: change value assumption below when we allow
63736376
non-values in structures. *)
63746377
make_args ((l, Arg (ty, Sort.value)) :: args) ty_fun
6375-
(* TODO vding: Add a case for Position arguments? *)
6378+
| Tarrow ((l,_marg,_mret),_,ty_fun,_) when is_position l ->
6379+
let arg = src_pos (Location.ghostify sarg.pexp_loc) [] env in
6380+
make_args ((l, Arg (arg, Sort.value)) :: args) ty_fun
63766381
| Tarrow ((l,_,_),_,ty_res',_) when l = Nolabel || !Clflags.classic ->
63776382
List.rev args, ty_fun, no_labels ty_res'
63786383
| Tvar _ -> List.rev args, ty_fun, false
@@ -6558,15 +6563,9 @@ and type_apply_arg env ~app_loc ~funct ~index ~position ~partial_app (lbl, arg)
65586563
let arg = option_none env (instance ty_arg) Location.none in
65596564
(lbl, Arg (arg, Value_mode.global, sort_arg))
65606565
| Position _ ->
6561-
let arg = {
6562-
exp_desc = Texp_src_pos;
6563-
exp_loc = Location.ghostify app_loc;
6564-
exp_extra = [];
6565-
exp_type = instance Predef.type_lexing_position;
6566-
exp_attributes = [];
6567-
exp_env = env }
6568-
(* CR src_pos: Confirm that global value mode is correct *)
6569-
in (lbl, Arg (arg, Value_mode.global, sort_arg))
6566+
let arg = src_pos (Location.ghostify app_loc) [] env in
6567+
(* CR src_pos: Confirm that global value mode is correct *)
6568+
(lbl, Arg (arg, Value_mode.global, sort_arg))
65706569
| Labelled _ | Nolabel -> assert false)
65716570
| Omitted _ as arg -> (lbl, arg)
65726571

0 commit comments

Comments
 (0)