Introduce Terr in place of Tany for type errors
[hiphop-php.git] / hphp / hack / src / decl / decl_hint.ml
blob0d173aa29b1a363a88bd47468ee0cf57481b5790
1 (**
2 * Copyright (c) 2015, Facebook, Inc.
3 * All rights reserved.
5 * This source code is licensed under the BSD-style license found in the
6 * LICENSE file in the "hack" directory of this source tree. An additional grant
7 * of patent rights can be found in the PATENTS file in the same directory.
9 *)
11 (*****************************************************************************)
12 (* Converts a type hint into a type *)
13 (*****************************************************************************)
14 open Core
15 open Nast
16 open Typing_defs
18 (* Unpacking a hint for typing *)
20 let rec hint env (p, h) =
21 let h = hint_ p env h in
22 Typing_reason.Rhint p, h
24 and hint_ p env = function
25 | Hany -> Tany
26 | Hmixed -> Tmixed
27 | Hthis -> Tthis
28 | Harray (h1, h2) ->
29 if env.Decl_env.mode = FileInfo.Mstrict && h1 = None
30 then Errors.generic_array_strict p;
31 let h1 = Option.map h1 (hint env) in
32 let h2 = Option.map h2 (hint env) in
33 Tarray (h1, h2)
34 | Hprim p -> Tprim p
35 | Habstr x ->
36 Tgeneric x
37 | Hoption (_, Hprim Tvoid) ->
38 Errors.option_return_only_typehint p `void;
39 Terr
40 | Hoption (_, Hprim Tnoreturn) ->
41 Errors.option_return_only_typehint p `noreturn;
42 Terr
43 | Hoption (_, Hmixed) ->
44 Errors.option_mixed p;
45 Terr
46 | Hoption h ->
47 let h = hint env h in
48 Toption h
49 | Hfun (hl, b, h) ->
50 let paraml = List.map hl (hint env) in
51 let paraml = List.map paraml (fun x -> None, x) in
52 let ret = hint env h in
53 let arity_min = List.length paraml in
54 let arity = if b
55 then Fellipsis arity_min
56 else Fstandard (arity_min, arity_min)
58 Tfun {
59 ft_pos = p;
60 ft_deprecated = None;
61 ft_abstract = false;
62 ft_arity = arity;
63 ft_tparams = [];
64 ft_where_constraints = [];
65 ft_params = paraml;
66 ft_ret = ret;
68 | Happly ((p, "\\Tuple"), _)
69 | Happly ((p, "\\tuple"), _) ->
70 Errors.tuple_syntax p;
71 Terr
72 | Happly (((_p, c) as id), argl) ->
73 Decl_hooks.dispatch_class_id_hook id None;
74 Decl_env.add_wclass env c;
75 let argl = List.map argl (hint env) in
76 Tapply (id, argl)
77 | Haccess (root_ty, ids) ->
78 let root_ty = hint env root_ty in
79 Taccess (root_ty, ids)
80 | Htuple hl ->
81 let tyl = List.map hl (hint env) in
82 Ttuple tyl
83 | Hshape fdm ->
84 let fdm = ShapeMap.map (hint env) fdm in
85 (* Fields are only partially known, because this shape type comes from
86 * type hint - shapes that contain listed fields can be passed here, but
87 * due to structural subtyping they can also contain other fields, that we
88 * don't know about. *)
89 Tshape (FieldsPartiallyKnown ShapeMap.empty, fdm)