Skip to content

Commit bce8b40

Browse files
committed
add injectable sourceLoc arg to functions
1 parent e4d98a7 commit bce8b40

File tree

3 files changed

+75
-3
lines changed

3 files changed

+75
-3
lines changed

compiler/ml/predef.ml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ and ident_result = ident_create "result"
5151

5252
and ident_dict = ident_create "dict"
5353

54+
and ident_source_loc = ident_create "sourceLoc"
55+
5456
and ident_bigint = ident_create "bigint"
5557

5658
and ident_lazy_t = ident_create "lazy_t"
@@ -98,6 +100,8 @@ and path_result = Pident ident_result
98100

99101
and path_dict = Pident ident_dict
100102

103+
and path_source_loc = Pident ident_source_loc
104+
101105
and path_bigint = Pident ident_bigint
102106

103107
and path_lazy_t = Pident ident_lazy_t
@@ -318,6 +322,32 @@ let common_initial_env add_type add_extension empty_env =
318322
],
319323
Record_regular );
320324
}
325+
and decl_source_loc =
326+
{
327+
decl_abstr with
328+
type_kind =
329+
(let mk_field name typ =
330+
{
331+
ld_id = ident_create name;
332+
ld_attributes = [];
333+
ld_loc = Location.none;
334+
ld_mutable = Immutable;
335+
ld_optional = false;
336+
ld_type = typ;
337+
}
338+
in
339+
Type_record
340+
( [
341+
(* __FILE__ *)
342+
mk_field "filename" type_string;
343+
(* __MODULE__ *)
344+
mk_field "module_" type_string;
345+
(* __POS__ *)
346+
mk_field "pos"
347+
(newgenty (Ttuple [type_string; type_int; type_int; type_int]));
348+
],
349+
Record_regular ));
350+
}
321351
and decl_uncurried =
322352
let tvar1 = newgenvar () in
323353
{
@@ -402,6 +432,7 @@ let common_initial_env add_type add_extension empty_env =
402432
|> add_type ident_array decl_array
403433
|> add_type ident_list decl_list
404434
|> add_type ident_dict decl_dict
435+
|> add_type ident_source_loc decl_source_loc
405436
|> add_type ident_unknown decl_unknown
406437
|> add_exception ident_undefined_recursive_module
407438
[newgenty (Ttuple [type_string; type_int; type_int])]

compiler/ml/predef.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ val path_list : Path.t
4646
val path_option : Path.t
4747
val path_result : Path.t
4848
val path_dict : Path.t
49+
val path_source_loc : Path.t
4950

5051
val path_bigint : Path.t
5152
val path_lazy_t : Path.t

compiler/ml/typecore.ml

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2223,6 +2223,41 @@ let not_function env ty =
22232223
let ls, tvar = list_labels env ty in
22242224
ls = [] && not tvar
22252225
2226+
let rec find_injectable_source_loc_arg t =
2227+
match t.desc with
2228+
| Tarrow (Labelled n, {desc = Tconstr (p, [], _)}, _, _, _)
2229+
when Path.same p Predef.path_source_loc ->
2230+
Some n
2231+
| Tarrow (_, _, t, _, _) -> find_injectable_source_loc_arg t
2232+
| _ -> None
2233+
2234+
let mk_source_loc_field ~parent_loc field_name ident_name =
2235+
( Location.mknoloc (Longident.Lident field_name),
2236+
Ast_helper.Exp.ident
2237+
~loc:{parent_loc with loc_ghost = true}
2238+
(Location.mknoloc (Longident.Lident ident_name)),
2239+
false )
2240+
2241+
let expand_injectable_args ~(apply_expr : Parsetree.expression) ~exp_type
2242+
(sargs : sargs) =
2243+
match find_injectable_source_loc_arg exp_type with
2244+
| None -> sargs
2245+
| Some injectable_source_loc_arg_label_name ->
2246+
let mk_source_loc_field =
2247+
mk_source_loc_field ~parent_loc:apply_expr.pexp_loc
2248+
in
2249+
sargs
2250+
@ [
2251+
( Labelled (Location.mknoloc injectable_source_loc_arg_label_name),
2252+
Ast_helper.Exp.record
2253+
[
2254+
mk_source_loc_field "filename" "__FILE__";
2255+
mk_source_loc_field "module_" "__MODULE__";
2256+
mk_source_loc_field "pos" "__POS__";
2257+
]
2258+
None );
2259+
]
2260+
22262261
type lazy_args =
22272262
(Asttypes.Noloc.arg_label * (unit -> Typedtree.expression) option) list
22282263
@@ -2412,7 +2447,9 @@ and type_expect_ ?type_clash_context ?in_function ?(recarg = Rejected) env sexp
24122447
let args, ty_res, fully_applied =
24132448
match translate_unified_ops env funct sargs with
24142449
| Some (targs, result_type) -> (targs, result_type, true)
2415-
| None -> type_application ?type_clash_context total_app env funct sargs
2450+
| None ->
2451+
type_application ~apply_expr:sexp ?type_clash_context total_app env
2452+
funct sargs
24162453
in
24172454
end_def ();
24182455
unify_var env (newvar ()) funct.exp_type;
@@ -3447,8 +3484,11 @@ and translate_unified_ops (env : Env.t) (funct : Typedtree.expression)
34473484
| _ -> None)
34483485
| _ -> None
34493486
3450-
and type_application ?type_clash_context total_app env funct (sargs : sargs) :
3451-
targs * Types.type_expr * bool =
3487+
and type_application ?type_clash_context ~apply_expr total_app env funct
3488+
(sargs : sargs) : targs * Types.type_expr * bool =
3489+
let sargs =
3490+
expand_injectable_args ~apply_expr ~exp_type:funct.exp_type sargs
3491+
in
34523492
let result_type omitted ty_fun =
34533493
List.fold_left
34543494
(fun ty_fun (l, ty, lv) -> newty2 lv (Tarrow (l, ty, ty_fun, Cok, None)))

0 commit comments

Comments
 (0)