Skip to content

Commit

Permalink
[flow] Show signature verification errors for builtin type sig
Browse files Browse the repository at this point in the history
Summary:
It turns out we are not exposing these errors at all. For libdefs it's mostly fine since things like `const foo = new Set()` is already banned, but it will be important for the next diff where I raise new kinds of errors.

Changelog: [errors] Signature verification errors will now show up for libdef files

Reviewed By: gkz

Differential Revision: D70303552

fbshipit-source-id: ce37ed169b8c7ecc47a801f302e4a887b371080f
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed Feb 27, 2025
1 parent 0cbd61d commit 3f4af12
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/flow_dot_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ let load_lib_files files =
locs_to_dirtify = [];
}
in
Merge_js.merge_lib_files ~sig_opts asts |> snd
let (_, _, master_cx) = Merge_js.merge_lib_files ~sig_opts asts in
master_cx

let stub_metadata ~root ~checked =
{
Expand Down
3 changes: 2 additions & 1 deletion src/services/inference/init_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ let load_lib_files ~ccx ~options ~reader files =
let (builtin_exports, master_cx, cx_opt) =
if ok then
let sig_opts = Type_sig_options.builtin_options options in
let (builtins, master_cx) = Merge_js.merge_lib_files ~sig_opts ordered_asts in
let (builtins, builtin_errors, master_cx) = Merge_js.merge_lib_files ~sig_opts ordered_asts in
let cx_opt =
match master_cx with
| Context.EmptyMasterContext -> None
Expand All @@ -66,6 +66,7 @@ let load_lib_files ~ccx ~options ~reader files =
(fun mref -> Context.MissingModule mref)
mk_builtins
in
Context.reset_errors cx builtin_errors;
Some cx
in
(Exports.of_builtins builtins, master_cx, cx_opt)
Expand Down
3 changes: 2 additions & 1 deletion src/typing/__tests__/type_hint_test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ end = struct
locs_to_dirtify = [];
}
in
Merge_js.merge_lib_files ~sig_opts asts |> snd
let (_, _, master_cx) = Merge_js.merge_lib_files ~sig_opts asts in
master_cx

let master_cx_ref = ref None

Expand Down
28 changes: 25 additions & 3 deletions src/typing/merge_js.ml
Original file line number Diff line number Diff line change
Expand Up @@ -866,14 +866,36 @@ let module_type_copied dst_cx src_cx ({ Type.module_export_types; module_global_
{ m with Type.module_global_types_tmap }

let merge_lib_files ~sig_opts ordered_asts =
let (_builtin_errors, builtin_locs, builtins) =
let (builtin_errors, builtin_locs, builtins) =
Type_sig_utils.parse_and_pack_builtins sig_opts ordered_asts
in
let builtin_errors =
builtin_errors
|> Base.List.filter_map ~f:(function
| Type_sig.SigError e ->
let e =
Signature_error.map
(fun l -> l |> Type_sig_collections.Locs.get builtin_locs |> ALoc.of_loc)
e
in
let msg = Error_message.ESignatureVerification e in
let source_file =
Base.Option.value_exn
(msg |> Error_message.loc_of_msg |> Base.Option.bind ~f:ALoc.source)
in
Some (Flow_error.error_of_msg ~source_file msg)
| Type_sig.CheckError -> None
)
|> Flow_error.ErrorSet.of_list
in
match ordered_asts with
| [] -> (builtins, Context.EmptyMasterContext)
| [] -> (builtins, builtin_errors, Context.EmptyMasterContext)
| fst_ast :: _ ->
let builtin_leader_file_key = Base.Option.value_exn (fst_ast |> fst |> Loc.source) in
(builtins, Context.NonEmptyMasterContext { builtin_leader_file_key; builtin_locs; builtins })
( builtins,
builtin_errors,
Context.NonEmptyMasterContext { builtin_leader_file_key; builtin_locs; builtins }
)

let mk_builtins metadata master_cx =
match master_cx with
Expand Down
4 changes: 3 additions & 1 deletion src/typing/merge_js.mli
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ val copy_into :
val merge_lib_files :
sig_opts:Type_sig_options.t ->
(Loc.t, Loc.t) Flow_ast.Program.t list ->
Type_sig_collections.Locs.index Packed_type_sig.Builtins.t * Context.master_context
Type_sig_collections.Locs.index Packed_type_sig.Builtins.t
* Flow_error.ErrorSet.t
* Context.master_context

val mk_builtins : Context.metadata -> Context.master_context -> Context.t -> Builtins.t

0 comments on commit 3f4af12

Please sign in to comment.