Introduce __ReturnsVoidToRx
[hiphop-php.git] / hphp / hack / src / typing / typing_disposable.ml
blob8091ba2f7e420d8bbd465bfe47eade48509eb167
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 (* Typing code concerned with disposable types. *)
12 open Hh_core
13 open Typing_defs
15 module Env = Typing_env
17 let is_disposable_visitor env =
18 object(this)
19 inherit [string option] Type_visitor.type_visitor
20 (* Only bother looking at classish types. Other types can spuriously
21 * claim to implement these interfaces. Ideally we should check
22 * constrained generics, abstract types, etc.
24 method! on_tclass acc _ (_, class_name) tyl =
25 let default () =
26 List.fold_left tyl ~f:this#on_type ~init:acc in
27 begin match Env.get_class env class_name with
28 | None -> default ()
29 | Some c ->
30 if c.tc_is_disposable
31 then Some (Utils.strip_ns class_name)
32 else default ()
33 end
34 end
36 (* Does ty (or a type embedded in ty) implement IDisposable
37 * or IAsyncDisposable, directly or indirectly?
38 * Return Some class_name if it does, None if it doesn't.
40 let is_disposable_type env ty =
41 match Env.expand_type env ty with
42 | _env, ety ->
43 (is_disposable_visitor env)#on_type None ety
45 let enforce_is_disposable env hint =
46 match hint with
47 | (_, Nast.Happly ((p, c), _)) ->
48 begin match Decl_env.get_class_dep env.Env.decl_env c with
49 | None -> ()
50 | Some c ->
51 if not c.Decl_defs.dc_is_disposable
52 then Errors.must_extend_disposable p
53 end
54 | _ -> ()
56 (* Ensure that `ty` is a subtype of IDisposable (for `using`) or
57 * IAsyncDisposable (for `await using`)
59 let enforce_is_disposable_type env has_await pos ty =
60 let class_name =
61 if has_await
62 then SN.Classes.cIAsyncDisposable
63 else SN.Classes.cIDisposable in
64 let disposable_ty = (Reason.Rusing pos, Tclass ((pos, class_name), [])) in
65 Typing_ops.sub_type pos Reason.URusing env ty disposable_ty