Skip to content

Commit

Permalink
[flow] Make builtin lookup's any from error have a generic `RImplicit…
Browse files Browse the repository at this point in the history
…Any` description

Summary:
Since it's any, these description won't be visible to user in any ways. So let's just give it a more generic description, instead of letting callsites construct one without understanding the requirement.

Changelog: [internal]

Reviewed By: panagosg7

Differential Revision: D56799561

fbshipit-source-id: 571e16d32bb44d52d2e8e3b34bb9165a40ee53fc
  • Loading branch information
SamChou19815 authored and facebook-github-bot committed May 1, 2024
1 parent f727f42 commit 8b29a1f
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
19 changes: 10 additions & 9 deletions src/typing/flow_js_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -746,33 +746,34 @@ let emit_cacheable_env_error cx loc err =
in
add_output cx err

let lookup_builtin_module_error cx module_name reason =
let lookup_builtin_module_error cx module_name loc =
let potential_generator =
Context.missing_module_generators cx
|> Base.List.find ~f:(fun (pattern, _) -> Str.string_match pattern module_name 0)
|> Base.Option.map ~f:snd
in
add_output
cx
(Error_message.EBuiltinModuleLookupFailed
{ loc = loc_of_reason reason; potential_generator; name = module_name }
);
AnyT.error_of_kind UnresolvedName reason
(Error_message.EBuiltinModuleLookupFailed { loc; potential_generator; name = module_name });
AnyT.error_of_kind UnresolvedName (mk_reason RAnyImplicit loc)

let lookup_builtin_name_error name reason =
Error (AnyT.error_of_kind UnresolvedName reason, Nel.one (Env_api.BuiltinNameLookupFailed name))
let lookup_builtin_name_error name loc =
Error
( AnyT.error_of_kind UnresolvedName (mk_reason RAnyImplicit loc),
Nel.one (Env_api.BuiltinNameLookupFailed name)
)

let lookup_builtin_value_result cx x reason =
let builtins = Context.builtins cx in
match Builtins.get_builtin_value_opt builtins x with
| Some t -> Ok (TypeUtil.mod_reason_of_t (Base.Fn.const reason) t)
| None -> lookup_builtin_name_error x reason
| None -> lookup_builtin_name_error x (loc_of_reason reason)

let lookup_builtin_type_result cx x reason =
let builtins = Context.builtins cx in
match Builtins.get_builtin_type_opt builtins x with
| Some t -> Ok (TypeUtil.mod_reason_of_t (Base.Fn.const reason) t)
| None -> lookup_builtin_name_error x reason
| None -> lookup_builtin_name_error x (loc_of_reason reason)

let apply_env_errors cx loc = function
| Ok t -> t
Expand Down
5 changes: 2 additions & 3 deletions src/typing/type_annotation.ml
Original file line number Diff line number Diff line change
Expand Up @@ -1019,13 +1019,12 @@ module Make (ConsGen : Type_annotation_sig.ConsGen) (Statement : Statement_sig.S
}
) ->
let { Ast.StringLiteral.value; _ } = str_lit in
let reason = mk_annot_reason (RCommonJSExports value) loc in
let remote_module_t = Flow_js_utils.get_builtin_module cx value reason in
let remote_module_t = Flow_js_utils.get_builtin_module cx value loc in
let str_t = mk_singleton_string str_loc value in
reconstruct_ast
(Type_operation_utils.Import_export.cjs_require_type
cx
reason
(mk_annot_reason (RCommonJSExports value) loc)
~legacy_interop:false
remote_module_t
)
Expand Down
6 changes: 3 additions & 3 deletions src/typing/type_operation_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ module Import_export = struct
Flow_js_utils.add_output cx message

let get_module_t cx ?(perform_platform_validation = false) (loc, mref) =
let reason = Reason.(mk_reason (RCustom mref) loc) in
if Context.in_declare_module cx then
Flow_js_utils.get_builtin_module cx mref reason
Flow_js_utils.get_builtin_module cx mref loc
else
let module_t =
match Context.find_require cx mref with
| Ok t -> t
| Error m_name -> Flow_js_utils.lookup_builtin_module_error cx m_name reason
| Error m_name -> Flow_js_utils.lookup_builtin_module_error cx m_name loc
in
( if perform_platform_validation && Files.multi_platform Context.((metadata cx).file_options)
then
let reason = Reason.(mk_reason (RCustom mref) loc) in
match Flow_js.possible_concrete_types_for_inspection cx reason module_t with
| [ModuleT m] -> check_platform_availability cx reason m.module_available_platforms
| _ -> ()
Expand Down
2 changes: 1 addition & 1 deletion src/typing/type_sig_merge.ml
Original file line number Diff line number Diff line change
Expand Up @@ -773,8 +773,8 @@ and merge_annot env file = function
let id = eval_id_of_aloc file loc in
Type.(EvalT (t1, TypeDestructorT (use_op, reason, RestType (Object.Rest.Sound, t2)), id))
| ExportsT (loc, ref) ->
let module_t = Flow_js_utils.get_builtin_module file.cx ref loc in
let reason = Reason.(mk_annot_reason (RModule (OrdinaryName ref)) loc) in
let module_t = Flow_js_utils.get_builtin_module file.cx ref reason in
ConsGen.cjs_require file.cx module_t reason false false
| Conditional
{ loc; distributive_tparam; infer_tparams; check_type; extends_type; true_type; false_type }
Expand Down

0 comments on commit 8b29a1f

Please sign in to comment.