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
131 changes: 84 additions & 47 deletions analysis/reanalyze/src/exception.ml
Original file line number Diff line number Diff line change
Expand Up @@ -109,16 +109,17 @@ module Event = struct
if config.Dce_config.cli.debug then (
Log_.item "@.";
Log_.item "Events combine: #events %d@." (events |> List.length));
let exn_table = Hashtbl.create 1 in
let exn_table = Exn.Table.create 1 in
let extend_exn_table exn loc =
match Hashtbl.find_opt exn_table exn with
| Some loc_set -> Hashtbl.replace exn_table exn (Loc_set.add loc loc_set)
| None -> Hashtbl.replace exn_table exn (Loc_set.add loc Loc_set.empty)
match Exn.Table.find_opt exn_table exn with
| Some loc_set ->
Exn.Table.replace exn_table exn (Loc_set.add loc loc_set)
| None -> Exn.Table.replace exn_table exn (Loc_set.add loc Loc_set.empty)
in
let shrink_exn_table exn loc =
match Hashtbl.find_opt exn_table exn with
match Exn.Table.find_opt exn_table exn with
| Some loc_set ->
Hashtbl.replace exn_table exn (Loc_set.remove loc loc_set)
Exn.Table.replace exn_table exn (Loc_set.remove loc loc_set)
| None -> ()
in
let rec loop exn_set events =
Expand Down Expand Up @@ -188,28 +189,79 @@ and check = {
loc: Location.t;
loc_full: Location.t;
module_name: string;
scope: string list;
(** The module the check's code is in, as [[File; M; N]] for [File.M.N] *)
exn_name: string;
exceptions: Exceptions.t;
}

let create_checks_builder () : checks_builder = ref []

let checks_builder_add (builder : checks_builder) ~events ~exceptions ~loc
?(loc_full = loc) ~module_name exn_name =
?(loc_full = loc) ~module_name ~scope exn_name =
builder :=
{events; exceptions; loc; loc_full; module_name; exn_name} :: !builder
{events; exceptions; loc; loc_full; module_name; scope; exn_name}
:: !builder

let checks_builder_to_list (builder : checks_builder) : check list =
!builder |> List.rev

module Checks = struct
let do_check ~(values_table : values_table) ~config
{events; exceptions; loc; loc_full; module_name; exn_name} =
{events; exceptions; loc; loc_full; module_name; scope; exn_name} =
let throw_set, exn_table =
events |> Event.combine ~values_table ~config ~module_name
in
let missing_annotations = Exceptions.diff throw_set exceptions in
let redundant_annotations = Exceptions.diff exceptions throw_set in
(* The compiler names exceptions by their full path, as [Lib.M.E].
Messages name them as the code they are about would: [E] in [Lib.M],
[M.E] elsewhere in [Lib]. *)
let prefixes =
let rec go rev_scope =
match rev_scope with
| [] -> []
| _ :: outer ->
(String.concat "." (List.rev rev_scope) ^ ".") :: go outer
in
go (List.rev scope)
in
let shorten exn =
let name = Exn.to_string exn in
match
prefixes
|> List.find_opt (fun prefix -> String.starts_with ~prefix name)
with
| Some prefix ->
Exn.with_name
(String.sub name (String.length prefix)
(String.length name - String.length prefix))
exn
| None -> exn
in
(* Two different exceptions, as [M.E] and an inner [M.N.E], keep their
names within the file when shortening would give them the same one. *)
let shortened = ref [] in
Exceptions.union throw_set exceptions
|> Exceptions.iter (fun exn ->
shortened := Exn.to_string (shorten exn) :: !shortened);
let relative exn =
let short = shorten exn in
let name = Exn.to_string short in
if List.length (List.filter (String.equal name) !shortened) > 1 then
let name = Exn.to_string exn in
let prefix = module_name ^ "." in
if String.starts_with ~prefix name then
Exn.with_name
(String.sub name (String.length prefix)
(String.length name - String.length prefix))
exn
else exn
else short
in
let throw_set = Exceptions.map relative throw_set in
let missing_annotations = Exceptions.map relative missing_annotations in
let redundant_annotations = Exceptions.map relative redundant_annotations in
(if not (Exceptions.is_empty missing_annotations) then
let description =
Issue.ExceptionAnalysisMissing
Expand Down Expand Up @@ -263,8 +315,8 @@ let traverse_ast ~file ~values_builder ~checks_builder () =
|> List.fold_left
(fun acc desc ->
match desc with
| Typedtree.Tpat_construct ({txt}, _, _) ->
Exceptions.add (Exn.from_lid txt) acc
| Typedtree.Tpat_construct ({txt}, cstr, _) ->
Exceptions.add (Exn.of_constructor txt cstr) acc
| _ -> acc)
Exceptions.empty
in
Expand Down Expand Up @@ -308,8 +360,8 @@ let traverse_ast ~file ~values_builder ~checks_builder () =
in
let throw_args args =
match args with
| [(_, Some {Typedtree.exp_desc = Texp_construct ({txt}, _, _)})] ->
[Exn.from_lid txt] |> Exceptions.from_list
| [(_, Some {Typedtree.exp_desc = Texp_construct ({txt}, cstr, _)})] ->
[Exn.of_constructor txt cstr] |> Exceptions.from_list
| [(_, Some {Typedtree.exp_desc = Texp_ident _})] ->
[Exn.from_string "genericException"] |> Exceptions.from_list
| _ -> [Exn.from_string "TODO_from_raise1"] |> Exceptions.from_list
Expand All @@ -319,29 +371,13 @@ 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
(* The type checker resolves the names a [@throws] lists where they are
written, and records the exceptions with the declaration. *)
let exceptions_of_throws (throws : Types.throws list) =
throws
|> List.map (fun ({throws_name; throws_identity} : Types.throws) ->
Exn.make ~id:throws_identity ~name:throws_name)
|> Exceptions.from_list
in
let expr ~(module_path : Module_path.t) (self : Tast_mapper.mapper)
(expr : Typedtree.expression) =
Expand Down Expand Up @@ -369,7 +405,7 @@ let traverse_ast ~file ~values_builder ~checks_builder () =
from other modules and packages. *)
current_events :=
{
Event.exceptions = get_exceptions_from_annotations vd.val_attributes;
Event.exceptions = exceptions_of_throws vd.val_throws;
loc;
kind = Call {callee; module_path = module_path.path};
}
Expand Down Expand Up @@ -447,8 +483,8 @@ let traverse_ast ~file ~values_builder ~checks_builder () =
:: old_events);
expr
in
let toplevel_eval (self : Tast_mapper.mapper) (expr : Typedtree.expression)
attributes =
let toplevel_eval ~(module_path : Module_path.t) (self : Tast_mapper.mapper)
(expr : Typedtree.expression) throws =
let old_id = !current_id in
let old_events = !current_events in
let name = "Toplevel expression" in
Expand All @@ -457,8 +493,10 @@ let traverse_ast ~file ~values_builder ~checks_builder () =
let module_name = file.File_context.module_name in
self.expr self expr |> ignore;
checks_builder_add checks_builder ~events:!current_events
~exceptions:(get_exceptions_from_annotations attributes)
~loc:expr.exp_loc ~module_name name;
~exceptions:(exceptions_of_throws throws)
~loc:expr.exp_loc ~module_name
~scope:(module_name :: List.rev_map Name.to_string module_path.path)
name;
current_id := old_id;
current_events := old_events
in
Expand All @@ -475,9 +513,7 @@ let traverse_ast ~file ~values_builder ~checks_builder () =
let process_binding name =
current_id := name;
current_events := [];
let exceptions_from_annotations =
get_exceptions_from_annotations vb.vb_attributes
in
let exceptions_from_annotations = exceptions_of_throws vb.vb_throws in
values_builder_add values_builder ~module_path ~name
exceptions_from_annotations;
let res = super.value_binding self vb in
Expand All @@ -488,8 +524,9 @@ let traverse_ast ~file ~values_builder ~checks_builder () =
| Some exceptions -> exceptions
| _ -> Exceptions.empty
in
let scope = module_name :: List.rev_map Name.to_string module_path.path in
checks_builder_add checks_builder ~events:!current_events ~exceptions
~loc:vb.vb_pat.pat_loc ~loc_full:vb.vb_loc ~module_name name;
~loc:vb.vb_pat.pat_loc ~loc_full:vb.vb_loc ~module_name ~scope name;
current_id := old_id;
current_events := old_events;
res
Expand Down Expand Up @@ -536,8 +573,8 @@ let traverse_ast ~file ~values_builder ~checks_builder () =
let mapper = make_mapper mp in
let mp' =
match structure_item.str_desc with
| Tstr_eval (expr, attributes) ->
toplevel_eval mapper expr attributes;
| Tstr_eval (expr, _attributes, throws) ->
toplevel_eval ~module_path:mp mapper expr throws;
mp
| Tstr_module {mb_id; mb_loc; mb_expr} -> (
let name = mb_id |> Ident.name |> Name.create in
Expand Down
4 changes: 2 additions & 2 deletions analysis/reanalyze/src/exceptions.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ let add = Exn_set.add
let diff = Exn_set.diff
let empty = Exn_set.empty
let from_list = Exn_set.of_list
let to_list = Exn_set.elements
let is_empty = Exn_set.is_empty
let map = Exn_set.map
let iter = Exn_set.iter
let union = Exn_set.union

Expand All @@ -19,7 +19,7 @@ let pp ~exn_table ppf exceptions =
let name = Exn.to_string exn in
match exn_table with
| Some exn_table -> (
match Hashtbl.find_opt exn_table exn with
match Exn.Table.find_opt exn_table exn with
| Some loc_set ->
let positions =
loc_set |> Loc_set.elements
Expand Down
32 changes: 26 additions & 6 deletions analysis/reanalyze/src/exn.ml
Original file line number Diff line number Diff line change
@@ -1,7 +1,27 @@
type t = string
(* An exception is its identity, which the type checker records on its
definition and on each [@throws] that names it (see [Throws_attribute]),
and a name to show in messages. So [Lib.E] and [E] after [open Lib] are
equal. *)
type t = {id: string; name: string}

let compare = String.compare
let match_failure = "Match_failure"
let from_lid lid = lid |> Longident.flatten |> String.concat "."
let from_string s = s
let to_string s = s
let compare a b = String.compare a.id b.id
let make ~id ~name = {id; name}
let from_string s = {id = s; name = s}
let match_failure = from_string "Match_failure"
let to_string e = e.name

(* The exception a pattern or expression constructs, named as written *)
let of_constructor lid (cstr : Types.constructor_description) =
let name = lid |> Longident.flatten |> String.concat "." in
match Throws_attribute.identity cstr with
| Some id -> {id; name}
| None -> from_string name

let with_name name e = {e with name}

module Table = Hashtbl.Make (struct
type nonrec t = t

let equal a b = String.equal a.id b.id
let hash a = Hashtbl.hash a.id
end)
9 changes: 8 additions & 1 deletion analysis/reanalyze/src/exn.mli
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
type t

val compare : t -> t -> int
val from_lid : Longident.t -> t
val make : id:string -> name:string -> t
val from_string : string -> t
val match_failure : t
val to_string : t -> string

val of_constructor : Longident.t -> Types.constructor_description -> t
(** The exception a pattern or expression constructs, named as written *)

val with_name : string -> t -> t

module Table : Hashtbl.S with type key = t
2 changes: 1 addition & 1 deletion analysis/reanalyze/src/issue.ml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ module Exn_set = Set.Make (Exn)

type missing_throw_info = {
exn_name: string;
exn_table: (Exn.t, Loc_set.t) Hashtbl.t;
exn_table: Loc_set.t Exn.Table.t;
loc_full: Location.t;
missing_annotations: Exn_set.t;
throw_set: Exn_set.t;
Expand Down
1 change: 1 addition & 0 deletions changes/throws-names-are-dependencies.breaking.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- A module named in `@throws` is now a dependency of the module that names it, so a dependency cycle through such a name is an error. Move the exception to a module of its own.
1 change: 1 addition & 0 deletions changes/throws-resolved-in-type-checker.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- The compiler resolves the exceptions a `@throws` names where it is written, so exception analysis matches them with catches through opens, aliases, includes and functors, and warns (warning 114) about a name that is not an exception in scope.
1 change: 1 addition & 0 deletions compiler/typing/ctype.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4633,6 +4633,7 @@ let nondep_extension_constructor env mid ext =
ext_attributes = ext.ext_attributes;
ext_loc = ext.ext_loc;
ext_is_exception = ext.ext_is_exception;
ext_identity = ext.ext_identity;
})

let same_constr env t1 t2 =
Expand Down
2 changes: 2 additions & 0 deletions compiler/typing/datarepr.ml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,7 @@ let constructor_descrs ty_path decl cstrs =
cstr_loc = cd_loc;
cstr_attributes = cd_attributes;
cstr_inlined;
cstr_identity = None;
}
in
(cd_id, cstr) :: descr_rem
Expand Down Expand Up @@ -237,6 +238,7 @@ let extension_descr path_ext ext =
cstr_loc = ext.ext_loc;
cstr_attributes = ext.ext_attributes;
cstr_inlined;
cstr_identity = ext.ext_identity;
}

let none = {desc = Ttuple []; level = -1; id = -1}
Expand Down
15 changes: 14 additions & 1 deletion compiler/typing/depend.ml
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ let add = add_parent

let addmodule bv lid = add_path bv lid.txt

(* The type checker looks up the exceptions a [@throws] names, so their
modules are dependencies, as for any other use. *)
let add_throws bv (attributes : Parsetree.attributes) =
attributes
|> List.iter (fun (({txt}, payload) : Parsetree.attribute) ->
if txt = "throws" then
Throws_attribute.names payload
|> List.iter (fun (lid, loc) -> add bv {Location.txt = lid; loc}))

let handle_extension ext =
match (fst ext).txt with
| "error" ->
Expand Down Expand Up @@ -370,6 +379,7 @@ and add_case bv {pc_lhs; pc_guard; pc_rhs} =
add_expr bv pc_rhs

and add_bindings recf bv pel =
List.iter (fun x -> add_throws bv x.pvb_attributes) pel;
let bv' = List.fold_left (fun bv x -> add_pattern bv x.pvb_pat) bv pel in
let bv = if recf = Recursive then bv' else bv in
List.iter (fun x -> add_expr bv x.pvb_expr) pel;
Expand Down Expand Up @@ -425,6 +435,7 @@ and add_sig_item (bv, m) item =
match item.psig_desc with
| Psig_value vd ->
add_type bv vd.pval_type;
add_throws bv vd.pval_attributes;
(bv, m)
| Psig_type (_, dcls) ->
List.iter (add_type_declaration bv) dcls;
Expand Down Expand Up @@ -507,14 +518,16 @@ and add_structure_binding bv item_list =

and add_struct_item (bv, m) item : _ String_map.t * _ String_map.t =
match item.pstr_desc with
| Pstr_eval (e, _attrs) ->
| Pstr_eval (e, attrs) ->
add_throws bv attrs;
add_expr bv e;
(bv, m)
| Pstr_value (rf, pel) ->
let bv = add_bindings rf bv pel in
(bv, m)
| Pstr_primitive vd ->
add_type bv vd.pval_type;
add_throws bv vd.pval_attributes;
(bv, m)
| Pstr_type (_, dcls) ->
List.iter (add_type_declaration bv) dcls;
Expand Down
1 change: 1 addition & 0 deletions compiler/typing/predef.ml
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ let common_initial_env add_type add_extension empty_env =
Parsetree.PStr [] );
];
ext_is_exception = true;
ext_identity = None;
}
in
empty_env
Expand Down
Loading
Loading