Skip to content

Commit 0a37396

Browse files
cknittclaude
andcommitted
Know the remaining stdlib and Belt throwing functions in exception analysis
Why: The table of throwing library functions in exn_lib.ml is kept by hand, and the Temporal `fromStringOrThrow` externals, the Intl `*Exn` externals, `Belt.MutableStack.popOrThrow` and the `getOrThrow`/`getExn` of `Belt.Map.Dict` and `Belt.Set.Dict` were missing from it, so calls to them were reported as throwing nothing. The table now lists them under every module path a call can resolve to, and `Belt_MutableSet` gets the entries that `Belt.MutableSet` already had. Reanalyze modified (exception analysis): Example: let plainDate = s => Temporal.PlainDate.fromStringOrThrow(s) @throws(JsExn) let locales = s => Intl.getCanonicalLocalesExn(s) Before (reanalyze): Exception Analysis StdlibTest.res:4:5-11 locales throws nothing and is annotated with redundant @throws(JsExn) After (reanalyze): Exception Analysis StdlibTest.res:1:5-13 plainDate might throw JsExn (StdlibTest.res:1:21) and is not annotated with @throws(JsExn) Review: Why not also list Temporal arithmetic, `Int.range` or `Date.toTemporalInstant`? They throw only for some arguments and their names don't say so; listing them would flag every call. The table keeps to functions named `*OrThrow`/`*Exn` plus the explicit throwers. Fixes #9182 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Kbf3gGhHjzBTXqSo8Dckpb Signed-off-by: Christoph Knittel <ck@cca.io>
1 parent ac73578 commit 0a37396

5 files changed

Lines changed: 131 additions & 1 deletion

File tree

‎analysis/reanalyze/src/exn_lib.ml‎

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ let raises_lib_table : (Name.t, Exceptions.t) Hashtbl.t =
3131
in
3232
let belt_set = [("getExn", [not_found]); ("getOrThrow", [not_found])] in
3333
let belt_mutable_set = belt_set in
34+
let belt_mutable_stack = [("popOrThrow", [not_found])] in
3435
let belt_option = [("getExn", [not_found]); ("getOrThrow", [not_found])] in
3536
let belt_result = [("getExn", [not_found]); ("getOrThrow", [not_found])] in
3637
let bs_json =
@@ -69,6 +70,13 @@ let raises_lib_table : (Name.t, Exceptions.t) Hashtbl.t =
6970
[("fromStringOrThrow", [js_exn]); ("fromFloatOrThrow", [js_exn])]
7071
in
7172
let stdlib_bool = [("fromStringOrThrow", [invalid_argument])] in
73+
let stdlib_intl =
74+
[
75+
("getCanonicalLocalesExn", [js_exn]);
76+
("getCanonicalLocalesManyExn", [js_exn]);
77+
("supportedValuesOfExn", [js_exn]);
78+
]
79+
in
7280
let stdlib_js_error =
7381
[
7482
("EvalError.throwWithMessage", [js_exn]);
@@ -94,6 +102,28 @@ let raises_lib_table : (Name.t, Exceptions.t) Hashtbl.t =
94102
let stdlib_nullable = [("getOrThrow", [invalid_argument])] in
95103
let stdlib_option = [("getOrThrow", [js_exn])] in
96104
let stdlib_result = [("getOrThrow", [js_exn])] in
105+
let stdlib_temporal = [("fromStringOrThrow", [js_exn])] in
106+
(* Each Temporal module is reachable as [Temporal.M], [Stdlib.Temporal.M],
107+
[Stdlib_Temporal.M] and [Stdlib_Temporal_M]. *)
108+
let temporal_modules =
109+
[
110+
"Duration";
111+
"Instant";
112+
"PlainDate";
113+
"PlainDateTime";
114+
"PlainMonthDay";
115+
"PlainTime";
116+
"PlainYearMonth";
117+
"ZonedDateTime";
118+
]
119+
|> List.concat_map (fun m ->
120+
[
121+
("Temporal." ^ m, stdlib_temporal);
122+
("Stdlib.Temporal." ^ m, stdlib_temporal);
123+
("Stdlib_Temporal." ^ m, stdlib_temporal);
124+
("Stdlib_Temporal_" ^ m, stdlib_temporal);
125+
])
126+
in
97127
let yojson_basic = [("from_string", [yojson_json_error])] in
98128
let yojson_basic_util =
99129
[
@@ -120,6 +150,9 @@ let raises_lib_table : (Name.t, Exceptions.t) Hashtbl.t =
120150
("Belt.Map.Int", belt_map);
121151
("Belt.Map.String", belt_map);
122152
("Belt_Map", belt_map);
153+
("Belt.Map.Dict", belt_map);
154+
("Belt_Map.Dict", belt_map);
155+
("Belt_MapDict", belt_map);
123156
("Belt_Map.Int", belt_map);
124157
("Belt_Map.String", belt_map);
125158
("Belt_MapInt", belt_map);
@@ -134,11 +167,16 @@ let raises_lib_table : (Name.t, Exceptions.t) Hashtbl.t =
134167
("Belt_MutableMapString", belt_mutable_map);
135168
("Belt.MutableQueue", belt_mutable_queue);
136169
("Belt_MutableQueue", belt_mutable_queue);
170+
("Belt_MutableSet", belt_mutable_set);
171+
("Belt_MutableSet.Int", belt_mutable_set);
172+
("Belt_MutableSet.String", belt_mutable_set);
137173
("Belt_MutableSetInt", belt_mutable_set);
138174
("Belt_MutableSetString", belt_mutable_set);
139175
("Belt.MutableSet", belt_mutable_set);
140176
("Belt.MutableSet.Int", belt_mutable_set);
141177
("Belt.MutableSet.String", belt_mutable_set);
178+
("Belt.MutableStack", belt_mutable_stack);
179+
("Belt_MutableStack", belt_mutable_stack);
142180
("Belt.Option", belt_option);
143181
("Belt_Option", belt_option);
144182
("Belt.Result", belt_result);
@@ -147,12 +185,16 @@ let raises_lib_table : (Name.t, Exceptions.t) Hashtbl.t =
147185
("Belt.Set.Int", belt_set);
148186
("Belt.Set.String", belt_set);
149187
("Belt_Set", belt_set);
188+
("Belt.Set.Dict", belt_set);
189+
("Belt_Set.Dict", belt_set);
190+
("Belt_SetDict", belt_set);
150191
("Belt_Set.Int", belt_set);
151192
("Belt_Set.String", belt_set);
152193
("Belt_SetInt", belt_set);
153194
("Belt_SetString", belt_set);
154195
("BigInt", stdlib_big_int);
155196
("Bool", stdlib_bool);
197+
("Intl", stdlib_intl);
156198
("JsError", stdlib_js_error);
157199
("JSON", stdlib_json);
158200
("Json_decode", bs_json);
@@ -171,6 +213,8 @@ let raises_lib_table : (Name.t, Exceptions.t) Hashtbl.t =
171213
("Stdlib.BigInt", stdlib_big_int);
172214
("Stdlib_Bool", stdlib_bool);
173215
("Stdlib.Bool", stdlib_bool);
216+
("Stdlib_Intl", stdlib_intl);
217+
("Stdlib.Intl", stdlib_intl);
174218
("Stdlib_JsError", stdlib_js_error);
175219
("Stdlib.JsError", stdlib_js_error);
176220
("Stdlib_JSON", stdlib_json);
@@ -188,6 +232,7 @@ let raises_lib_table : (Name.t, Exceptions.t) Hashtbl.t =
188232
("Yojson.Basic", yojson_basic);
189233
("Yojson.Basic.Util", yojson_basic_util);
190234
]
235+
@ temporal_modules
191236
|> List.iter (fun (name, group) ->
192237
group
193238
|> List.iter (fun (s, e) ->
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Exception analysis now knows the throwing Temporal `fromStringOrThrow`, Intl `*Exn`, `Belt.MutableStack.popOrThrow` and `Belt.Map.Dict`/`Belt.Set.Dict` `getOrThrow` functions.

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,5 +123,9 @@
123123
Exception Analysis
124124
ExternalTest.res:7:5-24
125125
bigIntFromStringExn2 might throw JsExn (ExternalTest.res:7:35) and is not annotated with @throws(JsExn)
126+
127+
Exception Analysis
128+
StdlibTest.res:67:5-34
129+
plainDateFromStringUnannotated might throw JsExn (StdlibTest.res:67:42) and is not annotated with @throws(JsExn)
126130

127-
Analysis reported 31 issues (Exception Analysis:31)
131+
Analysis reported 32 issues (Exception Analysis:32)

‎tests/analysis_tests/tests-reanalyze/deadcode/src/exception/BeltTest.res‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,30 @@ let mapGetExn7 = (s, k) => s->Belt.Map.getExn(k)
2929

3030
@throws(Not_found)
3131
let mapGetExn8 = (s, k) => s->Belt_Map.getExn(k)
32+
33+
@throws(Not_found)
34+
let stackPopOrThrow = s => Belt.MutableStack.popOrThrow(s)
35+
36+
@throws(Not_found)
37+
let directStackPopOrThrow = s => Belt_MutableStack.popOrThrow(s)
38+
39+
@throws(Not_found)
40+
let mapDictGetOrThrow = (m, k, ~cmp) => Belt.Map.Dict.getOrThrow(m, k, ~cmp)
41+
42+
@throws(Not_found)
43+
let mapDictGetExn = (m, k, ~cmp) => Belt.Map.Dict.getExn(m, k, ~cmp)
44+
45+
@throws(Not_found)
46+
let directMapDictGetOrThrow = (m, k, ~cmp) => Belt_MapDict.getOrThrow(m, k, ~cmp)
47+
48+
@throws(Not_found)
49+
let setDictGetOrThrow = (s, v, ~cmp) => Belt.Set.Dict.getOrThrow(s, v, ~cmp)
50+
51+
@throws(Not_found)
52+
let directSetDictGetOrThrow = (s, v, ~cmp) => Belt_SetDict.getOrThrow(s, v, ~cmp)
53+
54+
@throws(Not_found)
55+
let directMutableSetGetOrThrow = (s, v) => Belt_MutableSet.getOrThrow(s, v)
56+
57+
@throws(Not_found)
58+
let directMutableSetIntGetOrThrow = (s, v) => Belt_MutableSet.Int.getOrThrow(s, v)

‎tests/analysis_tests/tests-reanalyze/deadcode/src/exception/StdlibTest.res‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,56 @@ let bigIntFromStringExn = s => s->BigInt.fromStringOrThrow
1212

1313
@throws(JsExn)
1414
let jsonParseExn = s => s->JSON.parseOrThrow
15+
16+
@throws(JsExn)
17+
let durationFromStringOrThrow = s => Temporal.Duration.fromStringOrThrow(s)
18+
19+
@throws(JsExn)
20+
let instantFromStringOrThrow = s => Temporal.Instant.fromStringOrThrow(s)
21+
22+
@throws(JsExn)
23+
let plainDateFromStringOrThrow = s => Temporal.PlainDate.fromStringOrThrow(s)
24+
25+
@throws(JsExn)
26+
let plainDateTimeFromStringOrThrow = s => Temporal.PlainDateTime.fromStringOrThrow(s)
27+
28+
@throws(JsExn)
29+
let plainMonthDayFromStringOrThrow = s => Temporal.PlainMonthDay.fromStringOrThrow(s)
30+
31+
@throws(JsExn)
32+
let plainTimeFromStringOrThrow = s => Temporal.PlainTime.fromStringOrThrow(s)
33+
34+
@throws(JsExn)
35+
let plainYearMonthFromStringOrThrow = s => Temporal.PlainYearMonth.fromStringOrThrow(s)
36+
37+
@throws(JsExn)
38+
let zonedDateTimeFromStringOrThrow = s => Temporal.ZonedDateTime.fromStringOrThrow(s)
39+
40+
@throws(JsExn)
41+
let stdlibPlainDateFromStringOrThrow = s => Stdlib.Temporal.PlainDate.fromStringOrThrow(s)
42+
43+
@throws(JsExn)
44+
let openedPlainDateFromStringOrThrow = s => {
45+
open Temporal
46+
PlainDate.fromStringOrThrow(s)
47+
}
48+
49+
@throws(JsExn)
50+
let directPlainDateFromStringOrThrow = s => Stdlib_Temporal_PlainDate.fromStringOrThrow(s)
51+
52+
@throws(JsExn)
53+
let aliasPlainDateFromStringOrThrow = s => Stdlib_Temporal.PlainDate.fromStringOrThrow(s)
54+
55+
@throws(JsExn)
56+
let canonicalLocales = s => Intl.getCanonicalLocalesExn(s)
57+
58+
@throws(JsExn)
59+
let canonicalLocalesMany = s => Intl.getCanonicalLocalesManyExn(s)
60+
61+
@throws(JsExn)
62+
let supportedValuesOf = s => Intl.supportedValuesOfExn(s)
63+
64+
@throws(JsExn)
65+
let stdlibCanonicalLocales = s => Stdlib.Intl.getCanonicalLocalesExn(s)
66+
67+
let plainDateFromStringUnannotated = s => Temporal.PlainDate.fromStringOrThrow(s)

0 commit comments

Comments
 (0)