solved some TODOs about Tgeneric type arguments (2)
[hiphop-php.git] / hphp / hack / src / typing / nast_check / record_field_check.ml
blobb1bde7ed554458a450f86e67b2fb163b8ef7b558
1 (*
2 * Copyright (c) 2019, 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 Core_kernel
11 open Aast
13 let id_if_string expr : 'a option =
14 let (pos, expr_) = expr in
15 match expr_ with
16 | String s -> Some (pos, s)
17 | _ -> None
19 let check_duplicates (ids : Aast.sid list) : unit =
20 let _ =
21 List.fold ids ~init:SMap.empty ~f:(fun acc (pos, name) ->
22 (match SMap.find_opt name acc with
23 | Some prev_pos -> Errors.repeated_record_field name pos prev_pos
24 | None -> ());
25 SMap.add name pos acc)
29 let handler =
30 object
31 inherit Nast_visitor.handler_base as super
33 (* Ban duplicate fields in declarations: record Foo { x: int, x: int } *)
34 method! at_record_def _env rd =
35 let field_names = List.map rd.rd_fields ~f:(fun (id, _, _) -> id) in
36 check_duplicates field_names
38 (* Ban duplicate fields in instantiations: Foo['x' => 1, 'x' => 2]; *)
39 method! at_expr env (pos, e) =
40 match e with
41 | Aast.Record (_, fields) ->
42 let field_names =
43 List.map fields ~f:(fun (id, _) -> id_if_string id) |> List.filter_opt
45 check_duplicates field_names
46 | _ -> super#at_expr env (pos, e)
47 end