-
Notifications
You must be signed in to change notification settings - Fork 0
/
errors.ml
1731 lines (1452 loc) · 64.5 KB
/
errors.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
(**
* Copyright (c) 2014, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the "hack" directory of this source tree. An additional grant
* of patent rights can be found in the PATENTS file in the same directory.
*
*)
open Utils
(*****************************************************************************)
(* Types *)
(*****************************************************************************)
type error_code = int
(* We use `Pos.t message` on the server and convert to `Pos.absolute message`
* before sending it to the client *)
type 'a message = 'a * string
type 'a error_ = error_code * 'a message list
type error = Pos.t error_
type t = error list
(*****************************************************************************)
(* HH_FIXMEs hook *)
(*****************************************************************************)
let (is_hh_fixme: (Pos.t -> error_code -> bool) ref) = ref (fun _ _ -> false)
(*****************************************************************************)
(* Errors accumulator. *)
(*****************************************************************************)
let (error_list: t ref) = ref []
let accumulate_errors = ref false
let add_error error =
if !accumulate_errors
then error_list := error :: !error_list
else
(* We have an error, but haven't handled it in any way *)
(* TODO: Can't get this to work together with error at no { for if-statements *)
(*assert false*)
()
let add code pos msg =
if !is_hh_fixme pos code then () else
add_error (code, [pos, msg])
let add_list code pos_msg_l =
let pos = fst (List.hd pos_msg_l) in
if !is_hh_fixme pos code then () else
add_error (code, pos_msg_l)
(*****************************************************************************)
(* Accessors. *)
(*****************************************************************************)
let get_code (error: 'a error_) = ((fst error): error_code)
let get_pos (error : error) = fst (List.hd (snd error))
let to_list (error : 'a error_) = snd error
let make_error code (x: (Pos.t * string) list) = ((code, x): error)
(*****************************************************************************)
(* Error code printing. *)
(*****************************************************************************)
let error_kind error_code =
match error_code / 1000 with
| 1 -> "Parsing"
| 2 -> "Naming"
| 3 -> "NastCheck"
| 4 -> "Typing"
| 5 -> "Lint"
| _ -> "Other"
let error_code_to_string error_code =
let error_kind = error_kind error_code in
let error_number = string_of_int error_code in
error_kind^"["^error_number^"]"
(*****************************************************************************)
(* Error codes.
* Each error has a unique number associated with it. The following modules
* define the error code associated with each kind of error.
* It is ok to extend the codes with new values, it is NOT OK to change the
* value of an existing error to a different error code!
* I added some comments to make that extra clear :-)
*)
(*****************************************************************************)
module Parsing = struct
let fixme_format = 1001 (* DONT MODIFY!!!! *)
let parsing_error = 1002 (* DONT MODIFY!!!! *)
let unexpected_eof = 1003 (* DONT MODIFY!!!! *)
let unterminated_comment = 1004 (* DONT MODIFY!!!! *)
let unterminated_xhp_comment = 1005 (* DONT MODIFY!!!! *)
(* EXTEND HERE WITH NEW VALUES IF NEEDED *)
end
module Naming = struct
let add_a_typehint = 2001 (* DONT MODIFY!!!! *)
let typeparam_alok = 2002 (* DONT MODIFY!!!! *)
let assert_arity = 2003 (* DONT MODIFY!!!! *)
let primitive_invalid_alias = 2004 (* DONT MODIFY!!!! *)
let cyclic_constraint = 2005 (* DONT MODIFY!!!! *)
let did_you_mean_naming = 2006 (* DONT MODIFY!!!! *)
let different_scope = 2007 (* DONT MODIFY!!!! *)
let disallowed_xhp_type = 2008 (* DONT MODIFY!!!! *)
(* DEPRECATED let double_instead_of_float = 2009 *)
(* DEPRECATED let dynamic_class = 2010 *)
let dynamic_method_call = 2011 (* DONT MODIFY!!!! *)
let error_name_already_bound = 2012 (* DONT MODIFY!!!! *)
let expected_collection = 2013 (* DONT MODIFY!!!! *)
let expected_variable = 2014 (* DONT MODIFY!!!! *)
let fd_name_already_bound = 2015 (* DONT MODIFY!!!! *)
let gen_array_rec_arity = 2016 (* DONT MODIFY!!!! *)
(* let gen_array_va_rec_arity = 2017 *)
let gena_arity = 2018 (* DONT MODIFY!!!! *)
let generic_class_var = 2019 (* DONT MODIFY!!!! *)
let genva_arity = 2020 (* DONT MODIFY!!!! *)
let illegal_CLASS = 2021 (* DONT MODIFY!!!! *)
let illegal_class_meth = 2022 (* DONT MODIFY!!!! *)
let illegal_constant = 2023 (* DONT MODIFY!!!! *)
let illegal_fun = 2024 (* DONT MODIFY!!!! *)
let illegal_inst_meth = 2025 (* DONT MODIFY!!!! *)
let illegal_meth_caller = 2026 (* DONT MODIFY!!!! *)
let illegal_meth_fun = 2027 (* DONT MODIFY!!!! *)
(* DEPRECATED integer_instead_of_int = 2028 *)
let invalid_req_extends = 2029 (* DONT MODIFY!!!! *)
let invalid_req_implements = 2030 (* DONT MODIFY!!!! *)
let local_const = 2031 (* DONT MODIFY!!!! *)
let lowercase_this = 2032 (* DONT MODIFY!!!! *)
let method_name_already_bound = 2033 (* DONT MODIFY!!!! *)
let missing_arrow = 2034 (* DONT MODIFY!!!! *)
let missing_typehint = 2035 (* DONT MODIFY!!!! *)
let name_already_bound = 2036 (* DONT MODIFY!!!! *)
let naming_too_few_arguments = 2037 (* DONT MODIFY!!!! *)
let naming_too_many_arguments = 2038 (* DONT MODIFY!!!! *)
let primitive_toplevel = 2039 (* DONT MODIFY!!!! *)
(* DEPRECATED let real_instead_of_float = 2040 *)
let shadowed_type_param = 2041 (* DONT MODIFY!!!! *)
let start_with_T = 2042 (* DONT MODIFY!!!! *)
let this_must_be_return = 2043 (* DONT MODIFY!!!! *)
let this_no_argument = 2044 (* DONT MODIFY!!!! *)
let this_hint_outside_class = 2045 (* DONT MODIFY!!!! *)
let this_reserved = 2046 (* DONT MODIFY!!!! *)
let tparam_with_tparam = 2047 (* DONT MODIFY!!!! *)
let typedef_constraint = 2048 (* DONT MODIFY!!!! *)
let unbound_name = 2049 (* DONT MODIFY!!!! *)
let undefined = 2050 (* DONT MODIFY!!!! *)
let unexpected_arrow = 2051 (* DONT MODIFY!!!! *)
let unexpected_typedef = 2052 (* DONT MODIFY!!!! *)
let using_internal_class = 2053 (* DONT MODIFY!!!! *)
let void_cast = 2054 (* DONT MODIFY!!!! *)
let object_cast = 2055 (* DONT MODIFY!!!! *)
let unset_cast = 2056 (* DONT MODIFY!!!! *)
(* DEPRECATED let nullsafe_property_access = 2057 *)
let illegal_TRAIT = 2058 (* DONT MODIFY!!!! *)
(* DEPRECATED let shape_typehint = 2059 *)
let dynamic_new_in_strict_mode = 2060 (* DONT MODIFY!!!! *)
let invalid_type_access_root = 2061 (* DONT MODIFY!!!! *)
let duplicate_user_attribute = 2062 (* DONT MODIFY!!!! *)
let return_only_typehint = 2063 (* DONT MODIFY!!!! *)
(* EXTEND HERE WITH NEW VALUES IF NEEDED *)
end
module NastCheck = struct
let abstract_body = 3001 (* DONT MODIFY!!!! *)
let abstract_with_body = 3002 (* DONT MODIFY!!!! *)
let await_in_sync_function = 3003 (* DONT MODIFY!!!! *)
let call_before_init = 3004 (* DONT MODIFY!!!! *)
let case_fallthrough = 3005 (* DONT MODIFY!!!! *)
let continue_in_switch = 3006 (* DONT MODIFY!!!! *)
let dangerous_method_name = 3007 (* DONT MODIFY!!!! *)
let default_fallthrough = 3008 (* DONT MODIFY!!!! *)
let interface_with_member_variable = 3009 (* DONT MODIFY!!!! *)
let interface_with_static_member_variable = 3010 (* DONT MODIFY!!!! *)
let magic = 3011 (* DONT MODIFY!!!! *)
let no_construct_parent = 3012 (* DONT MODIFY!!!! *)
let non_interface = 3013 (* DONT MODIFY!!!! *)
let not_abstract_without_body = 3014 (* DONT MODIFY!!!! *)
let not_initialized = 3015 (* DONT MODIFY!!!! *)
let not_public_interface = 3016 (* DONT MODIFY!!!! *)
let requires_non_class = 3017 (* DONT MODIFY!!!! *)
let return_in_finally = 3018 (* DONT MODIFY!!!! *)
let return_in_gen = 3019 (* DONT MODIFY!!!! *)
let toString_returns_string = 3020 (* DONT MODIFY!!!! *)
let toString_visibility = 3021 (* DONT MODIFY!!!! *)
let toplevel_break = 3022 (* DONT MODIFY!!!! *)
let toplevel_continue = 3023 (* DONT MODIFY!!!! *)
let uses_non_trait = 3024 (* DONT MODIFY!!!! *)
let illegal_function_name = 3025 (* DONT MODIFY!!!! *)
let not_abstract_without_typeconst = 3026 (* DONT MODIFY!!!! *)
let typeconst_depends_on_external_tparam = 3027 (* DONT MODIFY!!!! *)
let typeconst_assigned_tparam = 3028 (* DONT MODIFY!!!! *)
let abstract_with_typeconst = 3029 (* DONT MODIFY!!!! *)
let constructor_required = 3030 (* DONT MODIFY!!!! *)
(* EXTEND HERE WITH NEW VALUES IF NEEDED *)
end
module Typing = struct
(* let abstract_class_final = 4001 (\* DONT MODIFY!!!! *\) *)
let uninstantiable_class = 4002 (* DONT MODIFY!!!! *)
let anonymous_recursive = 4003 (* DONT MODIFY!!!! *)
let anonymous_recursive_call = 4004 (* DONT MODIFY!!!! *)
let array_access = 4005 (* DONT MODIFY!!!! *)
let array_append = 4006 (* DONT MODIFY!!!! *)
let array_cast = 4007 (* DONT MODIFY!!!! *)
let array_get_arity = 4008 (* DONT MODIFY!!!! *)
let bad_call = 4009 (* DONT MODIFY!!!! *)
let class_arity = 4010 (* DONT MODIFY!!!! *)
let const_mutation = 4011 (* DONT MODIFY!!!! *)
let constructor_no_args = 4012 (* DONT MODIFY!!!! *)
let cyclic_class_def = 4013 (* DONT MODIFY!!!! *)
let cyclic_typedef = 4014 (* DONT MODIFY!!!! *)
let discarded_awaitable = 4015 (* DONT MODIFY!!!! *)
let isset_empty_in_strict = 4016 (* DONT MODIFY!!!! *)
(* DEPRECATED dynamic_yield_private = 4017 *)
let enum_constant_type_bad = 4018 (* DONT MODIFY!!!! *)
let enum_switch_nonexhaustive = 4019 (* DONT MODIFY!!!! *)
let enum_switch_not_const = 4020 (* DONT MODIFY!!!! *)
let enum_switch_redundant = 4021 (* DONT MODIFY!!!! *)
let enum_switch_redundant_default = 4022 (* DONT MODIFY!!!! *)
let enum_switch_wrong_class = 4023 (* DONT MODIFY!!!! *)
let enum_type_bad = 4024 (* DONT MODIFY!!!! *)
let enum_type_typedef_mixed = 4025 (* DONT MODIFY!!!! *)
let expected_class = 4026 (* DONT MODIFY!!!! *)
let expected_literal_string = 4027 (* DONT MODIFY!!!! *)
(* DEPRECATED expected_static_int = 4028 *)
let expected_tparam = 4029 (* DONT MODIFY!!!! *)
let expecting_return_type_hint = 4030 (* DONT MODIFY!!!! *)
let expecting_return_type_hint_suggest = 4031 (* DONT MODIFY!!!! *)
let expecting_type_hint = 4032 (* DONT MODIFY!!!! *)
let expecting_type_hint_suggest = 4033 (* DONT MODIFY!!!! *)
let extend_final = 4035 (* DONT MODIFY!!!! *)
let field_kinds = 4036 (* DONT MODIFY!!!! *)
(* DEPRECATED field_missing = 4037 *)
let format_string = 4038 (* DONT MODIFY!!!! *)
let fun_arity_mismatch = 4039 (* DONT MODIFY!!!! *)
let fun_too_few_args = 4040 (* DONT MODIFY!!!! *)
let fun_too_many_args = 4041 (* DONT MODIFY!!!! *)
let fun_unexpected_nonvariadic = 4042 (* DONT MODIFY!!!! *)
let fun_variadicity_hh_vs_php56 = 4043 (* DONT MODIFY!!!! *)
let gena_expects_array = 4044 (* DONT MODIFY!!!! *)
let generic_array_strict = 4045 (* DONT MODIFY!!!! *)
let generic_static = 4046 (* DONT MODIFY!!!! *)
let implement_abstract = 4047 (* DONT MODIFY!!!! *)
let interface_final = 4048 (* DONT MODIFY!!!! *)
let invalid_shape_field_const = 4049 (* DONT MODIFY!!!! *)
let invalid_shape_field_literal = 4050 (* DONT MODIFY!!!! *)
let invalid_shape_field_name = 4051 (* DONT MODIFY!!!! *)
let invalid_shape_field_type = 4052 (* DONT MODIFY!!!! *)
let member_not_found = 4053 (* DONT MODIFY!!!! *)
let member_not_implemented = 4054 (* DONT MODIFY!!!! *)
let missing_assign = 4055 (* DONT MODIFY!!!! *)
let missing_constructor = 4056 (* DONT MODIFY!!!! *)
let missing_field = 4057 (* DONT MODIFY!!!! *)
(* DEPRECATED negative_tuple_index = 4058 *)
let self_outside_class = 4059 (* DONT MODIFY!!!! *)
let new_static_inconsistent = 4060 (* DONT MODIFY!!!! *)
let static_outside_class = 4061 (* DONT MODIFY!!!! *)
let non_object_member = 4062 (* DONT MODIFY!!!! *)
let null_container = 4063 (* DONT MODIFY!!!! *)
let null_member = 4064 (* DONT MODIFY!!!! *)
let nullable_parameter = 4065 (* DONT MODIFY!!!! *)
let option_return_only_typehint = 4066 (* DONT MODIFY!!!! *)
let object_string = 4067 (* DONT MODIFY!!!! *)
let option_mixed = 4068 (* DONT MODIFY!!!! *)
let overflow = 4069 (* DONT MODIFY!!!! *)
let override_final = 4070 (* DONT MODIFY!!!! *)
let override_per_trait = 4071 (* DONT MODIFY!!!! *)
let pair_arity = 4072 (* DONT MODIFY!!!! *)
let abstract_call = 4073 (* DONT MODIFY!!!! *)
let parent_in_trait = 4074 (* DONT MODIFY!!!! *)
let parent_outside_class = 4075 (* DONT MODIFY!!!! *)
let parent_undefined = 4076 (* DONT MODIFY!!!! *)
let previous_default = 4077 (* DONT MODIFY!!!! *)
let private_class_meth = 4078 (* DONT MODIFY!!!! *)
let private_inst_meth = 4079 (* DONT MODIFY!!!! *)
let private_override = 4080 (* DONT MODIFY!!!! *)
let protected_class_meth = 4081 (* DONT MODIFY!!!! *)
let protected_inst_meth = 4082 (* DONT MODIFY!!!! *)
let read_before_write = 4083 (* DONT MODIFY!!!! *)
let return_in_void = 4084 (* DONT MODIFY!!!! *)
let shape_field_class_mismatch = 4085 (* DONT MODIFY!!!! *)
let shape_field_type_mismatch = 4086 (* DONT MODIFY!!!! *)
let should_be_override = 4087 (* DONT MODIFY!!!! *)
let sketchy_null_check = 4088 (* DONT MODIFY!!!! *)
let sketchy_null_check_primitive = 4089 (* DONT MODIFY!!!! *)
let smember_not_found = 4090 (* DONT MODIFY!!!! *)
let static_dynamic = 4091 (* DONT MODIFY!!!! *)
(* DEPRECATED let static_overflow = 4092 *)
let this_in_static = 4094 (* DONT MODIFY!!!! *)
let this_var_outside_class = 4095 (* DONT MODIFY!!!! *)
let trait_final = 4096 (* DONT MODIFY!!!! *)
let tuple_arity = 4097 (* DONT MODIFY!!!! *)
let tuple_arity_mismatch = 4098 (* DONT MODIFY!!!! *)
(* DEPRECATED tuple_index_too_large = 4099 *)
let tuple_syntax = 4100 (* DONT MODIFY!!!! *)
let type_arity_mismatch = 4101 (* DONT MODIFY!!!! *)
let type_param_arity = 4102 (* DONT MODIFY!!!! *)
let typing_too_few_args = 4104 (* DONT MODIFY!!!! *)
let typing_too_many_args = 4105 (* DONT MODIFY!!!! *)
let unbound_global = 4106 (* DONT MODIFY!!!! *)
let unbound_name_typing = 4107 (* DONT MODIFY!!!! *)
let undefined_field = 4108 (* DONT MODIFY!!!! *)
let undefined_parent = 4109 (* DONT MODIFY!!!! *)
let unify_error = 4110 (* DONT MODIFY!!!! *)
let unsatisfied_req = 4111 (* DONT MODIFY!!!! *)
let visibility = 4112 (* DONT MODIFY!!!! *)
let visibility_extends = 4113 (* DONT MODIFY!!!! *)
(* DEPRECATED void_parameter = 4114 *)
let wrong_extend_kind = 4115 (* DONT MODIFY!!!! *)
let generic_unify = 4116 (* DONT MODIFY!!!! *)
let nullsafe_not_needed = 4117 (* DONT MODIFY!!!! *)
let trivial_strict_eq = 4118 (* DONT MODIFY!!!! *)
let void_usage = 4119 (* DONT MODIFY!!!! *)
let declared_covariant = 4120 (* DONT MODIFY!!!! *)
let declared_contravariant = 4121 (* DONT MODIFY!!!! *)
(* DEPRECATED unset_in_strict = 4122 *)
let strict_members_not_known = 4123 (* DONT MODIFY!!!! *)
let generic_at_runtime = 4124 (* DONT MODIFY!!!! *)
let dynamic_class = 4125 (* DONT MODIFY!!!! *)
let attribute_arity = 4126 (* DONT MODIFY!!!! *)
let attribute_param_type = 4127 (* DONT MODIFY!!!! *)
let deprecated_use = 4128 (* DONT MODIFY!!!! *)
let abstract_const_usage = 4129 (* DONT MODIFY!!!! *)
let cannot_declare_constant = 4130 (* DONT MODIFY!!!! *)
let cyclic_typeconst = 4131 (* DONT MODIFY!!!! *)
let nullsafe_property_write_context = 4132 (* DONT MODIFY!!!! *)
let noreturn_usage = 4133 (* DONT MODIFY!!!! *)
let this_lvalue = 4134 (* DONT MODIFY!!!! *)
let unset_nonidx_in_strict = 4135 (* DONT MODIFY!!!! *)
let invalid_shape_field_name_empty = 4136 (* DONT MODIFY!!!! *)
let invalid_shape_field_name_number = 4137 (* DONT MODIFY!!!! *)
let shape_fields_unknown = 4138 (* DONT MODIFY!!!! *)
(* EXTEND HERE WITH NEW VALUES IF NEEDED *)
end
(*****************************************************************************)
(* Parsing errors. *)
(*****************************************************************************)
let fixme_format pos =
add Parsing.fixme_format pos
"HH_FIXME wrong format, expected '/* HH_FIXME[ERROR_NUMBER] */'"
let unexpected_eof pos =
add Parsing.unexpected_eof pos "Unexpected end of file"
let unterminated_comment pos =
add Parsing.unterminated_comment pos "unterminated comment"
let unterminated_xhp_comment pos =
add Parsing.unterminated_xhp_comment pos "unterminated xhp comment"
let parsing_error (p, msg) =
add Parsing.parsing_error p msg
(*****************************************************************************)
(* Naming errors *)
(*****************************************************************************)
let typeparam_alok (pos, x) =
add Naming.typeparam_alok pos (
"You probably forgot to bind this type parameter right?\nAdd <"^x^
"> somewhere (after the function name definition, \
or after the class name)\nExamples: "^"function foo<T> or class A<T>")
let generic_class_var pos =
add Naming.generic_class_var pos
"A class variable cannot be generic"
let unexpected_arrow pos cname =
add Naming.unexpected_arrow pos (
"Keys may not be specified for "^cname^" initialization"
)
let missing_arrow pos cname =
add Naming.missing_arrow pos (
"Keys must be specified for "^cname^" initialization"
)
let disallowed_xhp_type pos name =
add Naming.disallowed_xhp_type pos (
name^" is not a valid type. Use :xhp or XHPChild."
)
let name_already_bound name pos1 pos2 =
let name = Utils.strip_ns name in
add_list Naming.name_already_bound [
pos1, "Name already bound: "^name;
pos2, "Previous definition is here"
]
let method_name_already_bound pos name =
add Naming.method_name_already_bound pos (
"Method name already bound: "^name
)
let error_name_already_bound name name_prev p p_prev =
let name = Utils.strip_ns name in
let name_prev = Utils.strip_ns name_prev in
let errs = [
p, "Name already bound: "^name;
p_prev, (if String.compare name name_prev == 0
then "Previous definition is here"
else "Previous definition "^name_prev^" differs only in capitalization ")
] in
let hhi_msg =
"This appears to be defined in an hhi file included in your project "^
"root. The hhi files for the standard library are now a part of the "^
"typechecker and must be removed from your project. Typically, you can "^
"do this by deleting the \"hhi\" directory you copied into your "^
"project when first starting with Hack." in
let errs =
if (Relative_path.prefix p.Pos.pos_file) = Relative_path.Hhi
then errs @ [p_prev, hhi_msg]
else if (Relative_path.prefix p_prev.Pos.pos_file) = Relative_path.Hhi
then errs @ [p, hhi_msg]
else errs in
add_list Naming.error_name_already_bound errs
let unbound_name pos name kind =
let kind_str = match kind with
| `cls -> "an object type"
| `func -> "a global function"
| `const -> "a global constant"
in
add Naming.unbound_name pos
("Unbound name: "^(strip_ns name)^" ("^kind_str^")")
let different_scope pos var_name pos' =
add_list Naming.different_scope [
pos, ("The variable "^ var_name ^" is defined");
pos', ("But in a different scope")
]
let undefined pos var_name =
add Naming.undefined pos ("Undefined variable: "^var_name)
let this_reserved pos =
add Naming.this_reserved pos
"The type parameter \"this\" is reserved"
let start_with_T pos =
add Naming.start_with_T pos
"Please make your type parameter start with the letter T (capital)"
let already_bound pos name =
add Naming.name_already_bound pos ("Argument already bound: "^name)
let unexpected_typedef pos def_pos =
add_list Naming.unexpected_typedef [
pos, "Unexpected typedef";
def_pos, "Definition is here";
]
let fd_name_already_bound pos =
add Naming.fd_name_already_bound pos
"Field name already bound"
let primitive_toplevel pos =
add Naming.primitive_toplevel pos (
"Primitive type annotations are always available and may no \
longer be referred to in the toplevel namespace."
)
let primitive_invalid_alias pos used valid =
add Naming.primitive_invalid_alias pos
("Invalid Hack type. Using '"^used^"' in Hack is considered \
an error. Use '"^valid^"' instead, to keep the codebase \
consistent.")
let dynamic_new_in_strict_mode pos =
add Naming.dynamic_new_in_strict_mode pos
"Cannot use dynamic new in strict mode"
let invalid_type_access_root (pos, id) =
add Naming.invalid_type_access_root pos
(id^" must be an identifier for a class, \"self\", or \"this\"")
let duplicate_user_attribute (pos, name) existing_attr_pos =
add_list Naming.duplicate_user_attribute [
pos, "You cannot reuse the attribute "^name;
existing_attr_pos, name^" was already used here";
]
let unbound_attribute_name pos name =
let reason = if (str_starts_with name "__")
then "starts with __ but is not a standard attribute"
else "is not listed in .hhconfig"
in add Naming.unbound_name pos
("Unrecognized user attribute: "^name^" "^reason)
let this_no_argument pos =
add Naming.this_no_argument pos "\"this\" expects no arguments"
let void_cast pos =
add Naming.void_cast pos "Cannot cast to void."
let unset_cast pos =
add Naming.unset_cast pos "Don't use (unset), just assign null!"
let object_cast pos x =
add Naming.object_cast pos ("Object casts are unsupported. "^
"Try 'if ($var instanceof "^x^")' or "^
"'invariant($var instanceof "^x^", ...)'.")
let this_hint_outside_class pos =
add Naming.this_hint_outside_class pos
"Cannot use \"this\" outside of a class"
let this_must_be_return pos =
add Naming.this_must_be_return pos
"The type \"this\" can only be used as a return type, \
to instantiate a covariant type variable, \
or as a private non-static member variable"
let lowercase_this pos type_ =
add Naming.lowercase_this pos (
"Invalid Hack type \""^type_^"\". Use \"this\" instead"
)
let tparam_with_tparam pos x =
add Naming.tparam_with_tparam pos (
Printf.sprintf "%s is a type parameter. Type parameters cannot \
themselves take type parameters (e.g. %s<int> doesn't make sense)" x x
)
let shadowed_type_param p pos name =
add_list Naming.shadowed_type_param [
p, Printf.sprintf "You cannot re-bind the type parameter %s" name;
pos, Printf.sprintf "%s is already bound here" name
]
let missing_typehint pos =
add Naming.missing_typehint pos
"Please add a type hint"
let expected_variable pos =
add Naming.expected_variable pos
"Was expecting a variable name"
let naming_too_few_arguments pos =
add Naming.naming_too_few_arguments pos
"Too few arguments"
let naming_too_many_arguments pos =
add Naming.naming_too_many_arguments pos
"Too many arguments"
let expected_collection pos cn =
add Naming.expected_collection pos (
"Unexpected collection type " ^ (Utils.strip_ns cn)
)
let illegal_CLASS pos =
add Naming.illegal_CLASS pos
"Using __CLASS__ outside a class or trait"
let illegal_TRAIT pos =
add Naming.illegal_TRAIT pos
"Using __TRAIT__ outside a trait"
let dynamic_method_call pos =
add Naming.dynamic_method_call pos
"Dynamic method call"
let nullsafe_property_write_context pos =
add Typing.nullsafe_property_write_context pos
"?-> syntax not supported here, this function effectively does a write"
let illegal_fun pos =
let msg = "The argument to fun() must be a single-quoted, constant "^
"literal string representing a valid function name." in
add Naming.illegal_fun pos msg
let illegal_meth_fun pos =
let msg = "String argument to fun() contains ':';"^
" for static class methods, use"^
" class_meth(Cls::class, 'method_name'), not fun('Cls::method_name')" in
add Naming.illegal_meth_fun pos msg
let illegal_inst_meth pos =
let msg = "The argument to inst_meth() must be an expression and a "^
"constant literal string representing a valid method name." in
add Naming.illegal_inst_meth pos msg
let illegal_meth_caller pos =
let msg =
"The two arguments to meth_caller() must be:"
^"\n - first: ClassOrInterface::class"
^"\n - second: a single-quoted string literal containing the name"
^" of a non-static method of that class" in
add Naming.illegal_meth_caller pos msg
let illegal_class_meth pos =
let msg =
"The two arguments to class_meth() must be:"
^"\n - first: ValidClassname::class"
^"\n - second: a single-quoted string literal containing the name"
^" of a static method of that class" in
add Naming.illegal_class_meth pos msg
let assert_arity pos =
add Naming.assert_arity pos
"assert expects exactly one argument"
let gena_arity pos =
add Naming.gena_arity pos
"gena() expects exactly 1 argument"
let genva_arity pos =
add Naming.genva_arity pos
"genva() expects at least 1 argument"
let gen_array_rec_arity pos =
add Naming.gen_array_rec_arity pos
"gen_array_rec() expects exactly 1 argument"
let dynamic_class pos =
add Typing.dynamic_class pos
"Don't use dynamic classes"
let uninstantiable_class usage_pos decl_pos name =
let name = strip_ns name in
add_list Typing.uninstantiable_class [
usage_pos, (name^" is uninstantiable");
decl_pos, "Declaration is here"
]
let abstract_const_usage usage_pos decl_pos name =
let name = strip_ns name in
add_list Typing.abstract_const_usage [
usage_pos, ("Cannot reference abstract constant "^name^" directly");
decl_pos, "Declaration is here"
]
let typedef_constraint pos =
add Naming.typedef_constraint pos
"Constraints on typedefs are not supported"
let add_a_typehint pos =
add Naming.add_a_typehint pos
"Please add a type hint"
let local_const var_pos =
add Naming.local_const var_pos
"You cannot use a local variable in a constant definition"
let illegal_constant pos =
add Naming.illegal_constant pos
"Illegal constant value"
let cyclic_constraint pos =
add Naming.cyclic_constraint pos
"Cyclic constraint"
let invalid_req_implements pos =
add Naming.invalid_req_implements pos
"Only traits may use 'require implements'"
let invalid_req_extends pos =
add Naming.invalid_req_extends pos
"Only traits and interfaces may use 'require extends'"
let did_you_mean_naming pos name suggest_pos suggest_name =
add_list Naming.did_you_mean_naming [
pos, "Could not find "^(strip_ns name);
suggest_pos, "Did you mean "^(strip_ns suggest_name)^"?"
]
let using_internal_class pos name =
add Naming.using_internal_class pos (
name^" is an implementation internal class that cannot be used directly"
)
(*****************************************************************************)
(* Init check errors *)
(*****************************************************************************)
let no_construct_parent pos =
add NastCheck.no_construct_parent pos (
sl["You are extending a class that needs to be initialized\n";
"Make sure you call parent::__construct.\n"
]
)
let constructor_required (pos, name) prop_names =
let name = Utils.strip_ns name in
let props_str = SSet.fold (fun x acc -> x^" "^acc) prop_names "" in
add NastCheck.constructor_required pos
("Lacking __construct, class "^name^" does not initialize its private member(s): "^props_str)
let not_initialized (pos, cname) prop_names =
let cname = Utils.strip_ns cname in
let props_str = SSet.fold (fun x acc -> x^" "^acc) prop_names "" in
let members, verb = if 1 == SSet.cardinal prop_names then "member", "is"
else "members", "are" in
let setters_str = SSet.fold (fun x acc -> "$this->"^x^" "^acc) prop_names "" in
add NastCheck.not_initialized pos (
sl[
"Class "; cname ; " does not initialize all of its members; ";
props_str; verb; " not always initialized.";
"\nMake sure you systematically set "; setters_str;
"when the method __construct is called.";
"\nAlternatively, you can define the "; members ;" as optional (?...)\n"
])
let call_before_init pos cv =
add NastCheck.call_before_init pos (
sl([
"Until the initialization of $this is over,";
" you can only call private methods\n";
"The initialization is not over because ";
] @
if cv = "parent::__construct"
then ["you forgot to call parent::__construct"]
else ["$this->"; cv; " can still potentially be null"])
)
(*****************************************************************************)
(* Nast errors check *)
(*****************************************************************************)
let type_arity pos name nargs =
add Typing.type_arity_mismatch pos (
sl["The type ";(Utils.strip_ns name);
" expects ";nargs;" type parameter(s)"]
)
let abstract_with_body (p, _) =
add NastCheck.abstract_with_body p
"This method is declared as abstract, but has a body"
let not_abstract_without_body (p, _) =
add NastCheck.not_abstract_without_body p
"This method is not declared as abstract, it must have a body"
let not_abstract_without_typeconst (p, _) =
add NastCheck.not_abstract_without_typeconst p
("This type constant is not declared as abstract, it must have"^
" an assigned type")
let abstract_with_typeconst (p, _) =
add NastCheck.abstract_with_typeconst p
("This type constant is declared as abstract, it cannot be assigned a type")
let typeconst_depends_on_external_tparam pos ext_pos ext_name =
add_list NastCheck.typeconst_depends_on_external_tparam [
pos, ("A type constant can only use type parameters declared in its own"^
" type parameter list");
ext_pos, (ext_name ^ " was declared as a type parameter here");
]
let typeconst_assigned_tparam pos tp_name =
add NastCheck.typeconst_assigned_tparam pos
(tp_name ^" is a type parameter. It cannot be assigned to a type constant")
let return_in_gen p =
add NastCheck.return_in_gen p
("You cannot return a value in a generator (a generator"^
" is a function that uses yield)")
let return_in_finally p =
add NastCheck.return_in_finally p
("Don't use return in a finally block;"^
" there's nothing to receive the return value")
let toplevel_break p =
add NastCheck.toplevel_break p
"break can only be used inside loops or switch statements"
let toplevel_continue p =
add NastCheck.toplevel_continue p
"continue can only be used inside loops"
let continue_in_switch p =
add NastCheck.continue_in_switch p
("In PHP, 'continue;' inside a switch \
statement is equivalent to 'break;'."^
" Hack does not support this; use 'break' if that is what you meant.")
let await_in_sync_function p =
add NastCheck.await_in_sync_function p
"await can only be used inside async functions"
let magic (p, s) =
add NastCheck.magic p
("Don't call "^s^" it's one of these magic things we want to avoid")
let non_interface (p : Pos.t) (c2: string) (verb: string): 'a =
add NastCheck.non_interface p
("Cannot " ^ verb ^ " " ^ (strip_ns c2) ^ " - it is not an interface")
let toString_returns_string pos =
add NastCheck.toString_returns_string pos "__toString should return a string"
let toString_visibility pos =
add NastCheck.toString_visibility pos
"__toString must have public visibility and cannot be static"
let uses_non_trait (p: Pos.t) (n: string) (t: string) =
add NastCheck.uses_non_trait p
((Utils.strip_ns n) ^ " is not a trait. It is " ^ t ^ ".")
let requires_non_class (p: Pos.t) (n: string) (t: string) =
add NastCheck.requires_non_class p
((Utils.strip_ns n) ^ " is not a class. It is " ^ t ^ ".")
let abstract_body pos =
add NastCheck.abstract_body pos "This method shouldn't have a body"
let not_public_interface pos =
add NastCheck.not_public_interface pos
"Access type for interface method must be public"
let interface_with_member_variable pos =
add NastCheck.interface_with_member_variable pos
"Interfaces cannot have member variables"
let interface_with_static_member_variable pos =
add NastCheck.interface_with_static_member_variable pos
"Interfaces cannot have static variables"
let illegal_function_name pos mname =
add NastCheck.illegal_function_name pos
("Illegal function name: " ^ strip_ns mname)
let dangerous_method_name pos =
add NastCheck.dangerous_method_name pos (
"This is a dangerous method name, "^
"if you want to define a constructor, use "^
"__construct"
)
(*****************************************************************************)
(* Nast terminality *)
(*****************************************************************************)
let case_fallthrough pos1 pos2 =
add_list NastCheck.case_fallthrough [
pos1, ("This switch has a case that implicitly falls through and is "^
"not annotated with // FALLTHROUGH");
pos2, "This case implicitly falls through"
]
let default_fallthrough pos =
add NastCheck.default_fallthrough pos
("This switch has a default case that implicitly falls "^
"through and is not annotated with // FALLTHROUGH")
(*****************************************************************************)
(* Typing errors *)
(*****************************************************************************)
let visibility_extends vis pos parent_pos parent_vis =
let msg1 = pos, "This member visibility is: " ^ vis in
let msg2 = parent_pos, parent_vis ^ " was expected" in
add_list Typing.visibility_extends [msg1; msg2]
let member_not_implemented member_name parent_pos pos defn_pos =
let msg1 = pos, "This object doesn't implement the method "^member_name in
let msg2 = parent_pos, "Which is required by this interface" in
let msg3 = defn_pos, "As defined here" in
add_list Typing.member_not_implemented [msg1; msg2; msg3]
let override parent_pos parent_name pos name (error: error) =
let msg1 = pos, ("This object is of type "^(strip_ns name)) in
let msg2 = parent_pos,
("It is incompatible with this object of type "^(strip_ns parent_name)^
"\nbecause some declarations are incompatible."^
"\nRead the following to see why:"
) in
(* This is a cascading error message *)
let code, msgl = error in
add_list code (msg1 :: msg2 :: msgl)
let missing_constructor pos =
add Typing.missing_constructor pos
"The constructor is not implemented"
let typedef_trail_entry pos =
pos, "Typedef definition comes from here"
let add_with_trail code errs trail =
add_list code (errs @ List.map typedef_trail_entry trail)
let enum_constant_type_bad pos ty_pos ty trail =
add_with_trail Typing.enum_constant_type_bad
[pos, "Enum constants must be an int or string";
ty_pos, "Not " ^ ty]
trail
let enum_type_bad pos ty trail =
add_with_trail Typing.enum_type_bad
[pos, "Enums must be int or string, not " ^ ty]
trail
let enum_type_typedef_mixed pos =
add Typing.enum_type_typedef_mixed pos
"Can't use typedef that resolves to mixed in enum"
let enum_switch_redundant const first_pos second_pos =
add_list Typing.enum_switch_redundant [
second_pos, "Redundant case statement";
first_pos, const ^ " already handled here"
]
let enum_switch_nonexhaustive pos missing enum_pos =
add_list Typing.enum_switch_nonexhaustive [
pos, "Switch statement nonexhaustive; the following cases are missing: " ^
String.concat ", " missing;
enum_pos, "Enum declared here"
]
let enum_switch_redundant_default pos enum_pos =
add_list Typing.enum_switch_redundant_default [
pos, "All cases already covered; a redundant default case prevents "^
"detecting future errors";
enum_pos, "Enum declared here"
]
let enum_switch_not_const pos =
add Typing.enum_switch_not_const pos
"Case in switch on enum is not an enum constant"
let enum_switch_wrong_class pos expected got =
add Typing.enum_switch_wrong_class pos
("Switching on enum " ^ expected ^ " but using constant from " ^ got)
let invalid_shape_field_name p =
add Typing.invalid_shape_field_name p
"Was expecting a constant string or class constant (for shape access)"
let invalid_shape_field_name_empty p =
add Typing.invalid_shape_field_name_empty p
"A shape field name cannot be an empty string"
let invalid_shape_field_name_number p =
add Typing.invalid_shape_field_name_number p
"A shape field name cannot start with numbers"
let invalid_shape_field_type pos ty_pos ty trail =
add_with_trail Typing.invalid_shape_field_type
[pos, "A shape field name must be an int or string";
ty_pos, "Not " ^ ty]
trail
let invalid_shape_field_literal key_pos witness_pos =
add_list Typing.invalid_shape_field_literal
[key_pos, "Shape uses literal string as field name";
witness_pos, "But expected a class constant"]
let invalid_shape_field_const key_pos witness_pos =
add_list Typing.invalid_shape_field_const
[key_pos, "Shape uses class constant as field name";
witness_pos, "But expected a literal string"]
let shape_field_class_mismatch key_pos witness_pos key_class witness_class =
add_list Typing.shape_field_class_mismatch
[key_pos, "Shape field name is class constant from " ^ key_class;
witness_pos, "But expected constant from " ^ witness_class]
let shape_field_type_mismatch key_pos witness_pos key_ty witness_ty =
add_list Typing.shape_field_type_mismatch
[key_pos, "Shape field name is " ^ key_ty ^ " class constant";
witness_pos, "But expected " ^ witness_ty]
let missing_field pos1 pos2 name =
add_list Typing.missing_field
[pos1, "The field '"^name^"' is missing";
pos2, "The field '"^name^"' is defined"]
let shape_fields_unknown pos1 pos2 =
add_list Typing.shape_fields_unknown
[pos1, "This is a shape type coming from a type annotation. Because of " ^
"structural subtyping it might have some other fields besides " ^
"those listed in its declaration.";
pos2, "It is incompatible with a shape created using \"shape\" "^
"constructor, which has all the fields known"]
let explain_constraint p_inst pos name (error : error) =
let inst_msg = "Some type constraint(s) here are violated" in
let code, msgl = error in
(* There may be multiple constraints instantiated at one spot; avoid
* duplicating the instantiation message *)
let msgl = match msgl with
| (p, x) :: rest when x = inst_msg && p = p_inst -> rest
| _ -> msgl in
let name = Utils.strip_ns name in
add_list code begin
[p_inst, inst_msg;