From d5177afd21cbed7f728b84ea9ccbd8b48450808c Mon Sep 17 00:00:00 2001 From: Christoph Knittel Date: Mon, 30 Nov 2026 19:51:23 +0000 Subject: [PATCH] Read @throws from library declarations in exception analysis Why: Exception analysis knew which library functions throw only from a hand-written table in exn_lib.ml, and the table fell behind. The Temporal `fromStringOrThrow` functions were never added (#9182), and neither were some Intl and Belt functions. Third-party libraries and a project's own externals had no way to declare their exceptions. The table is gone. At each call, the analysis now reads the `@throws` on the declaration of the called function. The `.cmi` keeps the attributes of externals and of `let`s in interfaces, so their `@throws` reaches calls in other modules and packages. A `let` in a module without an interface used to lose its attributes in the inferred signature. The type checker now copies its `@throws` there, as it already did for `@deprecated`. The stdlib and Belt annotate their throwing functions themselves. A guard in the reanalyze tests fails when a stdlib or Belt function named `*OrThrow` or `*Exn` has no `@throws`. Analysis modified (exceptions of a call to `f`, `Exception.Event` loop): Before (rule): throws(`f`) = values(`f`) when `f` is in the values table throws(`f`) = table(`f`) otherwise After (rule): throws(`f`) = declared(`f`) when the declaration has a `@throws` throws(`f`) = values(`f`) otherwise Signature modified (inferred signature of a module without an interface, `Typemod.type_structure`): Example: /** Finds the value. */ @throws(Not_found) let find = x => x > 0 ? x : throw(Not_found) Before (rescript): the signature in the .cmi let find: int => int After (rescript): @throws(Not_found) let find: int => int Reanalyze modified (calls to annotated externals): Example: // Lib.res @throws(JsExn) external parse: string => int = "parseInt" // App.res let parse = s => Lib.parse(s) @throws(JsExn) let parseAnnotated = s => Lib.parse(s) let date = () => Temporal.PlainDate.fromStringOrThrow("x") Before (reanalyze): Exception Analysis App.res:4:5-18 parseAnnotated throws nothing and is annotated with redundant @throws(JsExn) Analysis reported 1 issues (Exception Analysis:1) After (reanalyze): Exception Analysis App.res:1:5-9 parse might throw JsExn (App.res:1:17) and is not annotated with @throws(JsExn) Exception Analysis App.res:6:5-8 date might throw JsExn (App.res:6:17) and is not annotated with @throws(JsExn) Analysis reported 2 issues (Exception Analysis:2) Review: Why does the declaration win over the implementation? The values table holds the `@throws` on the implementation's `let`. When the interface has a `@throws`, that is what callers see, so it counts alone. When it has none, the implementation's counts, so an annotated `.res` behind an unannotated `.resi` still works. Review: Why does the inferred signature copy only `@throws` and `@deprecated`? A doc comment is an attribute too, and keeping it in the `.cmi` would make every edit of the comment recompile the module's dependents. A module with a `.resi` is unaffected, because its `.cmi` comes from the interface. Fixes #9182 Co-Authored-By: Claude Opus 5.5 Signed-off-by: Christoph Knittel --- analysis/reanalyze/src/exception.ml | 76 ++++--- analysis/reanalyze/src/exn.ml | 8 - analysis/reanalyze/src/exn.mli | 8 - analysis/reanalyze/src/exn_lib.ml | 200 ------------------ ...nalyze-throws-from-declarations.feature.md | 1 + compiler/typing/typemod.ml | 14 +- packages/@rescript/belt/src/Belt_Array.resi | 4 + packages/@rescript/belt/src/Belt_List.resi | 6 + packages/@rescript/belt/src/Belt_Map.resi | 2 + packages/@rescript/belt/src/Belt_MapDict.resi | 2 + packages/@rescript/belt/src/Belt_MapInt.resi | 2 + .../@rescript/belt/src/Belt_MapString.resi | 2 + .../@rescript/belt/src/Belt_MutableMap.resi | 4 + .../belt/src/Belt_MutableMapInt.resi | 4 + .../belt/src/Belt_MutableMapString.resi | 4 + .../@rescript/belt/src/Belt_MutableQueue.resi | 4 + .../@rescript/belt/src/Belt_MutableSet.resi | 2 + .../belt/src/Belt_MutableSetInt.resi | 4 + .../belt/src/Belt_MutableSetString.resi | 4 + .../@rescript/belt/src/Belt_MutableStack.resi | 3 + packages/@rescript/belt/src/Belt_Option.resi | 2 + packages/@rescript/belt/src/Belt_Result.resi | 2 + packages/@rescript/belt/src/Belt_Set.resi | 2 + packages/@rescript/belt/src/Belt_SetDict.resi | 2 + packages/@rescript/belt/src/Belt_SetInt.resi | 2 + .../@rescript/belt/src/Belt_SetString.resi | 2 + .../belt/src/Belt_internalAVLset.resi | 2 + .../belt/src/Belt_internalAVLtree.resi | 2 + .../belt/src/Belt_internalMapInt.res | 1 + .../belt/src/Belt_internalMapString.res | 1 + .../belt/src/Belt_internalSetInt.res | 1 + .../belt/src/Belt_internalSetString.res | 1 + packages/@rescript/runtime/Pervasives.res | 6 + packages/@rescript/runtime/Stdlib.res | 2 + packages/@rescript/runtime/Stdlib_BigInt.resi | 4 +- packages/@rescript/runtime/Stdlib_Bool.resi | 1 + packages/@rescript/runtime/Stdlib_Intl.res | 3 + packages/@rescript/runtime/Stdlib_JSON.res | 5 +- packages/@rescript/runtime/Stdlib_JSON.resi | 4 +- .../@rescript/runtime/Stdlib_JsError.resi | 9 + packages/@rescript/runtime/Stdlib_List.resi | 3 + packages/@rescript/runtime/Stdlib_Null.resi | 1 + .../@rescript/runtime/Stdlib_Nullable.resi | 1 + packages/@rescript/runtime/Stdlib_Option.resi | 1 + packages/@rescript/runtime/Stdlib_Result.resi | 1 + .../runtime/Stdlib_Temporal_Duration.res | 2 +- .../runtime/Stdlib_Temporal_Instant.res | 2 +- .../runtime/Stdlib_Temporal_PlainDate.res | 2 +- .../runtime/Stdlib_Temporal_PlainDateTime.res | 2 +- .../runtime/Stdlib_Temporal_PlainMonthDay.res | 2 +- .../runtime/Stdlib_Temporal_PlainTime.res | 2 +- .../Stdlib_Temporal_PlainYearMonth.res | 2 +- .../runtime/Stdlib_Temporal_ZonedDateTime.res | 2 +- packages/@rescript/runtime/lib/es6/Stdlib.mjs | 2 +- packages/@rescript/runtime/lib/js/Stdlib.cjs | 2 +- .../checkThrowsAnnotations.mjs | 66 ++++++ .../deadcode/expected/deadcode.txt | 1 - .../deadcode/expected/exception.txt | 54 ++++- .../deadcode/src/exception/BeltTest.res | 33 +++ .../deadcode/src/exception/BsJson.res | 0 .../src/exception/ExnPayloadGuard.res | 15 ++ .../deadcode/src/exception/StdlibTest.res | 74 +++++++ .../deadcode/src/exception/TestYojson.res | 17 -- .../deadcode/src/exception/ThrowsDecl.res | 19 ++ .../src/exception/ThrowsDeclCalls.res | 49 +++++ .../deadcode/src/exception/ThrowsIface.res | 15 ++ .../deadcode/src/exception/ThrowsIface.resi | 13 ++ .../deadcode/src/exception/Yojson.res | 19 -- .../tests-reanalyze/deadcode/test.sh | 3 + 69 files changed, 505 insertions(+), 308 deletions(-) delete mode 100644 analysis/reanalyze/src/exn_lib.ml create mode 100644 changes/reanalyze-throws-from-declarations.feature.md create mode 100644 tests/analysis_tests/tests-reanalyze/checkThrowsAnnotations.mjs delete mode 100644 tests/analysis_tests/tests-reanalyze/deadcode/src/exception/BsJson.res create mode 100644 tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ExnPayloadGuard.res delete mode 100644 tests/analysis_tests/tests-reanalyze/deadcode/src/exception/TestYojson.res create mode 100644 tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsDecl.res create mode 100644 tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsDeclCalls.res create mode 100644 tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsIface.res create mode 100644 tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsIface.resi delete mode 100644 tests/analysis_tests/tests-reanalyze/deadcode/src/exception/Yojson.res diff --git a/analysis/reanalyze/src/exception.ml b/analysis/reanalyze/src/exception.ml index 1a3b88f1fd7..fb27cdfb825 100644 --- a/analysis/reanalyze/src/exception.ml +++ b/analysis/reanalyze/src/exception.ml @@ -127,17 +127,20 @@ module Event = struct if config.Dce_config.cli.debug then Log_.item "%a@." print ev; exceptions |> Exceptions.iter (fun exn -> extend_exn_table exn loc); loop (Exceptions.union exn_set exceptions) rest - | ({kind = Call {callee; module_path}; loc} as ev) :: rest -> + | ({kind = Call {callee; module_path}; exceptions = declared; loc} as ev) + :: rest -> if config.Dce_config.cli.debug then Log_.item "%a@." print ev; + (* [declared] comes from the [@throws] of the declaration the caller + sees, as an interface's [let]. When it has none, the [@throws] of + the implementation's binding, from the values table, counts. *) let exceptions = - match - callee |> Values.find_path values_table ~module_name ~module_path - with - | Some exceptions -> exceptions - | _ -> ( - match Exn_lib.find callee with + if not (Exceptions.is_empty declared) then declared + else + match + callee |> Values.find_path values_table ~module_name ~module_path + with | Some exceptions -> exceptions - | None -> Exceptions.empty) + | None -> declared in exceptions |> Exceptions.iter (fun exn -> extend_exn_table exn loc); loop (Exceptions.union exn_set exceptions) rest @@ -280,7 +283,7 @@ let traverse_ast ~file ~values_builder ~checks_builder () = case.c_rhs |> iter_expr self) in let is_throw s = s = "Pervasives.raise" || s = "Pervasives.throw" in - (* [/] and [mod] are listed in [Exn_lib] as throwing [Division_by_zero]. + (* [/] and [mod] are declared with [@throws(Division_by_zero)] in Pervasives. They cannot throw when the divisor is a non-zero literal, as in [x / 10]. *) let is_division s = s = "Pervasives./" || s = "Pervasives.mod" || s = "Stdlib./" @@ -316,6 +319,30 @@ let traverse_ast ~file ~values_builder ~checks_builder () = |> Annotation.get_attribute_payload (fun s -> s = "doesNotThrow") <> None in + let get_exceptions_from_annotations attributes = + let throws_annotation_payload = + attributes |> Annotation.get_attribute_payload (fun s -> s = "throws") + in + let rec get_exceptions payload = + match payload with + | Annotation.StringPayload s -> + [Exn.from_string s] |> Exceptions.from_list + | Annotation.ConstructPayload s when s <> "::" -> + [Exn.from_string s] |> Exceptions.from_list + | Annotation.IdentPayload s -> + [Exn.from_string (s |> Longident.flatten |> String.concat ".")] + |> Exceptions.from_list + | Annotation.TuplePayload tuple -> + tuple + |> List.map (fun payload -> + payload |> get_exceptions |> Exceptions.to_list) + |> List.concat |> Exceptions.from_list + | _ -> Exceptions.empty + in + match throws_annotation_payload with + | None -> Exceptions.empty + | Some payload -> payload |> get_exceptions + in let expr ~(module_path : Module_path.t) (self : Tast_mapper.mapper) (expr : Typedtree.expression) = let loc = expr.exp_loc in @@ -323,7 +350,7 @@ let traverse_ast ~file ~values_builder ~checks_builder () = let old_events = !current_events in if is_does_no_throw then current_events := []; (match expr.exp_desc with - | Texp_ident (callee_, _, _) -> + | Texp_ident (callee_, _, vd) -> let callee = callee_ |> Dce_path.from_path_t |> Module_path.resolve_alias module_path in @@ -337,9 +364,12 @@ let traverse_ast ~file ~values_builder ~checks_builder () = "@{%s@} can be analyzed only if called directly" (callee_name |> Name.to_string); }); + (* A [@throws] on the declaration (an external, a [let] in an + interface, or a library's) is kept in the .cmi, so it reaches calls + from other modules and packages. *) current_events := { - Event.exceptions = Exceptions.empty; + Event.exceptions = get_exceptions_from_annotations vd.val_attributes; loc; kind = Call {callee; module_path = module_path.path}; } @@ -417,30 +447,6 @@ let traverse_ast ~file ~values_builder ~checks_builder () = :: old_events); expr in - let get_exceptions_from_annotations attributes = - let throws_annotation_payload = - attributes |> Annotation.get_attribute_payload (fun s -> s = "throws") - in - let rec get_exceptions payload = - match payload with - | Annotation.StringPayload s -> - [Exn.from_string s] |> Exceptions.from_list - | Annotation.ConstructPayload s when s <> "::" -> - [Exn.from_string s] |> Exceptions.from_list - | Annotation.IdentPayload s -> - [Exn.from_string (s |> Longident.flatten |> String.concat ".")] - |> Exceptions.from_list - | Annotation.TuplePayload tuple -> - tuple - |> List.map (fun payload -> - payload |> get_exceptions |> Exceptions.to_list) - |> List.concat |> Exceptions.from_list - | _ -> Exceptions.empty - in - match throws_annotation_payload with - | None -> Exceptions.empty - | Some payload -> payload |> get_exceptions - in let toplevel_eval (self : Tast_mapper.mapper) (expr : Typedtree.expression) attributes = let old_id = !current_id in diff --git a/analysis/reanalyze/src/exn.ml b/analysis/reanalyze/src/exn.ml index 47b64705cea..145d47975c5 100644 --- a/analysis/reanalyze/src/exn.ml +++ b/analysis/reanalyze/src/exn.ml @@ -1,15 +1,7 @@ type t = string let compare = String.compare -let decode_error = "DecodeError" -let assert_failure = "Assert_failure" -let division_by_zero = "Division_by_zero" -let invalid_argument = "Invalid_argument" -let js_exn = "JsExn" let match_failure = "Match_failure" -let not_found = "Not_found" let from_lid lid = lid |> Longident.flatten |> String.concat "." let from_string s = s let to_string s = s -let yojson_json_error = "Yojson.Json_error" -let yojson_type_error = "Yojson.Basic.Util.Type_error" diff --git a/analysis/reanalyze/src/exn.mli b/analysis/reanalyze/src/exn.mli index 21eb9f90cc5..683440249eb 100644 --- a/analysis/reanalyze/src/exn.mli +++ b/analysis/reanalyze/src/exn.mli @@ -1,15 +1,7 @@ type t val compare : t -> t -> int -val assert_failure : t -val decode_error : t -val division_by_zero : t val from_lid : Longident.t -> t val from_string : string -> t -val invalid_argument : t -val js_exn : t val match_failure : t -val not_found : t val to_string : t -> string -val yojson_json_error : t -val yojson_type_error : t diff --git a/analysis/reanalyze/src/exn_lib.ml b/analysis/reanalyze/src/exn_lib.ml deleted file mode 100644 index 2fe0d9a5849..00000000000 --- a/analysis/reanalyze/src/exn_lib.ml +++ /dev/null @@ -1,200 +0,0 @@ -let raises_lib_table : (Name.t, Exceptions.t) Hashtbl.t = - let table = Hashtbl.create 15 in - let open Exn in - let belt_array = - [ - ("getExn", [assert_failure]); - ("getOrThrow", [assert_failure]); - ("setExn", [assert_failure]); - ("setOrThrow", [assert_failure]); - ] - in - let belt_list = - [ - ("getExn", [not_found]); - ("getOrThrow", [not_found]); - ("headExn", [not_found]); - ("headOrThrow", [not_found]); - ("tailExn", [not_found]); - ("tailOrThrow", [not_found]); - ] - in - let belt_map = [("getExn", [not_found]); ("getOrThrow", [not_found])] in - let belt_mutable_map = belt_map in - let belt_mutable_queue = - [ - ("peekExn", [not_found]); - ("peekOrThrow", [not_found]); - ("popExn", [not_found]); - ("popOrThrow", [not_found]); - ] - in - let belt_set = [("getExn", [not_found]); ("getOrThrow", [not_found])] in - let belt_mutable_set = belt_set in - let belt_option = [("getExn", [not_found]); ("getOrThrow", [not_found])] in - let belt_result = [("getExn", [not_found]); ("getOrThrow", [not_found])] in - let bs_json = - (* bs-json *) - [ - ("bool", [decode_error]); - ("float", [decode_error]); - ("int", [decode_error]); - ("string", [decode_error]); - ("char", [decode_error]); - ("date", [decode_error]); - ("nullable", [decode_error]); - ("nullAs", [decode_error]); - ("array", [decode_error]); - ("list", [decode_error]); - ("pair", [decode_error]); - ("tuple2", [decode_error]); - ("tuple3", [decode_error]); - ("tuple4", [decode_error]); - ("dict", [decode_error]); - ("field", [decode_error]); - ("at", [decode_error; invalid_argument]); - ("oneOf", [decode_error]); - ("either", [decode_error]); - ] - in - let stdlib = - [ - ("panic", [js_exn]); - ("assertEqual", [js_exn]); - ("/", [division_by_zero]); - ("mod", [division_by_zero]); - ] - in - let stdlib_big_int = - [("fromStringOrThrow", [js_exn]); ("fromFloatOrThrow", [js_exn])] - in - let stdlib_bool = [("fromStringOrThrow", [invalid_argument])] in - let stdlib_js_error = - [ - ("EvalError.throwWithMessage", [js_exn]); - ("RangeError.throwWithMessage", [js_exn]); - ("ReferenceError.throwWithMessage", [js_exn]); - ("SyntaxError.throwWithMessage", [js_exn]); - ("TypeError.throwWithMessage", [js_exn]); - ("URIError.throwWithMessage", [js_exn]); - ("panic", [js_exn]); - ("throw", [js_exn]); - ("throwWithMessage", [js_exn]); - ] - in - let stdlib_json = [("parseOrThrow", [js_exn]); ("stringifyAny", [js_exn])] in - let stdlib_list = - [ - ("headOrThrow", [not_found]); - ("tailOrThrow", [not_found]); - ("getOrThrow", [not_found]); - ] - in - let stdlib_null = [("getOrThrow", [invalid_argument])] in - let stdlib_nullable = [("getOrThrow", [invalid_argument])] in - let stdlib_option = [("getOrThrow", [js_exn])] in - let stdlib_result = [("getOrThrow", [js_exn])] in - let yojson_basic = [("from_string", [yojson_json_error])] in - let yojson_basic_util = - [ - ("member", [yojson_type_error]); - ("to_assoc", [yojson_type_error]); - ("to_bool", [yojson_type_error]); - ("to_bool_option", [yojson_type_error]); - ("to_float", [yojson_type_error]); - ("to_float_option", [yojson_type_error]); - ("to_int", [yojson_type_error]); - ("to_list", [yojson_type_error]); - ("to_number", [yojson_type_error]); - ("to_number_option", [yojson_type_error]); - ("to_string", [yojson_type_error]); - ("to_string_option", [yojson_type_error]); - ] - in - [ - ("Belt.Array", belt_array); - ("Belt_Array", belt_array); - ("Belt.List", belt_list); - ("Belt_List", belt_list); - ("Belt.Map", belt_map); - ("Belt.Map.Int", belt_map); - ("Belt.Map.String", belt_map); - ("Belt_Map", belt_map); - ("Belt_Map.Int", belt_map); - ("Belt_Map.String", belt_map); - ("Belt_MapInt", belt_map); - ("Belt_MapString", belt_map); - ("Belt.MutableMap", belt_mutable_map); - ("Belt.MutableMap.Int", belt_mutable_map); - ("Belt.MutableMap.String", belt_mutable_map); - ("Belt_MutableMap", belt_mutable_map); - ("Belt_MutableMap.Int", belt_mutable_map); - ("Belt_MutableMap.String", belt_mutable_map); - ("Belt_MutableMapInt", belt_mutable_map); - ("Belt_MutableMapString", belt_mutable_map); - ("Belt.MutableQueue", belt_mutable_queue); - ("Belt_MutableQueue", belt_mutable_queue); - ("Belt_MutableSetInt", belt_mutable_set); - ("Belt_MutableSetString", belt_mutable_set); - ("Belt.MutableSet", belt_mutable_set); - ("Belt.MutableSet.Int", belt_mutable_set); - ("Belt.MutableSet.String", belt_mutable_set); - ("Belt.Option", belt_option); - ("Belt_Option", belt_option); - ("Belt.Result", belt_result); - ("Belt_Result", belt_result); - ("Belt.Set", belt_set); - ("Belt.Set.Int", belt_set); - ("Belt.Set.String", belt_set); - ("Belt_Set", belt_set); - ("Belt_Set.Int", belt_set); - ("Belt_Set.String", belt_set); - ("Belt_SetInt", belt_set); - ("Belt_SetString", belt_set); - ("BigInt", stdlib_big_int); - ("Bool", stdlib_bool); - ("JsError", stdlib_js_error); - ("JSON", stdlib_json); - ("Json_decode", bs_json); - ("Json.Decode", bs_json); - ("List", stdlib_list); - ("MutableSet", belt_mutable_set); - ("MutableSet.Int", belt_mutable_set); - ("MutableSet.String", belt_mutable_set); - ("Null", stdlib_null); - ("Nullable", stdlib_nullable); - ("Option", stdlib_option); - ("Pervasives", stdlib); - ("Result", stdlib_result); - ("Stdlib", stdlib); - ("Stdlib_BigInt", stdlib_big_int); - ("Stdlib.BigInt", stdlib_big_int); - ("Stdlib_Bool", stdlib_bool); - ("Stdlib.Bool", stdlib_bool); - ("Stdlib_JsError", stdlib_js_error); - ("Stdlib.JsError", stdlib_js_error); - ("Stdlib_JSON", stdlib_json); - ("Stdlib.JSON", stdlib_json); - ("Stdlib_List", stdlib_list); - ("Stdlib.List", stdlib_list); - ("Stdlib_Null", stdlib_null); - ("Stdlib.Null", stdlib_null); - ("Stdlib_Nullable", stdlib_nullable); - ("Stdlib.Nullable", stdlib_nullable); - ("Stdlib_Option", stdlib_option); - ("Stdlib.Option", stdlib_option); - ("Stdlib_Result", stdlib_result); - ("Stdlib.Result", stdlib_result); - ("Yojson.Basic", yojson_basic); - ("Yojson.Basic.Util", yojson_basic_util); - ] - |> List.iter (fun (name, group) -> - group - |> List.iter (fun (s, e) -> - Hashtbl.add table - (name ^ "." ^ s |> Name.create) - (e |> Exceptions.from_list))); - table - -let find (path : Dce_path.t) = - Hashtbl.find_opt raises_lib_table (path |> Dce_path.to_name) diff --git a/changes/reanalyze-throws-from-declarations.feature.md b/changes/reanalyze-throws-from-declarations.feature.md new file mode 100644 index 00000000000..48bdbe454e4 --- /dev/null +++ b/changes/reanalyze-throws-from-declarations.feature.md @@ -0,0 +1 @@ +- Exception analysis reads `@throws` from library and external declarations, including third-party ones, instead of a built-in list, and a `@throws` on a `let` now reaches other modules even without an interface. diff --git a/compiler/typing/typemod.ml b/compiler/typing/typemod.ml index 92ae8cebdbc..0e801633d39 100644 --- a/compiler/typing/typemod.ml +++ b/compiler/typing/typemod.ml @@ -1492,12 +1492,18 @@ and type_structure funct_body anchor env sstr = in (* The environment keeps let-bound values without attributes, so uses in this module do not warn. The signature carries their [deprecated] - attributes, so uses from other modules do. *) - let deprecated = + attributes, so uses from other modules do, and their [throws] + attributes, which reanalyze's exception analysis reads at calls + from other modules. Other attributes stay out: a doc comment in the + .cmi would make editing it recompile the module's dependents. *) + let signature_attributes = List.concat_map (fun (vb : value_binding) -> match Builtin_attributes.deprecated_attributes vb.vb_attributes + @ List.filter + (fun ({txt}, _) -> txt = "throws") + vb.vb_attributes with | [] -> [] | attrs -> @@ -1512,7 +1518,9 @@ and type_structure funct_body anchor env sstr = let vd = Env.find_value (Pident id) newenv in let vd = match - List.find_opt (fun (id', _) -> Ident.same id id') deprecated + List.find_opt + (fun (id', _) -> Ident.same id id') + signature_attributes with | None -> vd | Some (_, attrs) -> diff --git a/packages/@rescript/belt/src/Belt_Array.resi b/packages/@rescript/belt/src/Belt_Array.resi index 745f357c113..035925fce9e 100644 --- a/packages/@rescript/belt/src/Belt_Array.resi +++ b/packages/@rescript/belt/src/Belt_Array.resi @@ -42,12 +42,14 @@ let get: (t<'a>, int) => option<'a> Throw an exception if `i` is out of range. Otherwise return the value at index `i` in `arr`. */ +@throws(Assert_failure) let getExn: (t<'a>, int) => 'a /** Throw an exception if `i` is out of range. Otherwise return the value at index `i` in `arr`. */ +@throws(Assert_failure) let getOrThrow: (t<'a>, int) => 'a /** @@ -68,11 +70,13 @@ let set: (t<'a>, int, 'a) => bool /** `setExn(arr, i, x)` throw an exception if `i` is out of range. */ +@throws(Assert_failure) let setExn: (t<'a>, int, 'a) => unit /** `setOrThrow(arr, i, x)` throw an exception if `i` is out of range. */ +@throws(Assert_failure) let setOrThrow: (t<'a>, int, 'a) => unit external setUnsafe: (t<'a>, int, 'a) => unit = "%array_unsafe_set" diff --git a/packages/@rescript/belt/src/Belt_List.resi b/packages/@rescript/belt/src/Belt_List.resi index f8a9db85045..281041251af 100644 --- a/packages/@rescript/belt/src/Belt_List.resi +++ b/packages/@rescript/belt/src/Belt_List.resi @@ -60,6 +60,7 @@ switch Belt.List.headExn(list{}) { } ``` */ +@throws(Not_found) let headExn: t<'a> => 'a /** @@ -78,6 +79,7 @@ switch Belt.List.headOrThrow(list{}) { } ``` */ +@throws(Not_found) let headOrThrow: t<'a> => 'a /** @@ -110,6 +112,7 @@ switch Belt.List.tailExn(list{}) { } ``` */ +@throws(Not_found) let tailExn: t<'a> => t<'a> /** @@ -128,6 +131,7 @@ switch Belt.List.tailOrThrow(list{}) { } ``` */ +@throws(Not_found) let tailOrThrow: t<'a> => t<'a> /** @@ -177,6 +181,7 @@ switch abc->Belt.List.getExn(4) { } ``` */ +@throws(Not_found) let getExn: (t<'a>, int) => 'a /** @@ -197,6 +202,7 @@ switch abc->Belt.List.getOrThrow(4) { } ``` */ +@throws(Not_found) let getOrThrow: (t<'a>, int) => 'a /** diff --git a/packages/@rescript/belt/src/Belt_Map.resi b/packages/@rescript/belt/src/Belt_Map.resi index 2ff36890c4e..c2cbbb96b82 100644 --- a/packages/@rescript/belt/src/Belt_Map.resi +++ b/packages/@rescript/belt/src/Belt_Map.resi @@ -345,6 +345,7 @@ See `Belt.Map.get` throw when `k` not exist */ +@throws(Not_found) let getExn: (t<'k, 'v, 'id>, 'k) => 'v /** @@ -354,6 +355,7 @@ See `Belt.Map.get` throw when `k` not exist */ +@throws(Not_found) let getOrThrow: (t<'k, 'v, 'id>, 'k) => 'v /* ************************************************************************** */ diff --git a/packages/@rescript/belt/src/Belt_MapDict.resi b/packages/@rescript/belt/src/Belt_MapDict.resi index 22d08d1ff6b..2d68f38d8cc 100644 --- a/packages/@rescript/belt/src/Belt_MapDict.resi +++ b/packages/@rescript/belt/src/Belt_MapDict.resi @@ -132,8 +132,10 @@ let get: (t<'k, 'a, 'id>, 'k, ~cmp: cmp<'k, 'id>) => option<'a> let getWithDefault: (t<'k, 'a, 'id>, 'k, 'a, ~cmp: cmp<'k, 'id>) => 'a +@throws(Not_found) let getExn: (t<'k, 'a, 'id>, 'k, ~cmp: cmp<'k, 'id>) => 'a +@throws(Not_found) let getOrThrow: (t<'k, 'a, 'id>, 'k, ~cmp: cmp<'k, 'id>) => 'a let checkInvariantInternal: t<_> => unit diff --git a/packages/@rescript/belt/src/Belt_MapInt.resi b/packages/@rescript/belt/src/Belt_MapInt.resi index da86440d10c..58e447fc094 100644 --- a/packages/@rescript/belt/src/Belt_MapInt.resi +++ b/packages/@rescript/belt/src/Belt_MapInt.resi @@ -103,8 +103,10 @@ let get: (t<'v>, key) => option<'v> let getWithDefault: (t<'v>, key, 'v) => 'v +@throws(Not_found) let getExn: (t<'v>, key) => 'v +@throws(Not_found) let getOrThrow: (t<'v>, key) => 'v /** diff --git a/packages/@rescript/belt/src/Belt_MapString.resi b/packages/@rescript/belt/src/Belt_MapString.resi index c4fed2ad113..aa14e9fab2c 100644 --- a/packages/@rescript/belt/src/Belt_MapString.resi +++ b/packages/@rescript/belt/src/Belt_MapString.resi @@ -103,8 +103,10 @@ let get: (t<'v>, key) => option<'v> let getWithDefault: (t<'v>, key, 'v) => 'v +@throws(Not_found) let getExn: (t<'v>, key) => 'v +@throws(Not_found) let getOrThrow: (t<'v>, key) => 'v /** diff --git a/packages/@rescript/belt/src/Belt_MutableMap.resi b/packages/@rescript/belt/src/Belt_MutableMap.resi index 8b48c48a88e..68e3de5f8fb 100644 --- a/packages/@rescript/belt/src/Belt_MutableMap.resi +++ b/packages/@rescript/belt/src/Belt_MutableMap.resi @@ -103,7 +103,11 @@ let minimum: t<'k, 'a, _> => option<('k, 'a)> let maximum: t<'k, 'a, _> => option<('k, 'a)> let get: (t<'k, 'a, 'id>, 'k) => option<'a> let getWithDefault: (t<'k, 'a, 'id>, 'k, 'a) => 'a + +@throws(Not_found) let getExn: (t<'k, 'a, 'id>, 'k) => 'a + +@throws(Not_found) let getOrThrow: (t<'k, 'a, 'id>, 'k) => 'a /** Throw when invariant is not held. */ diff --git a/packages/@rescript/belt/src/Belt_MutableMapInt.resi b/packages/@rescript/belt/src/Belt_MutableMapInt.resi index d2a68170910..dc9cac42f9f 100644 --- a/packages/@rescript/belt/src/Belt_MutableMapInt.resi +++ b/packages/@rescript/belt/src/Belt_MutableMapInt.resi @@ -82,7 +82,11 @@ let minimum: t<'a> => option<(key, 'a)> let maximum: t<'a> => option<(key, 'a)> let get: (t<'a>, key) => option<'a> let getWithDefault: (t<'a>, key, 'a) => 'a + +@throws(Not_found) let getExn: (t<'a>, key) => 'a + +@throws(Not_found) let getOrThrow: (t<'a>, key) => 'a /** diff --git a/packages/@rescript/belt/src/Belt_MutableMapString.resi b/packages/@rescript/belt/src/Belt_MutableMapString.resi index b221010f967..eb05d1d8000 100644 --- a/packages/@rescript/belt/src/Belt_MutableMapString.resi +++ b/packages/@rescript/belt/src/Belt_MutableMapString.resi @@ -82,7 +82,11 @@ let minimum: t<'a> => option<(key, 'a)> let maximum: t<'a> => option<(key, 'a)> let get: (t<'a>, key) => option<'a> let getWithDefault: (t<'a>, key, 'a) => 'a + +@throws(Not_found) let getExn: (t<'a>, key) => 'a + +@throws(Not_found) let getOrThrow: (t<'a>, key) => 'a /** diff --git a/packages/@rescript/belt/src/Belt_MutableQueue.resi b/packages/@rescript/belt/src/Belt_MutableQueue.resi index fe5d8d38e36..c8ed5c7b235 100644 --- a/packages/@rescript/belt/src/Belt_MutableQueue.resi +++ b/packages/@rescript/belt/src/Belt_MutableQueue.resi @@ -46,11 +46,13 @@ let peek: t<'a> => option<'a> /** `peekExn(q)` throws an exception if `q` is empty. */ +@throws(Not_found) let peekExn: t<'a> => 'a /** `peekOrThrow(q)` throws an exception if `q` is empty. */ +@throws(Not_found) let peekOrThrow: t<'a> => 'a /** @@ -61,11 +63,13 @@ let pop: t<'a> => option<'a> /** `popExn(q)` throw an exception if q is empty. */ +@throws(Not_found) let popExn: t<'a> => 'a /** `popOrThrow(q)` throw an exception if q is empty. */ +@throws(Not_found) let popOrThrow: t<'a> => 'a /** diff --git a/packages/@rescript/belt/src/Belt_MutableSet.resi b/packages/@rescript/belt/src/Belt_MutableSet.resi index d4c12d19bfa..074679551b8 100644 --- a/packages/@rescript/belt/src/Belt_MutableSet.resi +++ b/packages/@rescript/belt/src/Belt_MutableSet.resi @@ -573,11 +573,13 @@ let get: (t<'value, 'id>, 'value) => option<'value> /** Same as `Belt.MutableSet.get` but throw when element does not exist. */ +@throws(Not_found) let getExn: (t<'value, 'id>, 'value) => 'value /** Same as `Belt.MutableSet.get` but throw when element does not exist. */ +@throws(Not_found) let getOrThrow: (t<'value, 'id>, 'value) => 'value /** diff --git a/packages/@rescript/belt/src/Belt_MutableSetInt.resi b/packages/@rescript/belt/src/Belt_MutableSetInt.resi index d64bf370448..981be1cfef9 100644 --- a/packages/@rescript/belt/src/Belt_MutableSetInt.resi +++ b/packages/@rescript/belt/src/Belt_MutableSetInt.resi @@ -103,7 +103,11 @@ let minimum: t => option let maximum: t => option let get: (t, value) => option + +@throws(Not_found) let getExn: (t, value) => value + +@throws(Not_found) let getOrThrow: (t, value) => value /** diff --git a/packages/@rescript/belt/src/Belt_MutableSetString.resi b/packages/@rescript/belt/src/Belt_MutableSetString.resi index 94381f493ce..cf2629db91e 100644 --- a/packages/@rescript/belt/src/Belt_MutableSetString.resi +++ b/packages/@rescript/belt/src/Belt_MutableSetString.resi @@ -103,7 +103,11 @@ let minimum: t => option let maximum: t => option let get: (t, value) => option + +@throws(Not_found) let getExn: (t, value) => value + +@throws(Not_found) let getOrThrow: (t, value) => value /** diff --git a/packages/@rescript/belt/src/Belt_MutableStack.resi b/packages/@rescript/belt/src/Belt_MutableStack.resi index 984020d5f3e..8061cf8101e 100644 --- a/packages/@rescript/belt/src/Belt_MutableStack.resi +++ b/packages/@rescript/belt/src/Belt_MutableStack.resi @@ -28,7 +28,10 @@ let copy: t<'a> => t<'a> let push: (t<'a>, 'a) => unit let pop: t<'a> => option<'a> + +@throws(Not_found) let popOrThrow: t<'a> => 'a + let top: t<'a> => option<'a> let isEmpty: t<'a> => bool let size: t<'a> => int diff --git a/packages/@rescript/belt/src/Belt_Option.resi b/packages/@rescript/belt/src/Belt_Option.resi index 1fad05201fd..9f6791121b9 100644 --- a/packages/@rescript/belt/src/Belt_Option.resi +++ b/packages/@rescript/belt/src/Belt_Option.resi @@ -70,6 +70,7 @@ switch Belt.Option.getExn(None) { } ``` */ +@throws(Not_found) let getExn: option<'a> => 'a /** @@ -87,6 +88,7 @@ switch Belt.Option.getOrThrow(None) { } ``` */ +@throws(Not_found) let getOrThrow: option<'a> => 'a /** diff --git a/packages/@rescript/belt/src/Belt_Result.resi b/packages/@rescript/belt/src/Belt_Result.resi index faf0b18593d..12af906f5f6 100644 --- a/packages/@rescript/belt/src/Belt_Result.resi +++ b/packages/@rescript/belt/src/Belt_Result.resi @@ -30,6 +30,7 @@ switch Belt.Result.getExn(Belt.Result.Error("Invalid data")) { } ``` */ +@throws(Not_found) let getExn: t<'a, 'b> => 'a /** @@ -47,6 +48,7 @@ switch Belt.Result.getOrThrow(Belt.Result.Error("Invalid data")) { } ``` */ +@throws(Not_found) let getOrThrow: t<'a, 'b> => 'a @deprecated("Use `mapWithDefault` instead") diff --git a/packages/@rescript/belt/src/Belt_Set.resi b/packages/@rescript/belt/src/Belt_Set.resi index 8d71784b8d4..9c4d9522a06 100644 --- a/packages/@rescript/belt/src/Belt_Set.resi +++ b/packages/@rescript/belt/src/Belt_Set.resi @@ -610,11 +610,13 @@ let get: (t<'value, 'id>, 'value) => option<'value> /** Same as [get](#get) but throw when element does not exist. */ +@throws(Not_found) let getExn: (t<'value, 'id>, 'value) => 'value /** Same as [get](#get) but throw when element does not exist. */ +@throws(Not_found) let getOrThrow: (t<'value, 'id>, 'value) => 'value /** diff --git a/packages/@rescript/belt/src/Belt_SetDict.resi b/packages/@rescript/belt/src/Belt_SetDict.resi index c2a5354a6c7..e3547c43abb 100644 --- a/packages/@rescript/belt/src/Belt_SetDict.resi +++ b/packages/@rescript/belt/src/Belt_SetDict.resi @@ -542,11 +542,13 @@ let get: (t<'value, 'id>, 'value, ~cmp: cmp<'value, 'id>) => option<'value> /** Same as [get](#get) but throw when element does not exist. */ +@throws(Not_found) let getExn: (t<'value, 'id>, 'value, ~cmp: cmp<'value, 'id>) => 'value /** Same as [get](#get) but throw when element does not exist. */ +@throws(Not_found) let getOrThrow: (t<'value, 'id>, 'value, ~cmp: cmp<'value, 'id>) => 'value /** diff --git a/packages/@rescript/belt/src/Belt_SetInt.resi b/packages/@rescript/belt/src/Belt_SetInt.resi index 590576256de..5e5b7b73c97 100644 --- a/packages/@rescript/belt/src/Belt_SetInt.resi +++ b/packages/@rescript/belt/src/Belt_SetInt.resi @@ -128,8 +128,10 @@ let maximum: t => option let get: (t, value) => option +@throws(Not_found) let getExn: (t, value) => value +@throws(Not_found) let getOrThrow: (t, value) => value /** diff --git a/packages/@rescript/belt/src/Belt_SetString.resi b/packages/@rescript/belt/src/Belt_SetString.resi index ec0f8cb375f..c7275a36554 100644 --- a/packages/@rescript/belt/src/Belt_SetString.resi +++ b/packages/@rescript/belt/src/Belt_SetString.resi @@ -128,8 +128,10 @@ let maximum: t => option let get: (t, value) => option +@throws(Not_found) let getExn: (t, value) => value +@throws(Not_found) let getOrThrow: (t, value) => value /** diff --git a/packages/@rescript/belt/src/Belt_internalAVLset.resi b/packages/@rescript/belt/src/Belt_internalAVLset.resi index e9ce7c88b1c..069125a25c0 100644 --- a/packages/@rescript/belt/src/Belt_internalAVLset.resi +++ b/packages/@rescript/belt/src/Belt_internalAVLset.resi @@ -74,6 +74,8 @@ let cmp: (t<'a>, t<'a>, ~cmp: cmp<'a, 'b>) => int let eq: (t<'a>, t<'a>, ~cmp: cmp<'a, 'b>) => bool let subset: (t<'a>, t<'a>, ~cmp: cmp<'a, 'b>) => bool let get: (t<'a>, 'a, ~cmp: cmp<'a, 'b>) => option<'a> + +@throws(Not_found) let getOrThrow: (t<'a>, 'a, ~cmp: cmp<'a, 'b>) => 'a let fromArray: (array<'a>, ~cmp: cmp<'a, 'b>) => t<'a> diff --git a/packages/@rescript/belt/src/Belt_internalAVLtree.resi b/packages/@rescript/belt/src/Belt_internalAVLtree.resi index 7fccf7020e9..fb18a5f9369 100644 --- a/packages/@rescript/belt/src/Belt_internalAVLtree.resi +++ b/packages/@rescript/belt/src/Belt_internalAVLtree.resi @@ -91,6 +91,8 @@ let eq: (t<'a, 'b>, t<'a, 'c>, ~kcmp: cmp<'a, _>, ~veq: ('b, 'c) => bool) => boo let get: (t<'a, 'b>, 'a, ~cmp: cmp<'a, _>) => option<'b> let getWithDefault: (t<'a, 'b>, 'a, 'b, ~cmp: cmp<'a, _>) => 'b + +@throws(Not_found) let getOrThrow: (t<'a, 'b>, 'a, ~cmp: cmp<'a, _>) => 'b let has: (t<'a, 'b>, 'a, ~cmp: cmp<'a, _>) => bool diff --git a/packages/@rescript/belt/src/Belt_internalMapInt.res b/packages/@rescript/belt/src/Belt_internalMapInt.res index 219879e90d4..d8770ad9cc6 100644 --- a/packages/@rescript/belt/src/Belt_internalMapInt.res +++ b/packages/@rescript/belt/src/Belt_internalMapInt.res @@ -44,6 +44,7 @@ let rec get = (n, x: key) => } } +@throws(Not_found) let rec getOrThrow = (n, x: key) => switch n { | None => throw(Not_found) diff --git a/packages/@rescript/belt/src/Belt_internalMapString.res b/packages/@rescript/belt/src/Belt_internalMapString.res index 880228823d9..9a515843390 100644 --- a/packages/@rescript/belt/src/Belt_internalMapString.res +++ b/packages/@rescript/belt/src/Belt_internalMapString.res @@ -44,6 +44,7 @@ let rec get = (n, x: key) => } } +@throws(Not_found) let rec getOrThrow = (n, x: key) => switch n { | None => throw(Not_found) diff --git a/packages/@rescript/belt/src/Belt_internalSetInt.res b/packages/@rescript/belt/src/Belt_internalSetInt.res index 55029c22473..f981c4b568c 100644 --- a/packages/@rescript/belt/src/Belt_internalSetInt.res +++ b/packages/@rescript/belt/src/Belt_internalSetInt.res @@ -85,6 +85,7 @@ let rec get = (n: t, x: value) => } } +@throws(Not_found) let rec getOrThrow = (n: t, x: value) => switch n { | None => throw(Not_found) diff --git a/packages/@rescript/belt/src/Belt_internalSetString.res b/packages/@rescript/belt/src/Belt_internalSetString.res index 910a1d0aaf9..216e54f96dc 100644 --- a/packages/@rescript/belt/src/Belt_internalSetString.res +++ b/packages/@rescript/belt/src/Belt_internalSetString.res @@ -85,6 +85,7 @@ let rec get = (n: t, x: value) => } } +@throws(Not_found) let rec getOrThrow = (n: t, x: value) => switch n { | None => throw(Not_found) diff --git a/packages/@rescript/runtime/Pervasives.res b/packages/@rescript/runtime/Pervasives.res index 2a51bfe1482..43d1b6f6455 100644 --- a/packages/@rescript/runtime/Pervasives.res +++ b/packages/@rescript/runtime/Pervasives.res @@ -56,10 +56,16 @@ external \"~-": 'a => 'a = "%neg" external \"+": ('a, 'a) => 'a = "%add" external \"-": ('a, 'a) => 'a = "%sub" external \"*": ('a, 'a) => 'a = "%mul" + +@throws(Division_by_zero) external \"/": ('a, 'a) => 'a = "%div" + external \"%": ('a, 'a) => 'a = "%mod" external \"<<": ('a, 'a) => 'a = "%lsl" + +@throws(Division_by_zero) external mod: ('a, 'a) => 'a = "%mod" + external \"**": ('a, 'a) => 'a = "%pow" external \"~~~": 'a => 'a = "%bitnot" diff --git a/packages/@rescript/runtime/Stdlib.res b/packages/@rescript/runtime/Stdlib.res index 303bff50169..aad73164c10 100644 --- a/packages/@rescript/runtime/Stdlib.res +++ b/packages/@rescript/runtime/Stdlib.res @@ -142,6 +142,7 @@ let caught = try panic("Invariant violated") catch { caught == "Panic! Invariant violated" ``` */ +@throws(JsExn) let panic = JsError.panic /** @@ -153,6 +154,7 @@ let panic = JsError.panic list{1, 2}->List.tailOrThrow == list{2} ``` */ +@throws(Assert_failure) let assertEqual = (a, b) => { if a != b { assert(false) diff --git a/packages/@rescript/runtime/Stdlib_BigInt.resi b/packages/@rescript/runtime/Stdlib_BigInt.resi index 231b853659b..142c8a6a2b0 100644 --- a/packages/@rescript/runtime/Stdlib_BigInt.resi +++ b/packages/@rescript/runtime/Stdlib_BigInt.resi @@ -55,7 +55,7 @@ switch BigInt.fromStringOrThrow("a") { } ``` */ -@val +@val @throws(JsExn) external fromStringOrThrow: string => bigint = "BigInt" /** @@ -113,7 +113,7 @@ switch BigInt.fromFloatOrThrow(123.5) { } ``` */ -@val +@val @throws(JsExn) external fromFloatOrThrow: float => bigint = "BigInt" /** diff --git a/packages/@rescript/runtime/Stdlib_Bool.resi b/packages/@rescript/runtime/Stdlib_Bool.resi index 1a28996cc66..77a6da7f98c 100644 --- a/packages/@rescript/runtime/Stdlib_Bool.resi +++ b/packages/@rescript/runtime/Stdlib_Bool.resi @@ -45,6 +45,7 @@ switch Bool.fromStringOrThrow("notAValidBoolean") { } ``` */ +@throws(Invalid_argument) let fromStringOrThrow: string => bool /** diff --git a/packages/@rescript/runtime/Stdlib_Intl.res b/packages/@rescript/runtime/Stdlib_Intl.res index 8717929d389..abb247210b6 100644 --- a/packages/@rescript/runtime/Stdlib_Intl.res +++ b/packages/@rescript/runtime/Stdlib_Intl.res @@ -22,6 +22,7 @@ See [`Intl.getCanonicalLocales`](https://developer.mozilla.org/en-US/docs/Web/Ja Intl.getCanonicalLocalesExn("EN-US") == ["en-US"] ``` */ +@throws(JsExn) external getCanonicalLocalesExn: string => array = "Intl.getCanonicalLocales" /** @@ -37,6 +38,7 @@ See [`Intl.getCanonicalLocales`](https://developer.mozilla.org/en-US/docs/Web/Ja Intl.getCanonicalLocalesManyExn(["EN-US", "fr"]) == ["en-US", "fr"] ``` */ +@throws(JsExn) external getCanonicalLocalesManyExn: array => array = "Intl.getCanonicalLocales" /** @@ -52,4 +54,5 @@ See [`Intl.supportedValuesOf`](https://developer.mozilla.org/en-US/docs/Web/Java Intl.supportedValuesOfExn("calendar")->Array.includes("gregory") == true ``` */ +@throws(JsExn) external supportedValuesOfExn: string => array = "Intl.supportedValuesOf" diff --git a/packages/@rescript/runtime/Stdlib_JSON.res b/packages/@rescript/runtime/Stdlib_JSON.res index a6aaa0fb7af..1f389c8f385 100644 --- a/packages/@rescript/runtime/Stdlib_JSON.res +++ b/packages/@rescript/runtime/Stdlib_JSON.res @@ -10,11 +10,12 @@ type rec t = @unboxed type replacer = Keys(array) | Replacer((string, t) => t) -@throws @val external parseOrThrow: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse" +@throws(JsExn) @val +external parseOrThrow: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse" @val external stringify: (t, ~replacer: replacer=?, ~space: int=?) => string = "JSON.stringify" -@throws @val +@throws(JsExn) @val external stringifyAny: ('a, ~replacer: replacer=?, ~space: int=?) => option = "JSON.stringify" diff --git a/packages/@rescript/runtime/Stdlib_JSON.resi b/packages/@rescript/runtime/Stdlib_JSON.resi index 429c2ffcc12..672b6237c91 100644 --- a/packages/@rescript/runtime/Stdlib_JSON.resi +++ b/packages/@rescript/runtime/Stdlib_JSON.resi @@ -60,7 +60,7 @@ try { - Throws a SyntaxError (JsExn.t) if the string isn't valid JSON. */ -@val +@val @throws(JsExn) external parseOrThrow: (string, ~reviver: (string, t) => t=?) => t = "JSON.parse" /** @@ -174,7 +174,7 @@ switch BigInt.fromInt(0)->JSON.stringifyAny { - Throws a TypeError if the value contains circular references. - Throws a TypeError if the value contains `BigInt`s. */ -@val +@val @throws(JsExn) external stringifyAny: ('a, ~replacer: replacer=?, ~space: int=?) => option = "JSON.stringify" diff --git a/packages/@rescript/runtime/Stdlib_JsError.resi b/packages/@rescript/runtime/Stdlib_JsError.resi index ef4a489cc61..237c3a659de 100644 --- a/packages/@rescript/runtime/Stdlib_JsError.resi +++ b/packages/@rescript/runtime/Stdlib_JsError.resi @@ -89,6 +89,7 @@ module EvalError: { See [`EvalError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/EvalError) on MDN. */ + @throws(JsExn) let throwWithMessage: string => 'a } @@ -108,6 +109,7 @@ module RangeError: { See [`RangeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RangeError) on MDN. */ + @throws(JsExn) let throwWithMessage: string => 'a } @@ -127,6 +129,7 @@ module ReferenceError: { See [`ReferenceError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/ReferenceError) on MDN. */ + @throws(JsExn) let throwWithMessage: string => 'a } @@ -146,6 +149,7 @@ module SyntaxError: { See [`SyntaxError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/SyntaxError) on MDN. */ + @throws(JsExn) let throwWithMessage: string => 'a } @@ -165,6 +169,7 @@ module TypeError: { See [`TypeError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypeError) on MDN. */ + @throws(JsExn) let throwWithMessage: string => 'a } @@ -184,6 +189,7 @@ module URIError: { See [`URIError`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/URIError) on MDN. */ + @throws(JsExn) let throwWithMessage: string => 'a } @@ -202,6 +208,7 @@ if 5 > 10 { } ``` */ +@throws(JsExn) external throw: t => 'a = "%raise" /** @@ -209,6 +216,7 @@ Creates a new `Error` with the provided `message` and throws it. `JsError.throwWithMessage("message")` is equivalent to `JsError.make("message")->JsError.throw`. */ +@throws(JsExn) let throwWithMessage: string => 'a /** @@ -233,6 +241,7 @@ try { } ``` */ +@throws(JsExn) let panic: string => 'a /** diff --git a/packages/@rescript/runtime/Stdlib_List.resi b/packages/@rescript/runtime/Stdlib_List.resi index 2de11bfbd03..a18f5dd7106 100644 --- a/packages/@rescript/runtime/Stdlib_List.resi +++ b/packages/@rescript/runtime/Stdlib_List.resi @@ -71,6 +71,7 @@ switch List.headOrThrow(list{}) { - Throws an Error if list is empty. */ +@throws(Not_found) let headOrThrow: list<'a> => 'a /** @@ -105,6 +106,7 @@ switch List.tailOrThrow(list{}) { - Throws an Error if list is empty. */ +@throws(Not_found) let tailOrThrow: list<'a> => list<'a> /** @@ -156,6 +158,7 @@ switch abc->List.getOrThrow(4) { - Throws an Error if `index` is larger than the length of list. */ +@throws(Not_found) let getOrThrow: (list<'a>, int) => 'a /** diff --git a/packages/@rescript/runtime/Stdlib_Null.resi b/packages/@rescript/runtime/Stdlib_Null.resi index 6726c2c0c29..c0fe10bc79e 100644 --- a/packages/@rescript/runtime/Stdlib_Null.resi +++ b/packages/@rescript/runtime/Stdlib_Null.resi @@ -154,6 +154,7 @@ switch Null.getOrThrow(%raw("null")) { - Throws `Invalid_argument` if `value` is `null` */ +@throws(Invalid_argument) let getOrThrow: t<'a> => 'a /** diff --git a/packages/@rescript/runtime/Stdlib_Nullable.resi b/packages/@rescript/runtime/Stdlib_Nullable.resi index 0fca9f47eca..d18a8f89fe1 100644 --- a/packages/@rescript/runtime/Stdlib_Nullable.resi +++ b/packages/@rescript/runtime/Stdlib_Nullable.resi @@ -154,6 +154,7 @@ switch Nullable.getOrThrow(%raw("undefined")) { - Throws `Invalid_argument` if `value` is `null` or `undefined` */ +@throws(Invalid_argument) let getOrThrow: t<'a> => 'a /** diff --git a/packages/@rescript/runtime/Stdlib_Option.resi b/packages/@rescript/runtime/Stdlib_Option.resi index 39e0e755b9c..9d5989c5871 100644 --- a/packages/@rescript/runtime/Stdlib_Option.resi +++ b/packages/@rescript/runtime/Stdlib_Option.resi @@ -73,6 +73,7 @@ switch Option.getOrThrow(None, ~message="was None!") { - Throws an error if `opt` is `None` */ +@throws(JsExn) let getOrThrow: (option<'a>, ~message: string=?) => 'a /** diff --git a/packages/@rescript/runtime/Stdlib_Result.resi b/packages/@rescript/runtime/Stdlib_Result.resi index 9ca4b71d06a..6fe8a67f14b 100644 --- a/packages/@rescript/runtime/Stdlib_Result.resi +++ b/packages/@rescript/runtime/Stdlib_Result.resi @@ -46,6 +46,7 @@ type t<'res, 'err> = result<'res, 'err> = Ok('res) | Error('err) } == true ``` */ +@throws(JsExn) let getOrThrow: (result<'a, 'b>, ~message: string=?) => 'a /** diff --git a/packages/@rescript/runtime/Stdlib_Temporal_Duration.res b/packages/@rescript/runtime/Stdlib_Temporal_Duration.res index 9806001fc12..146782208cb 100644 --- a/packages/@rescript/runtime/Stdlib_Temporal_Duration.res +++ b/packages/@rescript/runtime/Stdlib_Temporal_Duration.res @@ -82,7 +82,7 @@ See [`Temporal.Duration.from`](https://developer.mozilla.org/en-US/docs/Web/Java Temporal.Duration.fromStringOrThrow("PT1H30M")->Temporal.Duration.minutes == 30. ``` */ -@val +@val @throws(JsExn) external fromStringOrThrow: string => t = "Temporal.Duration.from" /** diff --git a/packages/@rescript/runtime/Stdlib_Temporal_Instant.res b/packages/@rescript/runtime/Stdlib_Temporal_Instant.res index 79ccdd77edb..c67e23e9a38 100644 --- a/packages/@rescript/runtime/Stdlib_Temporal_Instant.res +++ b/packages/@rescript/runtime/Stdlib_Temporal_Instant.res @@ -30,7 +30,7 @@ Temporal.Instant.fromStringOrThrow("2024-01-01T00:00:00Z")->Temporal.Instant.epo 1704067200000. ``` */ -@val +@val @throws(JsExn) external fromStringOrThrow: string => t = "Temporal.Instant.from" /** diff --git a/packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res b/packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res index 830606c3e58..36e06c8fbcf 100644 --- a/packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res +++ b/packages/@rescript/runtime/Stdlib_Temporal_PlainDate.res @@ -51,7 +51,7 @@ See [`Temporal.PlainDate.from`](https://developer.mozilla.org/en-US/docs/Web/Jav Temporal.PlainDate.fromStringOrThrow("2024-02-29")->Temporal.PlainDate.day == 29 ``` */ -@val +@val @throws(JsExn) external fromStringOrThrow: string => t = "Temporal.PlainDate.from" /** diff --git a/packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res b/packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res index 8dd2e2ec0ae..46c48dfd2a4 100644 --- a/packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res +++ b/packages/@rescript/runtime/Stdlib_Temporal_PlainDateTime.res @@ -62,7 +62,7 @@ See [`Temporal.PlainDateTime.from`](https://developer.mozilla.org/en-US/docs/Web Temporal.PlainDateTime.fromStringOrThrow("2024-02-29T09:30")->Temporal.PlainDateTime.hour == 9 ``` */ -@val +@val @throws(JsExn) external fromStringOrThrow: string => t = "Temporal.PlainDateTime.from" /** diff --git a/packages/@rescript/runtime/Stdlib_Temporal_PlainMonthDay.res b/packages/@rescript/runtime/Stdlib_Temporal_PlainMonthDay.res index 32b6c640d65..1ef4a7295f3 100644 --- a/packages/@rescript/runtime/Stdlib_Temporal_PlainMonthDay.res +++ b/packages/@rescript/runtime/Stdlib_Temporal_PlainMonthDay.res @@ -57,7 +57,7 @@ See [`Temporal.PlainMonthDay.from`](https://developer.mozilla.org/en-US/docs/Web Temporal.PlainMonthDay.fromStringOrThrow("12-25")->Temporal.PlainMonthDay.day == 25 ``` */ -@val +@val @throws(JsExn) external fromStringOrThrow: string => t = "Temporal.PlainMonthDay.from" /** diff --git a/packages/@rescript/runtime/Stdlib_Temporal_PlainTime.res b/packages/@rescript/runtime/Stdlib_Temporal_PlainTime.res index 8d40304278f..34c5e802956 100644 --- a/packages/@rescript/runtime/Stdlib_Temporal_PlainTime.res +++ b/packages/@rescript/runtime/Stdlib_Temporal_PlainTime.res @@ -47,7 +47,7 @@ See [`Temporal.PlainTime.from`](https://developer.mozilla.org/en-US/docs/Web/Jav Temporal.PlainTime.fromStringOrThrow("09:30")->Temporal.PlainTime.minute == 30 ``` */ -@val +@val @throws(JsExn) external fromStringOrThrow: string => t = "Temporal.PlainTime.from" /** diff --git a/packages/@rescript/runtime/Stdlib_Temporal_PlainYearMonth.res b/packages/@rescript/runtime/Stdlib_Temporal_PlainYearMonth.res index 0493d78e6db..fba8511fd3c 100644 --- a/packages/@rescript/runtime/Stdlib_Temporal_PlainYearMonth.res +++ b/packages/@rescript/runtime/Stdlib_Temporal_PlainYearMonth.res @@ -77,7 +77,7 @@ See [`Temporal.PlainYearMonth.from`](https://developer.mozilla.org/en-US/docs/We Temporal.PlainYearMonth.fromStringOrThrow("2024-02")->Temporal.PlainYearMonth.daysInMonth == 29 ``` */ -@val +@val @throws(JsExn) external fromStringOrThrow: string => t = "Temporal.PlainYearMonth.from" /** diff --git a/packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res b/packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res index 73256f9e00a..85a0075333b 100644 --- a/packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res +++ b/packages/@rescript/runtime/Stdlib_Temporal_ZonedDateTime.res @@ -88,7 +88,7 @@ Temporal.ZonedDateTime.fromStringOrThrow( )->Temporal.ZonedDateTime.timeZoneId == "Asia/Seoul" ``` */ -@val +@val @throws(JsExn) external fromStringOrThrow: (string, ~options: fromOptions=?) => t = "Temporal.ZonedDateTime.from" /** diff --git a/packages/@rescript/runtime/lib/es6/Stdlib.mjs b/packages/@rescript/runtime/lib/es6/Stdlib.mjs index 904294e9755..17a914df037 100644 --- a/packages/@rescript/runtime/lib/es6/Stdlib.mjs +++ b/packages/@rescript/runtime/lib/es6/Stdlib.mjs @@ -12,7 +12,7 @@ function assertEqual(a, b) { RE_EXN_ID: "Assert_failure", _1: [ "Stdlib.res", - 158, + 160, 4 ], Error: new Error() diff --git a/packages/@rescript/runtime/lib/js/Stdlib.cjs b/packages/@rescript/runtime/lib/js/Stdlib.cjs index ffb9f6ffb59..a76b3ec2c0f 100644 --- a/packages/@rescript/runtime/lib/js/Stdlib.cjs +++ b/packages/@rescript/runtime/lib/js/Stdlib.cjs @@ -12,7 +12,7 @@ function assertEqual(a, b) { RE_EXN_ID: "Assert_failure", _1: [ "Stdlib.res", - 158, + 160, 4 ], Error: new Error() diff --git a/tests/analysis_tests/tests-reanalyze/checkThrowsAnnotations.mjs b/tests/analysis_tests/tests-reanalyze/checkThrowsAnnotations.mjs new file mode 100644 index 00000000000..93d46ac902e --- /dev/null +++ b/tests/analysis_tests/tests-reanalyze/checkThrowsAnnotations.mjs @@ -0,0 +1,66 @@ +// Checks that every stdlib and Belt value named `*OrThrow` or `*Exn` declares +// what it throws with `@throws(...)`. The exception analysis reads that +// attribute from the declaration at each call site, so a missing one makes +// calls to the function look as if they throw nothing. +// +// A module's declarations are read from its .resi, or from its .res when it +// has no interface; the compiler copies a binding's `@throws` into the +// signature it infers. + +import fs from "node:fs"; +import path from "node:path"; + +const root = path.resolve(import.meta.dirname, "../../.."); +const dirs = ["packages/@rescript/runtime", "packages/@rescript/belt/src"]; + +// Names that end in `Exn` without throwing. +const allowed = new Set(["Stdlib_JsError.toJsExn"]); + +const declaration = + /^\s*(?:let|external)\s+(?:rec\s+)?([A-Za-z_][A-Za-z0-9_']*)\s*[:=]/; +const throwsWithPayload = /@throws\(/; + +const missing = []; + +for (const dir of dirs) { + const files = fs.readdirSync(path.join(root, dir)); + for (const file of files) { + const match = /^(.*)\.(resi?)$/.exec(file); + if (match === null) continue; + const [, module, ext] = match; + if (ext === "res" && files.includes(`${module}.resi`)) continue; + const lines = fs + .readFileSync(path.join(root, dir, file), "utf8") + .split("\n"); + let commentDepth = 0; + lines.forEach((line, i) => { + const inComment = commentDepth > 0; + commentDepth += + (line.match(/\/\*/g) ?? []).length - (line.match(/\*\//g) ?? []).length; + if (inComment) return; + const decl = declaration.exec(line); + if (decl === null) return; + const name = decl[1]; + if (!/(OrThrow|Exn)$/.test(name)) return; + if (allowed.has(`${module}.${name}`)) return; + // The attributes of a declaration are on its line and the lines above + // it, up to a blank line or the end of its doc comment. + let annotated = throwsWithPayload.test(line); + for (let j = i - 1; !annotated && j >= 0; j--) { + const above = lines[j].trim(); + if (above === "" || above.endsWith("*/")) break; + annotated = throwsWithPayload.test(above); + } + if (!annotated) missing.push(`${dir}/${file}:${i + 1}: ${name}`); + }); + } +} + +if (missing.length > 0) { + console.error( + "These declarations need a @throws(...) naming what they throw:", + ); + for (const m of missing) console.error(` ${m}`); + process.exit(1); +} +console.log("✅ Every *OrThrow and *Exn declaration has a @throws."); diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt b/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt index d523b404733..9822ce80203 100644 --- a/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt +++ b/tests/analysis_tests/tests-reanalyze/deadcode/expected/deadcode.txt @@ -8,7 +8,6 @@ addDeclaration_ VariantCase R2 AutoAnnotate.res:14:2 path:+AutoAnnotate.annotatedVariant addDeclaration_ VariantCase R4 AutoAnnotate.res:15:2 path:+AutoAnnotate.annotatedVariant Scanning BootloaderResource.cmt Source:BootloaderResource.res - Implementation 0 Scanning BucklescriptAnnotations.cmt Source:BucklescriptAnnotations.res addDeclaration_ Value +bar BucklescriptAnnotations.res:22:4 path:+BucklescriptAnnotations addDeclaration_ Value +f BucklescriptAnnotations.res:23:6 path:+BucklescriptAnnotations diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt b/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt index c6dd3c9024f..39019018000 100644 --- a/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt +++ b/tests/analysis_tests/tests-reanalyze/deadcode/expected/exception.txt @@ -151,5 +151,57 @@ Exception Analysis ExternalTest.res:7:5-24 bigIntFromStringExn2 might throw JsExn (ExternalTest.res:7:35) and is not annotated with @throws(JsExn) + + Exception Analysis + StdlibTest.res:67:5-34 + plainDateFromStringUnannotated might throw JsExn (StdlibTest.res:67:42) and is not annotated with @throws(JsExn) + + Exception Analysis + ThrowsDeclCalls.res:1:5-16 + callExternal might throw JsExn (ThrowsDeclCalls.res:1:24) and is not annotated with @throws(JsExn) + + Exception Analysis + ThrowsDeclCalls.res:3:5-20 + callExternalPipe might throw JsExn (ThrowsDeclCalls.res:3:31) and is not annotated with @throws(JsExn) + + Exception Analysis + ThrowsDeclCalls.res:5:5-14 + callNested might throw Not_found (ThrowsDeclCalls.res:5:22) and is not annotated with @throws(Not_found) + + Exception Analysis + ThrowsDeclCalls.res:7:5-16 + callIfaceLet might throw Not_found (ThrowsDeclCalls.res:7:24) and is not annotated with @throws(Not_found) + + Exception Analysis + ThrowsDeclCalls.res:9:5-21 + callIfaceExternal might throw JsExn (ThrowsDeclCalls.res:9:29) and is not annotated with @throws(JsExn) + + Exception Analysis + ThrowsDeclCalls.res:12:5-13 + callAlias might throw JsExn (ThrowsDeclCalls.res:12:21) and is not annotated with @throws(JsExn) + + Exception Analysis + ThrowsDeclCalls.res:14:5-12 + callOpen might throw JsExn (ThrowsDeclCalls.res:16:2) and is not annotated with @throws(JsExn) + + Exception Analysis + ThrowsDeclCalls.res:23:5-15 + callInclude might throw JsExn (ThrowsDeclCalls.res:23:23) and is not annotated with @throws(JsExn) + + Exception Analysis + ThrowsDeclCalls.res:26:5-15 + callFunctor might throw Not_found (ThrowsDeclCalls.res:26:23) and is not annotated with @throws(Not_found) + + Exception Analysis + ThrowsDeclCalls.res:28:5-18 + callFirstClass might throw Not_found (ThrowsDeclCalls.res:30:2) and is not annotated with @throws(Not_found) + + Exception Analysis + ThrowsDeclCalls.res:36:5-14 + callViaLet might throw Not_found (ThrowsDeclCalls.res:36:22) and is not annotated with @throws(Not_found) + + Exception Analysis + ThrowsDeclCalls.res:37:5-22 + callViaLetIncluded might throw Not_found (ThrowsDeclCalls.res:37:30) and is not annotated with @throws(Not_found) - Analysis reported 38 issues (Exception Analysis:38) + Analysis reported 51 issues (Exception Analysis:51) diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/BeltTest.res b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/BeltTest.res index 0a8910c486e..81b41844484 100644 --- a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/BeltTest.res +++ b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/BeltTest.res @@ -29,3 +29,36 @@ let mapGetExn7 = (s, k) => s->Belt.Map.getExn(k) @throws(Not_found) let mapGetExn8 = (s, k) => s->Belt_Map.getExn(k) + +@throws(Not_found) +let stackPopOrThrow = s => Belt.MutableStack.popOrThrow(s) + +@throws(Not_found) +let directStackPopOrThrow = s => Belt_MutableStack.popOrThrow(s) + +@throws(Not_found) +let mapDictGetOrThrow = (m, k, ~cmp) => Belt.Map.Dict.getOrThrow(m, k, ~cmp) + +@throws(Not_found) +let mapDictGetExn = (m, k, ~cmp) => Belt.Map.Dict.getExn(m, k, ~cmp) + +@throws(Not_found) +let directMapDictGetOrThrow = (m, k, ~cmp) => Belt_MapDict.getOrThrow(m, k, ~cmp) + +@throws(Not_found) +let setDictGetOrThrow = (s, v, ~cmp) => Belt.Set.Dict.getOrThrow(s, v, ~cmp) + +@throws(Not_found) +let directSetDictGetOrThrow = (s, v, ~cmp) => Belt_SetDict.getOrThrow(s, v, ~cmp) + +@throws(Not_found) +let directMutableSetGetOrThrow = (s, v) => Belt_MutableSet.getOrThrow(s, v) + +@throws(Not_found) +let directMutableSetIntGetOrThrow = (s, v) => Belt_MutableSet.Int.getOrThrow(s, v) + +@throws(Assert_failure) +let arrayGetExn = (a, i) => Belt.Array.getExn(a, i) + +@throws(Not_found) +let queuePopOrThrow = q => Belt.MutableQueue.popOrThrow(q) diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/BsJson.res b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/BsJson.res deleted file mode 100644 index e69de29bb2d..00000000000 diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ExnPayloadGuard.res b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ExnPayloadGuard.res new file mode 100644 index 00000000000..a618aac833c --- /dev/null +++ b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ExnPayloadGuard.res @@ -0,0 +1,15 @@ +exception Type_error(string, int) + +module Util = { + @throws(Type_error) + let member = (s, j) => s == "" ? throw(Type_error(s, j)) : j +} + +let bar = (str, json) => + switch { + open Util + member(str, json) + } { + | j => j + | exception Type_error("a", d) if d == json => json + } diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/StdlibTest.res b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/StdlibTest.res index 63868d7a7e8..c641633f24f 100644 --- a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/StdlibTest.res +++ b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/StdlibTest.res @@ -12,3 +12,77 @@ let bigIntFromStringExn = s => s->BigInt.fromStringOrThrow @throws(JsExn) let jsonParseExn = s => s->JSON.parseOrThrow + +@throws(JsExn) +let durationFromStringOrThrow = s => Temporal.Duration.fromStringOrThrow(s) + +@throws(JsExn) +let instantFromStringOrThrow = s => Temporal.Instant.fromStringOrThrow(s) + +@throws(JsExn) +let plainDateFromStringOrThrow = s => Temporal.PlainDate.fromStringOrThrow(s) + +@throws(JsExn) +let plainDateTimeFromStringOrThrow = s => Temporal.PlainDateTime.fromStringOrThrow(s) + +@throws(JsExn) +let plainMonthDayFromStringOrThrow = s => Temporal.PlainMonthDay.fromStringOrThrow(s) + +@throws(JsExn) +let plainTimeFromStringOrThrow = s => Temporal.PlainTime.fromStringOrThrow(s) + +@throws(JsExn) +let plainYearMonthFromStringOrThrow = s => Temporal.PlainYearMonth.fromStringOrThrow(s) + +@throws(JsExn) +let zonedDateTimeFromStringOrThrow = s => Temporal.ZonedDateTime.fromStringOrThrow(s) + +@throws(JsExn) +let stdlibPlainDateFromStringOrThrow = s => Stdlib.Temporal.PlainDate.fromStringOrThrow(s) + +@throws(JsExn) +let openedPlainDateFromStringOrThrow = s => { + open Temporal + PlainDate.fromStringOrThrow(s) +} + +@throws(JsExn) +let directPlainDateFromStringOrThrow = s => Stdlib_Temporal_PlainDate.fromStringOrThrow(s) + +@throws(JsExn) +let aliasPlainDateFromStringOrThrow = s => Stdlib_Temporal.PlainDate.fromStringOrThrow(s) + +@throws(JsExn) +let canonicalLocales = s => Intl.getCanonicalLocalesExn(s) + +@throws(JsExn) +let canonicalLocalesMany = s => Intl.getCanonicalLocalesManyExn(s) + +@throws(JsExn) +let supportedValuesOf = s => Intl.supportedValuesOfExn(s) + +@throws(JsExn) +let stdlibCanonicalLocales = s => Stdlib.Intl.getCanonicalLocalesExn(s) + +let plainDateFromStringUnannotated = s => Temporal.PlainDate.fromStringOrThrow(s) + +@throws(JsExn) +let callPanic = () => panic("x") + +@throws(Assert_failure) +let callAssertEqual = (a, b) => assertEqual(a, b) + +@throws(JsExn) +let rangeError = () => JsError.RangeError.throwWithMessage("x") + +@throws(Not_found) +let listHead = l => List.headOrThrow(l) + +@throws(Invalid_argument) +let boolFromString = s => Bool.fromStringOrThrow(s) + +@throws(Invalid_argument) +let nullableGetOrThrow = n => Nullable.getOrThrow(n) + +@throws(JsExn) +let stringifyAny = x => JSON.stringifyAny(x) diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/TestYojson.res b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/TestYojson.res deleted file mode 100644 index 6dda239e927..00000000000 --- a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/TestYojson.res +++ /dev/null @@ -1,17 +0,0 @@ -@throws(Yojson.Json_error) -let foo = x => Yojson.Basic.from_string(x) - -let bar = (str, json) => - switch { - open Yojson.Basic.Util - member(str, json) - } { - | j => j - | exception Yojson.Basic.Util.Type_error("a", d) when d == json => json - } - -@throws(Yojson.Basic.Util.Type_error) -let toString = x => Yojson.Basic.Util.to_string(x) - -@throws(Yojson.Basic.Util.Type_error) -let toInt = x => Yojson.Basic.Util.to_int(x) diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsDecl.res b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsDecl.res new file mode 100644 index 00000000000..d1414ba9971 --- /dev/null +++ b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsDecl.res @@ -0,0 +1,19 @@ +@throws(JsExn) +external parse: string => int = "parseThrowing" + +module Inner = { + @throws(Not_found) + external find: int => int = "findThrowing" +} + +module type S = { + @throws(Not_found) + let get: int => int +} + +module Make = (X: {}): S => { + let get = x => x +} + +@throws(Not_found) +let viaLet = x => x > 0 ? x : throw(Not_found) diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsDeclCalls.res b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsDeclCalls.res new file mode 100644 index 00000000000..8fa6c27eda4 --- /dev/null +++ b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsDeclCalls.res @@ -0,0 +1,49 @@ +let callExternal = s => ThrowsDecl.parse(s) + +let callExternalPipe = s => s->ThrowsDecl.parse + +let callNested = x => ThrowsDecl.Inner.find(x) + +let callIfaceLet = x => ThrowsIface.lookup(x) + +let callIfaceExternal = s => ThrowsIface.parseIface(s) + +module Alias = ThrowsDecl +let callAlias = s => Alias.parse(s) + +let callOpen = s => { + open ThrowsDecl + parse(s) +} + +module Included = { + include ThrowsDecl +} + +let callInclude = s => Included.parse(s) + +module Applied = ThrowsDecl.Make() +let callFunctor = x => Applied.get(x) + +let callFirstClass = (m: module(ThrowsDecl.S), x) => { + module M = unpack(m) + M.get(x) +} + +@throws(JsExn) +let callExternalAnnotated = s => ThrowsDecl.parse(s) + +let callViaLet = x => ThrowsDecl.viaLet(x) +let callViaLetIncluded = x => Included.viaLet(x) + +module IfaceIncluded = { + include ThrowsIface +} + +let callImplOnlyIncluded = x => IfaceIncluded.implOnly(x) + +// Only the interface's Not_found counts. +let catchNarrowed = x => + try ThrowsIface.narrowed(x) catch { + | Not_found => 0 + } diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsIface.res b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsIface.res new file mode 100644 index 00000000000..d32ef6fd1b7 --- /dev/null +++ b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsIface.res @@ -0,0 +1,15 @@ +let lookup = x => x +external parseIface: string => int = "parseThrowing" + +@throws(Not_found) +let implOnly = x => x > 0 ? x : throw(Not_found) + +@throws([Not_found, Invalid_argument]) +let narrowed = x => + if x > 0 { + x + } else if x < -10 { + throw(Invalid_argument("x")) + } else { + throw(Not_found) + } diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsIface.resi b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsIface.resi new file mode 100644 index 00000000000..e5e06047174 --- /dev/null +++ b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/ThrowsIface.resi @@ -0,0 +1,13 @@ +@throws(Not_found) +let lookup: int => int + +@throws(JsExn) +external parseIface: string => int = "parseThrowing" + +// The interface has no @throws, and it is what the .cmi holds. +let implOnly: int => int + +// The interface lists fewer exceptions than the implementation. Callers see +// the interface. +@throws(Not_found) +let narrowed: int => int diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/Yojson.res b/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/Yojson.res deleted file mode 100644 index 85dfbcc18e5..00000000000 --- a/tests/analysis_tests/tests-reanalyze/deadcode/src/exception/Yojson.res +++ /dev/null @@ -1,19 +0,0 @@ -exception Json_error(string) - -module Basic = { - type t - - @throws(Json_error) - let from_string: string => t = _ => throw(Json_error("Basic.from_string")) - - module Util = { - exception Type_error(string, t) - - @throws(Type_error) - let member: (string, t) => t = (_s, j) => throw(Type_error("Basic.Util.member", j)) - - let to_int: t => int = _ => 34 - - let to_string: t => string = _ => "" - } -} diff --git a/tests/analysis_tests/tests-reanalyze/deadcode/test.sh b/tests/analysis_tests/tests-reanalyze/deadcode/test.sh index a408b9da0a2..32f653fb4d6 100755 --- a/tests/analysis_tests/tests-reanalyze/deadcode/test.sh +++ b/tests/analysis_tests/tests-reanalyze/deadcode/test.sh @@ -1,5 +1,8 @@ DEBUG_FLAG="-debug" +# The exception analysis reads @throws from library declarations. +node ../checkThrowsAnnotations.mjs || exit 1 + if [ "$RUNNER_OS" == "Windows" ]; then exclude_dirs="src\exception" suppress="src\ToSuppress.res"