Skip to content

Commit 245a6a3

Browse files
committed
support value references
1 parent 373d71e commit 245a6a3

File tree

8 files changed

+55
-2
lines changed

8 files changed

+55
-2
lines changed

compiler/ml/cmt_utils.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
type deprecated_used_context = FunctionCall
1+
type deprecated_used_context = FunctionCall | Reference
22

33
type deprecated_used = {
44
source_loc: Location.t;

compiler/ml/typecore.ml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2263,7 +2263,12 @@ and type_expect_ ?deprecated_context ~context ?in_function ?(recarg = Rejected)
22632263
match sexp.pexp_desc with
22642264
| Pexp_ident lid ->
22652265
let path, desc =
2266-
Typetexp.find_value ?deprecated_context env lid.loc lid.txt
2266+
Typetexp.find_value
2267+
?deprecated_context:
2268+
(match deprecated_context with
2269+
| None -> Some Reference
2270+
| v -> v)
2271+
env lid.loc lid.txt
22672272
in
22682273
(if !Clflags.annotations then
22692274
let dloc = desc.Types.val_loc in

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,5 @@ let someNiceString3 = "abcdefg"->String.slice(~start=2, ~end=5)
77
let shift1 = Array.shift([1, 2, 3])
88
let shift2 = [1, 2, 3]->Array.shift
99

10+
let deprecatedThing1 = DeprecatedStuff.Constants.otherThing
11+

tests/tools_tests/src/migrate/DeprecatedStuff.res

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,9 @@ external slice: (string, ~from: int, ~to_: int) => string = "slice"
33

44
@send
55
external shift: array<'a> => option<'a> = "shift"
6+
7+
module Constants = {
8+
let otherThing = [2, 3]
9+
}
10+
11+
let deprecatedThing = [1, 2]

tests/tools_tests/src/migrate/DeprecatedStuff.resi

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,13 @@ external slice: (string, ~from: int, ~to_: int) => string = "slice"
1414
migrate: Array.shift(),
1515
})
1616
external shift: array<'a> => option<'a> = "shift"
17+
18+
module Constants: {
19+
let otherThing: array<int>
20+
}
21+
22+
@deprecated({
23+
reason: "Use `otherThing` instead.",
24+
migrate: DeprecatedStuff.Constants.otherThing,
25+
})
26+
let deprecatedThing: array<int>

tests/tools_tests/src/migrate/FileToMigrate.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,5 @@ let someNiceString3 = "abcdefg"->DeprecatedStuff.slice(~from=2, ~to_=5)
1010

1111
let shift1 = DeprecatedStuff.shift([1, 2, 3])
1212
let shift2 = [1, 2, 3]->DeprecatedStuff.shift
13+
14+
let deprecatedThing1 = DeprecatedStuff.deprecatedThing

tests/tools_tests/src/migrate/migrated/Migrated_StdlibMigration_Array.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,4 @@ let reduceRight2 = Array.reduceRight([1, 2, 3], 0, (acc, x) => acc + x)
166166

167167
let reduceRighti1 = [1, 2, 3]->Array.reduceRightWithIndex(0, (acc, x, i) => acc + x + i)
168168
let reduceRighti2 = Array.reduceRightWithIndex([1, 2, 3], 0, (acc, x, i) => acc + x + i)
169+

tools/src/tools.ml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,7 @@ module Migrate = struct
14861486
end
14871487

14881488
let makeMapper (deprecated_used : Cmt_utils.deprecated_used list) =
1489+
(* Function calls *)
14891490
let deprecated_function_calls =
14901491
deprecated_used
14911492
|> List.filter (fun (d : Cmt_utils.deprecated_used) ->
@@ -1499,6 +1500,22 @@ module Migrate = struct
14991500
deprecated_function_calls
15001501
|> List.iter (fun ({Cmt_utils.source_loc} as d) ->
15011502
Hashtbl.replace loc_to_deprecated_fn_call source_loc d);
1503+
1504+
(* References *)
1505+
let deprecated_references =
1506+
deprecated_used
1507+
|> List.filter (fun (d : Cmt_utils.deprecated_used) ->
1508+
match d.context with
1509+
| Some Reference -> true
1510+
| _ -> false)
1511+
in
1512+
let loc_to_deprecated_reference =
1513+
Hashtbl.create (List.length deprecated_references)
1514+
in
1515+
deprecated_references
1516+
|> List.iter (fun ({Cmt_utils.source_loc} as d) ->
1517+
Hashtbl.replace loc_to_deprecated_reference source_loc d);
1518+
15021519
let mapper =
15031520
{
15041521
Ast_mapper.default_mapper with
@@ -1513,6 +1530,16 @@ module Migrate = struct
15131530
expr =
15141531
(fun mapper exp ->
15151532
match exp with
1533+
| {pexp_desc = Pexp_ident {loc}}
1534+
when Hashtbl.mem loc_to_deprecated_reference loc -> (
1535+
(* Map references to their migration template. *)
1536+
let deprecated_info =
1537+
Hashtbl.find loc_to_deprecated_reference loc
1538+
in
1539+
Hashtbl.remove loc_to_deprecated_reference loc;
1540+
match deprecated_info.migration_template with
1541+
| None -> exp
1542+
| Some e -> e)
15161543
| {
15171544
pexp_desc =
15181545
Pexp_apply {funct = {pexp_loc = fn_loc}; args = source_args};

0 commit comments

Comments
 (0)