Skip to content

Commit a44b60c

Browse files
committed
update reanalyze exception tracking of stdlib
Fixes rescript-lang/rescript-core#71
1 parent 7273be7 commit a44b60c

File tree

5 files changed

+109
-170
lines changed

5 files changed

+109
-170
lines changed

analysis/reanalyze/src/Exn.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ let endOfFile = "End_of_file"
88
let exit = "exit"
99
let failure = "Failure"
1010
let invalidArgument = "Invalid_argument"
11-
let jsExnError = "Js.Exn.Error"
11+
let jsExnError = "Exn.Error"
1212
let matchFailure = "Match_failure"
1313
let notFound = "Not_found"
1414
let sysError = "Sys_error"

analysis/reanalyze/src/ExnLib.ml

+66-138
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,6 @@
11
let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
22
let table = Hashtbl.create 15 in
33
let open Exn in
4-
let array =
5-
[
6-
("get", [invalidArgument]);
7-
("set", [invalidArgument]);
8-
("make", [invalidArgument]);
9-
("init", [invalidArgument]);
10-
("make_matrix", [invalidArgument]);
11-
("fill", [invalidArgument]);
12-
("blit", [invalidArgument]);
13-
("iter2", [invalidArgument]);
14-
("map2", [invalidArgument]);
15-
]
16-
in
174
let beltArray = [("getExn", [assertFailure]); ("setExn", [assertFailure])] in
185
let beltList =
196
[("getExn", [notFound]); ("headExn", [notFound]); ("tailExn", [notFound])]
@@ -49,91 +36,10 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
4936
("either", [decodeError]);
5037
]
5138
in
52-
let buffer =
53-
[
54-
("sub", [invalidArgument]);
55-
("blit", [invalidArgument]);
56-
("nth", [invalidArgument]);
57-
("add_substitute", [notFound]);
58-
("add_channel", [endOfFile]);
59-
("truncate", [invalidArgument]);
60-
]
61-
in
62-
let bytes =
63-
[
64-
("get", [invalidArgument]);
65-
("set", [invalidArgument]);
66-
("create", [invalidArgument]);
67-
("make", [invalidArgument]);
68-
("init", [invalidArgument]);
69-
("sub", [invalidArgument]);
70-
("sub_string", [invalidArgument]);
71-
("extend", [invalidArgument]);
72-
("fill", [invalidArgument]);
73-
("blit", [invalidArgument]);
74-
("blit_string", [invalidArgument]);
75-
(* ("concat", [invalidArgument]), if longer than {!Sys.max_string_length}
76-
("cat", [invalidArgument]), if longer than {!Sys.max_string_length}
77-
("escaped", [invalidArgument]), if longer than {!Sys.max_string_length} *)
78-
("index", [notFound]);
79-
("rindex", [notFound]);
80-
("index_from", [invalidArgument; notFound]);
81-
("index_from_opt", [invalidArgument]);
82-
("rindex_from", [invalidArgument; notFound]);
83-
("rindex_from_opt", [invalidArgument]);
84-
("contains_from", [invalidArgument]);
85-
("rcontains_from", [invalidArgument]);
86-
]
87-
in
88-
let filename =
89-
[
90-
("chop_extension", [invalidArgument]);
91-
("temp_file", [sysError]);
92-
("open_temp_file", [sysError]);
93-
]
94-
in
95-
let hashtbl = [("find", [notFound])] in
96-
let list =
97-
[
98-
("hd", [failure]);
99-
("tl", [failure]);
100-
("nth", [failure; invalidArgument]);
101-
("nth_opt", [invalidArgument]);
102-
("init", [invalidArgument]);
103-
("iter2", [invalidArgument]);
104-
("map2", [invalidArgument]);
105-
("fold_left2", [invalidArgument]);
106-
("fold_right2", [invalidArgument]);
107-
("for_all2", [invalidArgument]);
108-
("exists2", [invalidArgument]);
109-
("find", [notFound]);
110-
("assoc", [notFound]);
111-
("combine", [invalidArgument]);
112-
]
113-
in
114-
let string =
115-
[
116-
("get", [invalidArgument]);
117-
("set", [invalidArgument]);
118-
("create", [invalidArgument]);
119-
("make", [invalidArgument]);
120-
("init", [invalidArgument]);
121-
("sub", [invalidArgument]);
122-
("fill", [invalidArgument]);
123-
(* ("concat", [invalidArgument]), if longer than {!Sys.max_string_length}
124-
("escaped", [invalidArgument]), if longer than {!Sys.max_string_length} *)
125-
("index", [notFound]);
126-
("rindex", [notFound]);
127-
("index_from", [invalidArgument; notFound]);
128-
("index_from_opt", [invalidArgument]);
129-
("rindex_from", [invalidArgument; notFound]);
130-
("rindex_from_opt", [invalidArgument]);
131-
("contains_from", [invalidArgument]);
132-
("rcontains_from", [invalidArgument]);
133-
]
134-
in
13539
let stdlib =
13640
[
41+
("panic", [jsExnError]);
42+
("assertEqual", [jsExnError]);
13743
("invalid_arg", [invalidArgument]);
13844
("failwith", [failure]);
13945
("/", [divisionByZero]);
@@ -142,29 +48,40 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
14248
("bool_of_string", [invalidArgument]);
14349
("int_of_string", [failure]);
14450
("float_of_string", [failure]);
145-
("read_int", [failure]);
146-
("output", [invalidArgument]);
147-
("close_out", [sysError]);
148-
("input_char", [endOfFile]);
149-
("input_line", [endOfFile]);
150-
("input", [invalidArgument]);
151-
("really_input", [endOfFile; invalidArgument]);
152-
("really_input_string", [endOfFile]);
153-
("input_byte", [endOfFile]);
154-
("input_binary_int", [endOfFile]);
155-
("close_in", [sysError]);
156-
("exit", [exit]);
15751
]
15852
in
159-
let str =
53+
let stdlibBigInt = [("fromStringExn", [jsExnError])] in
54+
let stdlibError = [("raise", [jsExnError])] in
55+
let stdlibExn =
56+
[
57+
("raiseError", [jsExnError]);
58+
("raiseEvalError", [jsExnError]);
59+
("raiseRangeError", [jsExnError]);
60+
("raiseReferenceError", [jsExnError]);
61+
("raiseSyntaxError", [jsExnError]);
62+
("raiseTypeError", [jsExnError]);
63+
("raiseUriError", [jsExnError]);
64+
]
65+
in
66+
let stdlibJson =
16067
[
161-
("search_forward", [notFound]);
162-
("search_backward", [notFound]);
163-
("matched_group", [notFound]);
164-
("group_beginning", [notFound; invalidArgument]);
165-
("group_end", [notFound; invalidArgument]);
68+
("parseExn", [jsExnError]);
69+
("parseExnWithReviver", [jsExnError]);
70+
("stringifyAny", [jsExnError]);
71+
("stringifyAnyWithIndent", [jsExnError]);
72+
("stringifyAnyWithReplacer", [jsExnError]);
73+
("stringifyAnyWithReplacerAndIndent", [jsExnError]);
74+
("stringifyAnyWithFilter", [jsExnError]);
75+
("stringifyAnyWithFilterAndIndent", [jsExnError]);
16676
]
16777
in
78+
let stdlibList =
79+
[("headExn", [notFound]); ("tailExn", [notFound]); ("getExn", [notFound])]
80+
in
81+
let stdlibNull = [("getExn", [invalidArgument])] in
82+
let stdlibNullable = [("getExn", [invalidArgument])] in
83+
let stdlibOption = [("getExn", [jsExnError])] in
84+
let stdlibResult = [("getExn", [notFound])] in
16885
let yojsonBasic = [("from_string", [yojsonJsonError])] in
16986
let yojsonBasicUtil =
17087
[
@@ -183,7 +100,6 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
183100
]
184101
in
185102
[
186-
("Array", array);
187103
("Belt.Array", beltArray);
188104
("Belt_Array", beltArray);
189105
("Belt.List", beltList);
@@ -206,6 +122,11 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
206122
("Belt_MutableMapString", beltMutableMap);
207123
("Belt.MutableQueue", beltMutableQueue);
208124
("Belt_MutableQueue", beltMutableQueue);
125+
("Belt_MutableSetInt", beltMutableSet);
126+
("Belt_MutableSetString", beltMutableSet);
127+
("Belt.MutableSet", beltMutableSet);
128+
("Belt.MutableSet.Int", beltMutableSet);
129+
("Belt.MutableSet.String", beltMutableSet);
209130
("Belt.Option", beltOption);
210131
("Belt_Option", beltOption);
211132
("Belt.Result", beltResult);
@@ -218,35 +139,42 @@ let raisesLibTable : (Name.t, Exceptions.t) Hashtbl.t =
218139
("Belt_Set.String", beltSet);
219140
("Belt_SetInt", beltSet);
220141
("Belt_SetString", beltSet);
221-
("Belt.MutableSet", beltMutableSet);
222-
("Belt.MutableSet.Int", beltMutableSet);
223-
("Belt.MutableSet.String", beltMutableSet);
224-
("MutableSet", beltMutableSet);
225-
("MutableSet.Int", beltMutableSet);
226-
("MutableSet.String", beltMutableSet);
227-
("Belt_MutableSetInt", beltMutableSet);
228-
("Belt_MutableSetString", beltMutableSet);
229-
("Buffer", buffer);
230-
("Bytes", bytes);
142+
("BigInt", stdlibBigInt);
231143
("Char", [("chr", [invalidArgument])]);
232-
("Filename", filename);
233-
("Hashtbl", hashtbl);
144+
("Error", stdlibError);
145+
("Exn", stdlibExn);
234146
("Js.Json", [("parseExn", [jsExnError])]);
147+
("JSON", stdlibJson);
235148
("Json_decode", bsJson);
236149
("Json.Decode", bsJson);
237-
("List", list);
150+
("List", stdlibList);
151+
("MutableSet", beltMutableSet);
152+
("MutableSet.Int", beltMutableSet);
153+
("MutableSet.String", beltMutableSet);
154+
("Null", stdlibNull);
155+
("Nullable", stdlibNullable);
156+
("Option", stdlibOption);
238157
("Pervasives", stdlib);
158+
("Result", stdlibResult);
239159
("Stdlib", stdlib);
240-
("Stdlib.Array", array);
241-
("Stdlib.Buffer", buffer);
242-
("Stdlib.Bytes", bytes);
243-
("Stdlib.Filename", filename);
244-
("Stdlib.Hashtbl", hashtbl);
245-
("Stdlib.List", list);
246-
("Stdlib.Str", str);
247-
("Stdlib.String", string);
248-
("Str", str);
249-
("String", string);
160+
("Stdlib_BigInt", stdlibBigInt);
161+
("Stdlib.BigInt", stdlibBigInt);
162+
("Stdlib_Error", stdlibError);
163+
("Stdlib.Error", stdlibError);
164+
("Stdlib_Exn", stdlibExn);
165+
("Stdlib.Exn", stdlibExn);
166+
("Stdlib_JSON", stdlibJson);
167+
("Stdlib.JSON", stdlibJson);
168+
("Stdlib_List", stdlibList);
169+
("Stdlib.List", stdlibList);
170+
("Stdlib_Null", stdlibNull);
171+
("Stdlib.Null", stdlibNull);
172+
("Stdlib_Nullable", stdlibNullable);
173+
("Stdlib.Nullable", stdlibNullable);
174+
("Stdlib_Option", stdlibOption);
175+
("Stdlib.Option", stdlibOption);
176+
("Stdlib_Result", stdlibResult);
177+
("Stdlib.Result", stdlibResult);
250178
("Yojson.Basic", yojsonBasic);
251179
("Yojson.Basic.Util", yojsonBasicUtil);
252180
]

tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt

+21-21
Original file line numberDiff line numberDiff line change
@@ -42,51 +42,51 @@
4242

4343
Exception Analysis
4444
Exn.res:73:5-24
45-
parse_json_from_file might raise Error (Exn.res:76:4) and is not annotated with @raises(Error)
45+
parse_json_from_file might raise Error (Exn.res:75:34) and is not annotated with @raises(Error)
4646

4747
Exception Analysis
48-
Exn.res:82:5-11
49-
reRaise might raise B (Exn.res:84:19) and is not annotated with @raises(B)
48+
Exn.res:80:5-11
49+
reRaise might raise B (Exn.res:82:19) and is not annotated with @raises(B)
5050

5151
Exception Analysis
52-
Exn.res:93:5-22
53-
raiseInInternalLet might raise A (Exn.res:94:14) and is not annotated with @raises(A)
52+
Exn.res:91:5-22
53+
raiseInInternalLet might raise A (Exn.res:92:14) and is not annotated with @raises(A)
5454

5555
Exception Analysis
56-
Exn.res:98:5-16
57-
indirectCall might raise Not_found (Exn.res:98:31) and is not annotated with @raises(Not_found)
56+
Exn.res:96:5-16
57+
indirectCall might raise Not_found (Exn.res:96:25) and is not annotated with @raises(Not_found)
5858

5959
Exception Analysis
60-
Exn.res:124:5-16
61-
severalCases might raise Failure (Exn.res:126:13 Exn.res:127:13 Exn.res:128:15) and is not annotated with @raises(Failure)
60+
Exn.res:121:5-16
61+
severalCases might raise Failure (Exn.res:123:13 Exn.res:124:13 Exn.res:125:15) and is not annotated with @raises(Failure)
6262

6363
Exception Analysis
64-
Exn.res:136:5-23
64+
Exn.res:133:5-23
6565
redundantAnnotation raises nothing and is annotated with redundant @raises(Invalid_argument)
6666

6767
Exception Analysis
68-
Exn.res:138:5-6
69-
_x might raise A (Exn.res:138:9) and is not annotated with @raises(A)
68+
Exn.res:135:5-6
69+
_x might raise A (Exn.res:135:9) and is not annotated with @raises(A)
7070

7171
Exception Analysis
72-
Exn.res:140:5
73-
_ might raise A (Exn.res:140:8) and is not annotated with @raises(A)
72+
Exn.res:137:5
73+
_ might raise A (Exn.res:137:8) and is not annotated with @raises(A)
7474

7575
Exception Analysis
76-
Exn.res:142:5-6
77-
() might raise A (Exn.res:142:9) and is not annotated with @raises(A)
76+
Exn.res:139:5-6
77+
() might raise A (Exn.res:139:9) and is not annotated with @raises(A)
7878

7979
Exception Analysis
80-
Exn.res:144:1-16
81-
Toplevel expression might raise Not_found (Exn.res:144:0) and is not annotated with @raises(Not_found)
80+
Exn.res:141:1-16
81+
Toplevel expression might raise Not_found (Exn.res:141:0) and is not annotated with @raises(Not_found)
8282

8383
Exception Analysis
84-
Exn.res:154:45-46
84+
Exn.res:151:46-47
8585
expression does not raise and is annotated with redundant @doesNotRaise
8686

8787
Exception Analysis
88-
Exn.res:154:5-21
89-
onResultPipeWrong might raise Assert_failure (Exn.res:154:48) and is not annotated with @raises(Assert_failure)
88+
Exn.res:151:5-21
89+
onResultPipeWrong might raise Assert_failure (Exn.res:151:50) and is not annotated with @raises(Assert_failure)
9090

9191
Exception Analysis
9292
ExnA.res:1:5-7

tests/analysis_tests/tests-reanalyze/deadcode/src/exception/Exn.res

+7-10
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,8 @@ exception Error(string, string, int)
7272

7373
let parse_json_from_file = s => {
7474
switch 34 {
75-
| exception Error(p1, p2, e) =>
76-
raise(Error(p1, p2, e))
77-
| v =>
78-
v
75+
| exception Error(p1, p2, e) => raise(Error(p1, p2, e))
76+
| v => v
7977
}
8078
}
8179

@@ -95,15 +93,14 @@ let raiseInInternalLet = b => {
9593
a + 34
9694
}
9795

98-
let indirectCall = () => () |> raisesWithAnnotaion
96+
let indirectCall = () => raisesWithAnnotaion()
9997

100-
@raises(Invalid_argument)
10198
let array = a => a[2]
10299

103100
let id = x => x
104101

105102
let tryChar = v => {
106-
try id(Char.chr(v)) |> ignore catch {
103+
try ignore(id(Char.chr(v))) catch {
107104
| _ => ()
108105
}
109106
42
@@ -113,12 +110,12 @@ let tryChar = v => {
113110
let raiseAtAt = () => \"@@"(raise, Not_found)
114111

115112
@raises(Not_found)
116-
let raisePipe = Not_found |> raise
113+
let raisePipe = raise(Not_found)
117114

118115
@raises(Not_found)
119116
let raiseArrow = Not_found->raise
120117

121-
@raises(Js.Exn.Error)
118+
@raises(Exn.Error)
122119
let bar = () => Js.Json.parseExn("!!!")
123120

124121
let severalCases = cases =>
@@ -151,4 +148,4 @@ let onResult = () => @doesNotRaise Belt.Array.getExn([], 0)
151148

152149
let onFunctionPipe = () => []->(@doesNotRaise Belt.Array.getExn)(0)
153150

154-
let onResultPipeWrong = () => @doesNotRaise []->Belt.Array.getExn(0)
151+
let onResultPipeWrong = () => (@doesNotRaise [])->Belt.Array.getExn(0)

0 commit comments

Comments
 (0)