2 * Copyright (c) 2015, Facebook, Inc.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
12 module Reason
= Typing_reason
13 module Env
= Typing_env
14 module TUtils
= Typing_utils
15 module MakeType
= Typing_make_type
17 let is_object env ty
=
18 Typing_solver.is_sub_type env ty
(MakeType.ty_object
(get_reason ty
))
20 let sub_string (p
: Pos.Map.key
) (env
: env
) (ty
: locl_ty
) : env
=
21 (* Under constraint-based inference, we implement sub_string as a subtype test.
22 * All the cases in the legacy implementation just fall out from subtyping rules.
23 * We test against ?(arraykey | bool | float | resource | object | dynamic |
24 * HH\FormatString<T>).
26 let r = Reason.Rwitness p
in
27 let (env
, formatter_tyvar
) = Env.fresh_invariant_type_var env p
in
35 MakeType.class_type
r SN.Classes.cHHFormatString
[formatter_tyvar
];
38 let stringish = MakeType.class_type
r SN.Classes.cStringish
[] in
39 let stringlike = MakeType.nullable_locl
r (MakeType.union
r tyl) in
40 Typing_subtype.sub_type_or_fail env ty
stringlike (fun () ->
41 if Typing_solver.is_sub_type env ty
stringish then
42 Errors.object_string_deprecated p
43 else if is_object env ty
then
44 Errors.object_string p
(get_pos ty
)
46 Errors.invalid_sub_string p
(Typing_print.error env ty
))