Skip to content

Commit 3b5e873

Browse files
committed
propagate context info for deprecated, and handle pipe without arguments
1 parent a3d443f commit 3b5e873

File tree

13 files changed

+121
-25
lines changed

13 files changed

+121
-25
lines changed

compiler/ml/builtin_attributes.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,11 +113,12 @@ let rec deprecated_of_attrs_with_migrate = function
113113
Some (string_of_opt_payload p, None)
114114
| _ :: tl -> deprecated_of_attrs_with_migrate tl
115115

116-
let check_deprecated loc attrs s =
116+
let check_deprecated ?deprecated_context loc attrs s =
117117
match deprecated_of_attrs_with_migrate attrs with
118118
| None -> ()
119119
| Some (txt, migration_template) ->
120-
!Cmt_utils.record_deprecated_used loc txt migration_template;
120+
!Cmt_utils.record_deprecated_used
121+
?deprecated_context loc txt migration_template;
121122
Location.deprecated loc (cat s txt)
122123

123124
let check_deprecated_inclusion ~def ~use loc attrs1 attrs2 s =

compiler/ml/builtin_attributes.mli

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,12 @@
2727
ocaml.boxed / ocaml.unboxed
2828
*)
2929

30-
val check_deprecated : Location.t -> Parsetree.attributes -> string -> unit
30+
val check_deprecated :
31+
?deprecated_context:Cmt_utils.deprecated_used_context ->
32+
Location.t ->
33+
Parsetree.attributes ->
34+
string ->
35+
unit
3136
val check_deprecated_inclusion :
3237
def:Location.t ->
3338
use:Location.t ->

compiler/ml/cmt_format.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,8 +166,8 @@ let add_saved_type b = saved_types := b :: !saved_types
166166
let get_saved_types () = !saved_types
167167
let set_saved_types l = saved_types := l
168168

169-
let record_deprecated_used source_loc deprecated_text migration_template =
170-
deprecated_used := {Cmt_utils.source_loc; deprecated_text; migration_template} :: !deprecated_used
169+
let record_deprecated_used ?deprecated_context source_loc deprecated_text migration_template =
170+
deprecated_used := {Cmt_utils.source_loc; deprecated_text; migration_template; context = deprecated_context} :: !deprecated_used
171171

172172
let _ = Cmt_utils.record_deprecated_used := record_deprecated_used
173173

compiler/ml/cmt_format.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,11 @@ val record_value_dependency :
113113
Types.value_description -> Types.value_description -> unit
114114

115115
val record_deprecated_used :
116-
Location.t -> string -> Parsetree.expression option -> unit
116+
?deprecated_context:Cmt_utils.deprecated_used_context ->
117+
Location.t ->
118+
string ->
119+
Parsetree.expression option ->
120+
unit
117121

118122
(*
119123

compiler/ml/cmt_utils.ml

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,19 @@
1+
type deprecated_used_context = FunctionCall
2+
13
type deprecated_used = {
24
source_loc: Location.t;
35
deprecated_text: string;
46
migration_template: Parsetree.expression option;
7+
context: deprecated_used_context option;
58
}
69

710
type cmt_extra_info = {deprecated_used: deprecated_used list}
811

912
let record_deprecated_used :
10-
(Location.t -> string -> Parsetree.expression option -> unit) ref =
11-
ref (fun _ _ _ -> ())
13+
(?deprecated_context:deprecated_used_context ->
14+
Location.t ->
15+
string ->
16+
Parsetree.expression option ->
17+
unit)
18+
ref =
19+
ref (fun ?deprecated_context _ _ _ -> ignore deprecated_context)

compiler/ml/typecore.ml

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2222,28 +2222,30 @@ type lazy_args =
22222222
(Asttypes.Noloc.arg_label * (unit -> Typedtree.expression) option) list
22232223
22242224
type targs = (Asttypes.Noloc.arg_label * Typedtree.expression option) list
2225-
let rec type_exp ~context ?recarg env sexp =
2225+
let rec type_exp ?deprecated_context ~context ?recarg env sexp =
22262226
(* We now delegate everything to type_expect *)
2227-
type_expect ~context ?recarg env sexp (newvar ())
2227+
type_expect ?deprecated_context ~context ?recarg env sexp (newvar ())
22282228
22292229
(* Typing of an expression with an expected type.
22302230
This provide better error messages, and allows controlled
22312231
propagation of return type information.
22322232
In the principal case, [type_expected'] may be at generic_level.
22332233
*)
22342234
2235-
and type_expect ~context ?in_function ?recarg env sexp ty_expected =
2235+
and type_expect ?deprecated_context ~context ?in_function ?recarg env sexp
2236+
ty_expected =
22362237
let previous_saved_types = Cmt_format.get_saved_types () in
22372238
let exp =
22382239
Builtin_attributes.warning_scope sexp.pexp_attributes (fun () ->
2239-
type_expect_ ~context ?in_function ?recarg env sexp ty_expected)
2240+
type_expect_ ?deprecated_context ~context ?in_function ?recarg env sexp
2241+
ty_expected)
22402242
in
22412243
Cmt_format.set_saved_types
22422244
(Cmt_format.Partial_expression exp :: previous_saved_types);
22432245
exp
22442246
2245-
and type_expect_ ~context ?in_function ?(recarg = Rejected) env sexp ty_expected
2246-
=
2247+
and type_expect_ ?deprecated_context ~context ?in_function ?(recarg = Rejected)
2248+
env sexp ty_expected =
22472249
let loc = sexp.pexp_loc in
22482250
(* Record the expression type before unifying it with the expected type *)
22492251
let rue exp =
@@ -2260,7 +2262,9 @@ and type_expect_ ~context ?in_function ?(recarg = Rejected) env sexp ty_expected
22602262
in
22612263
match sexp.pexp_desc with
22622264
| Pexp_ident lid ->
2263-
let path, desc = Typetexp.find_value env lid.loc lid.txt in
2265+
let path, desc =
2266+
Typetexp.find_value ?deprecated_context env lid.loc lid.txt
2267+
in
22642268
(if !Clflags.annotations then
22652269
let dloc = desc.Types.val_loc in
22662270
let annot =
@@ -2398,7 +2402,9 @@ and type_expect_ ~context ?in_function ?(recarg = Rejected) env sexp ty_expected
23982402
assert (sargs <> []);
23992403
begin_def ();
24002404
(* one more level for non-returning functions *)
2401-
let funct = type_exp ~context:None env sfunct in
2405+
let funct =
2406+
type_exp ~deprecated_context:FunctionCall ~context:None env sfunct
2407+
in
24022408
let ty = instance env funct.exp_type in
24032409
end_def ();
24042410
wrap_trace_gadt_instances env (lower_args env []) ty;

compiler/ml/typetexp.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,13 @@ let find_all_constructors =
131131
let find_all_labels =
132132
find_component Env.lookup_all_labels (fun lid -> Unbound_label lid)
133133

134-
let find_value env loc lid =
134+
let find_value ?deprecated_context env loc lid =
135135
Env.check_value_name (Longident.last lid) loc;
136136
let ((path, decl) as r) =
137137
find_component Env.lookup_value (fun lid -> Unbound_value lid) env loc lid
138138
in
139-
Builtin_attributes.check_deprecated loc decl.val_attributes (Path.name path);
139+
Builtin_attributes.check_deprecated ?deprecated_context loc
140+
decl.val_attributes (Path.name path);
140141
r
141142

142143
let lookup_module ?(load = false) env loc lid =

compiler/ml/typetexp.mli

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ val find_all_labels :
8989
Longident.t ->
9090
(label_description * (unit -> unit)) list
9191
val find_value :
92-
Env.t -> Location.t -> Longident.t -> Path.t * value_description
92+
?deprecated_context:Cmt_utils.deprecated_used_context ->
93+
Env.t ->
94+
Location.t ->
95+
Longident.t ->
96+
Path.t * value_description
9397
val find_module :
9498
Env.t -> Location.t -> Longident.t -> Path.t * module_declaration
9599
val lookup_module : ?load:bool -> Env.t -> Location.t -> Longident.t -> Path.t

tests/tools_tests/src/expected/FileToMigrate.res.expected

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ let someNiceString2 = String.slice(String.slice("abcdefg", ~start=0, ~end=1), ~s
44

55
let someNiceString3 = "abcdefg"->String.slice(~start=2, ~end=5)
66

7+
let shift1 = Array.shift([1, 2, 3])
8+
let shift2 = [1, 2, 3]->Array.shift
9+
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
@send
22
external slice: (string, ~from: int, ~to_: int) => string = "slice"
3+
4+
@send
5+
external shift: array<'a> => option<'a> = "shift"

0 commit comments

Comments
 (0)