Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 41 additions & 35 deletions analysis/reanalyze/src/exception.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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./"
Expand Down Expand Up @@ -316,14 +319,38 @@ 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
let is_does_no_throw = expr.exp_attributes |> does_not_throw in
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
Expand All @@ -337,9 +364,12 @@ let traverse_ast ~file ~values_builder ~checks_builder () =
"@{<info>%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;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[P2] Resolve declaration exception identities before transferring them to callers

For a library interface declaring exception E and @throws(E) let f: unit => unit, a consumer's try Lib.f() catch { | Lib.E => () } contributes the raw annotation string E here, but the catch is represented as Lib.E. Exn.from_string is identity, Exn.from_lid keeps the qualifier, and the catch subtracts sets of those strings, so the correctly caught exception is still reported as uncaught. Conversely, catching an unrelated local E can suppress the library exception. The existing new fixtures use globally named builtins and miss this case. Please resolve names in the declaration's defining scope and compare canonical exception identities on both sides, with a custom-library-exception regression.

loc;
kind = Call {callee; module_path = module_path.path};
}
Expand Down Expand Up @@ -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
Expand Down
8 changes: 0 additions & 8 deletions analysis/reanalyze/src/exn.ml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 0 additions & 8 deletions analysis/reanalyze/src/exn.mli
Original file line number Diff line number Diff line change
@@ -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
200 changes: 0 additions & 200 deletions analysis/reanalyze/src/exn_lib.ml

This file was deleted.

1 change: 1 addition & 0 deletions changes/reanalyze-throws-from-declarations.feature.md
Original file line number Diff line number Diff line change
@@ -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.
14 changes: 11 additions & 3 deletions compiler/typing/typemod.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 ->
Expand All @@ -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) ->
Expand Down
Loading
Loading