solved some TODOs about Tgeneric type arguments (2)
[hiphop-php.git] / hphp / hack / src / typing / typing_substring.ml
blob5a0b884d2b89bad75643a3fb353b06ed1a400229
1 (*
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the MIT license found in the
6 * LICENSE file in the "hack" directory of this source tree.
8 *)
10 open Typing_defs
11 open Typing_env_types
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
28 let tyl =
30 MakeType.arraykey r;
31 MakeType.bool r;
32 MakeType.float r;
33 MakeType.resource r;
34 MakeType.dynamic r;
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)
45 else
46 Errors.invalid_sub_string p (Typing_print.error env ty))