Skip to content

Commit 18b891e

Browse files
CopilotSimn
andcommitted
Replace overly-wide exception catches with specific exception types
Based on 517a97a (latest development commit). Each broad | _ -> catch in the display/typing pipeline is replaced with an enumeration of only the specific compilation-error exception types that the try block legitimately expects to raise. Any exception not in the list naturally bubbles up without requiring per-exception holes. Co-authored-by: Simn <634365+Simn@users.noreply.github.com>
1 parent 517a97a commit 18b891e

10 files changed

Lines changed: 18 additions & 17 deletions

File tree

src/compiler/compiler.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ with
447447
error com ("Error: No completion point was found") null_pos
448448
| DisplayException.DisplayException dex ->
449449
DisplayOutput.handle_display_exception com dex
450-
| CompilerMessage.Abort | Out_of_memory | EvalTypes.Sys_exit _ | Hlinterp.Sys_exit _ | DisplayJson.JsonCompleted as exc ->
450+
| CompilerMessage.Abort | Out_of_memory | EvalTypes.Sys_exit _ | Hlinterp.Sys_exit _ | DisplayJson.JsonCompleted | Globals.Cancelled as exc ->
451451
(* We don't want these to be caught by the catchall below *)
452452
raise exc
453453
| e when (try Sys.getenv "OCAMLRUNPARAM" <> "b" with _ -> true) && not Helper.is_debug_run ->

src/compiler/displayProcessing.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -135,9 +135,8 @@ let load_display_module_in_macro tctx display_file_dot_path clear = match displa
135135
let _ = MacroContext.load_macro_module (MacroContext.get_macro_context tctx) tctx.com cpath true p in
136136
Finalization.finalize mctx;
137137
Some mctx
138-
with DisplayException.DisplayException _ | Parser.TypePath _ | DisplayJson.JsonCompleted as exc ->
139-
raise exc
140-
| _ ->
138+
with Error.Fatal_error _ | Error.Error _ | Failure _ | Not_found
139+
| Lexer.Error _ | Parser.Error _ | Typecore.Forbid_package _ | Typecore.WithTypeError _ ->
141140
None
142141
end
143142
| None ->

src/context/commonCache.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class lib_build_task cs file ftime lib = object(self)
1515
try begin match lib#build path p with
1616
| Some r -> Hashtbl.add h path r
1717
| None -> ()
18-
end with _ ->
18+
end with Error.Fatal_error _ | Error.Error _ | Failure _ | Not_found | Invalid_argument _ ->
1919
()
2020
end
2121
) lib#list_modules;

src/context/display/display.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,5 +90,5 @@ let get_import_status ctx path =
9090
try
9191
let mt' = ctx.g.do_load_type_def ctx null_pos (mk_type_path ([],snd path)) in
9292
if path <> (t_infos mt').mt_path then Shadowed else Imported
93-
with _ ->
93+
with Not_found | Error.Fatal_error _ | Error.Error _ | Failure _ ->
9494
Unimported

src/context/display/displayFields.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,7 @@ let handle_missing_field_raise ctx tthis i mode with_type pfield =
381381
begin try
382382
let e = type_expr ctx e WithType.value in
383383
e.etype
384-
with _ ->
384+
with Error.Fatal_error _ | Error.Error _ | Typecore.Forbid_package _ | Typecore.WithTypeError _ | Failure _ ->
385385
mk_mono()
386386
end
387387
| _ -> mk_mono()
@@ -393,7 +393,7 @@ let handle_missing_field_raise ctx tthis i mode with_type pfield =
393393
begin try
394394
let e = type_expr ctx e WithType.value in
395395
e.etype
396-
with _ ->
396+
with Error.Fatal_error _ | Error.Error _ | Typecore.Forbid_package _ | Typecore.WithTypeError _ | Failure _ ->
397397
raise Exit
398398
end
399399
| _ -> raise Exit
@@ -410,7 +410,7 @@ let handle_missing_field_raise ctx tthis i mode with_type pfield =
410410
(name,false,e.etype)
411411
) el in
412412
(TFun(tl,tret),Method MethNormal)
413-
with _ ->
413+
with Error.Fatal_error _ | Error.Error _ | Typecore.Forbid_package _ | Typecore.WithTypeError _ | Failure _ ->
414414
raise Exit
415415
end
416416
| MGet ->

src/context/display/displayPath.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ module TypePathHandler = struct
155155
) en.e_constrs fields
156156
in
157157
Some fields
158-
with _ ->
158+
with Error.Fatal_error _ | Error.Error _ | Failure _ | Not_found
159+
| Lexer.Error _ | Parser.Error _ | Typecore.Forbid_package _ ->
159160
Error.abort ("Could not load module " ^ (s_type_path (p,c))) null_pos
160161
end
161162

src/context/display/displayToplevel.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ let maybe_resolve_macro_field ctx t c cf =
4949
let (tl,tr,c,cf) = ctx.g.do_load_macro ctx false c.cl_path cf.cf_name null_pos in
5050
let t = perform_type_voodoo t tl tr in
5151
t,{cf with cf_type = t}
52-
with _ ->
52+
with Exit | Not_found | Error.Fatal_error _ | Error.Error _ | Failure _ ->
5353
t,cf
5454

5555
let exclude : string list ref = ref []
@@ -157,7 +157,7 @@ let init_or_update_server cs com timer_name =
157157
try
158158
ignore(cc#find_file file_key);
159159
with Not_found ->
160-
try ignore(TypeloadParse.parse_module_file com file_path null_pos) with _ -> ()
160+
try ignore(TypeloadParse.parse_module_file com file_path null_pos) with Lexer.Error _ | Parser.Error _ | Failure _ | Error.Error _ | Error.Fatal_error _ -> ()
161161
) removed_files;
162162
DynArray.iter (Hashtbl.remove removed_files) removed_removed_files
163163

src/context/display/syntaxExplorer.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,8 @@ let explore_uncached_modules tctx cs symbols =
179179
(* We have to flush immediately so we catch exceptions from weird modules *)
180180
Typecore.flush_pass tctx.g PFinal ("final",cfile.c_package @ [module_name]);
181181
m :: acc
182-
with _ ->
182+
with Error.Fatal_error _ | Error.Error _ | Failure _ | Not_found
183+
| Lexer.Error _ | Parser.Error _ | Typecore.Forbid_package _ | Typecore.WithTypeError _ ->
183184
acc
184185
end
185186
) files []

src/typing/macroContext.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ let make_macro_com_api com mcom p =
102102
match ParserEntry.parse_string (ParserConfig.default_config com.defines) Grammar.parse_meta s null_pos raise_typing_error false with
103103
| ParseSuccess(meta,_) -> meta
104104
| ParseError(_,_,_) -> raise_typing_error "Malformed metadata string" p
105-
with _ ->
105+
with Lexer.Error _ | Failure _ ->
106106
raise_typing_error "Malformed metadata string" p
107107
in
108108
let bad_stage () =
@@ -311,7 +311,7 @@ let make_macro_api ctx mctx p =
311311
match ParserEntry.parse_string (ParserConfig.default_config mctx.com.defines) Grammar.parse_meta s null_pos raise_typing_error false with
312312
| ParseSuccess(meta,_) -> meta
313313
| ParseError(_,_,_) -> raise_typing_error "Malformed metadata string" p
314-
with _ ->
314+
with Lexer.Error _ | Failure _ ->
315315
raise_typing_error "Malformed metadata string" p
316316
in
317317
let com_api = make_macro_com_api ctx.com mctx.com p in

src/typing/typerDisplay.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ let completion_item_of_expr ctx e =
2929
try
3030
let e' = type_expr ctx (EConst(Ident s),null_pos) (WithType.with_type t) in
3131
Texpr.equal e e'
32-
with _ ->
32+
with Error.Fatal_error _ | Error.Error _ | Typecore.Forbid_package _ | Failure _ ->
3333
false
3434
in
3535
let tpair ?(values=PMap.empty) t =
@@ -581,7 +581,7 @@ let filter_ctors ctx r =
581581
| _ -> false)
582582
| _ -> false
583583
end
584-
with _ ->
584+
with Not_found | Error.Fatal_error _ | Error.Error _ | Failure _ ->
585585
false
586586
end
587587
end

0 commit comments

Comments
 (0)