-
Notifications
You must be signed in to change notification settings - Fork 349
/
Error.cpp
987 lines (819 loc) · 28 KB
/
Error.cpp
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
// This file is part of the Luau programming language and is licensed under MIT License; see LICENSE.txt for details
#include "Luau/Error.h"
#include "Luau/Clone.h"
#include "Luau/Common.h"
#include "Luau/FileResolver.h"
#include "Luau/StringUtils.h"
#include "Luau/ToString.h"
#include <stdexcept>
#include <type_traits>
LUAU_FASTFLAGVARIABLE(LuauTypeMismatchInvarianceInError, false)
static std::string wrongNumberOfArgsString(
size_t expectedCount, std::optional<size_t> maximumCount, size_t actualCount, const char* argPrefix = nullptr, bool isVariadic = false)
{
std::string s = "expects ";
if (isVariadic)
s += "at least ";
s += std::to_string(expectedCount) + " ";
if (maximumCount && expectedCount != *maximumCount)
s += "to " + std::to_string(*maximumCount) + " ";
if (argPrefix)
s += std::string(argPrefix) + " ";
s += "argument";
if ((maximumCount ? *maximumCount : expectedCount) != 1)
s += "s";
s += ", but ";
if (actualCount == 0)
{
s += "none";
}
else
{
if (actualCount < expectedCount)
s += "only ";
s += std::to_string(actualCount);
}
s += (actualCount == 1) ? " is" : " are";
s += " specified";
return s;
}
namespace Luau
{
struct ErrorConverter
{
FileResolver* fileResolver = nullptr;
std::string operator()(const Luau::TypeMismatch& tm) const
{
std::string givenTypeName = Luau::toString(tm.givenType);
std::string wantedTypeName = Luau::toString(tm.wantedType);
std::string result;
if (givenTypeName == wantedTypeName)
{
if (auto givenDefinitionModule = getDefinitionModuleName(tm.givenType))
{
if (auto wantedDefinitionModule = getDefinitionModuleName(tm.wantedType))
{
if (fileResolver != nullptr)
{
std::string givenModuleName = fileResolver->getHumanReadableModuleName(*givenDefinitionModule);
std::string wantedModuleName = fileResolver->getHumanReadableModuleName(*wantedDefinitionModule);
result = "Type '" + givenTypeName + "' from '" + givenModuleName + "' could not be converted into '" + wantedTypeName +
"' from '" + wantedModuleName + "'";
}
else
{
result = "Type '" + givenTypeName + "' from '" + *givenDefinitionModule + "' could not be converted into '" + wantedTypeName +
"' from '" + *wantedDefinitionModule + "'";
}
}
}
}
if (result.empty())
result = "Type '" + givenTypeName + "' could not be converted into '" + wantedTypeName + "'";
if (tm.error)
{
result += "\ncaused by:\n ";
if (!tm.reason.empty())
result += tm.reason + " ";
result += Luau::toString(*tm.error, TypeErrorToStringOptions{fileResolver});
}
else if (!tm.reason.empty())
{
result += "; " + tm.reason;
}
else if (FFlag::LuauTypeMismatchInvarianceInError && tm.context == TypeMismatch::InvariantContext)
{
result += " in an invariant context";
}
return result;
}
std::string operator()(const Luau::UnknownSymbol& e) const
{
switch (e.context)
{
case UnknownSymbol::Binding:
return "Unknown global '" + e.name + "'";
case UnknownSymbol::Type:
return "Unknown type '" + e.name + "'";
}
LUAU_ASSERT(!"Unexpected context for UnknownSymbol");
return "";
}
std::string operator()(const Luau::UnknownProperty& e) const
{
TypeId t = follow(e.table);
if (get<TableType>(t))
return "Key '" + e.key + "' not found in table '" + Luau::toString(t) + "'";
else if (get<ClassType>(t))
return "Key '" + e.key + "' not found in class '" + Luau::toString(t) + "'";
else
return "Type '" + Luau::toString(e.table) + "' does not have key '" + e.key + "'";
}
std::string operator()(const Luau::NotATable& e) const
{
return "Expected type table, got '" + Luau::toString(e.ty) + "' instead";
}
std::string operator()(const Luau::CannotExtendTable& e) const
{
switch (e.context)
{
case Luau::CannotExtendTable::Property:
return "Cannot add property '" + e.prop + "' to table '" + Luau::toString(e.tableType) + "'";
case Luau::CannotExtendTable::Metatable:
return "Cannot add metatable to table '" + Luau::toString(e.tableType) + "'";
case Luau::CannotExtendTable::Indexer:
return "Cannot add indexer to table '" + Luau::toString(e.tableType) + "'";
}
LUAU_ASSERT(!"Unknown context");
return "";
}
std::string operator()(const Luau::OnlyTablesCanHaveMethods& e) const
{
return "Cannot add method to non-table type '" + Luau::toString(e.tableType) + "'";
}
std::string operator()(const Luau::DuplicateTypeDefinition& e) const
{
std::string s = "Redefinition of type '" + e.name + "'";
if (e.previousLocation)
s += ", previously defined at line " + std::to_string(e.previousLocation->begin.line + 1);
return s;
}
std::string operator()(const Luau::CountMismatch& e) const
{
const std::string expectedS = e.expected == 1 ? "" : "s";
const std::string actualS = e.actual == 1 ? "" : "s";
const std::string actualVerb = e.actual == 1 ? "is" : "are";
switch (e.context)
{
case CountMismatch::Return:
return "Expected to return " + std::to_string(e.expected) + " value" + expectedS + ", but " + std::to_string(e.actual) + " " +
actualVerb + " returned here";
case CountMismatch::FunctionResult:
// It is alright if right hand side produces more values than the
// left hand side accepts. In this context consider only the opposite case.
return "Function only returns " + std::to_string(e.expected) + " value" + expectedS + ", but " + std::to_string(e.actual) + " " +
actualVerb + " required here";
case CountMismatch::ExprListResult:
return "Expression list has " + std::to_string(e.expected) + " value" + expectedS + ", but " + std::to_string(e.actual) + " " +
actualVerb + " required here";
case CountMismatch::Arg:
if (!e.function.empty())
return "Argument count mismatch. Function '" + e.function + "' " +
wrongNumberOfArgsString(e.expected, e.maximum, e.actual, /*argPrefix*/ nullptr, e.isVariadic);
else
return "Argument count mismatch. Function " +
wrongNumberOfArgsString(e.expected, e.maximum, e.actual, /*argPrefix*/ nullptr, e.isVariadic);
}
LUAU_ASSERT(!"Unknown context");
return "";
}
std::string operator()(const Luau::FunctionDoesNotTakeSelf&) const
{
return std::string("This function does not take self. Did you mean to use a dot instead of a colon?");
}
std::string operator()(const Luau::FunctionRequiresSelf& e) const
{
return "This function must be called with self. Did you mean to use a colon instead of a dot?";
}
std::string operator()(const Luau::OccursCheckFailed&) const
{
return "Type contains a self-recursive construct that cannot be resolved";
}
std::string operator()(const Luau::UnknownRequire& e) const
{
if (e.modulePath.empty())
return "Unknown require: unsupported path";
else
return "Unknown require: " + e.modulePath;
}
std::string operator()(const Luau::IncorrectGenericParameterCount& e) const
{
std::string name = e.name;
if (!e.typeFun.typeParams.empty() || !e.typeFun.typePackParams.empty())
{
name += "<";
bool first = true;
for (auto param : e.typeFun.typeParams)
{
if (first)
first = false;
else
name += ", ";
name += toString(param.ty);
}
for (auto param : e.typeFun.typePackParams)
{
if (first)
first = false;
else
name += ", ";
name += toString(param.tp);
}
name += ">";
}
if (e.typeFun.typeParams.size() != e.actualParameters)
return "Generic type '" + name + "' " +
wrongNumberOfArgsString(e.typeFun.typeParams.size(), std::nullopt, e.actualParameters, "type", !e.typeFun.typePackParams.empty());
return "Generic type '" + name + "' " +
wrongNumberOfArgsString(e.typeFun.typePackParams.size(), std::nullopt, e.actualPackParameters, "type pack", /*isVariadic*/ false);
}
std::string operator()(const Luau::SyntaxError& e) const
{
return e.message;
}
std::string operator()(const Luau::CodeTooComplex&) const
{
return "Code is too complex to typecheck! Consider simplifying the code around this area";
}
std::string operator()(const Luau::UnificationTooComplex&) const
{
return "Internal error: Code is too complex to typecheck! Consider adding type annotations around this area";
}
std::string operator()(const Luau::UnknownPropButFoundLikeProp& e) const
{
std::string candidatesSuggestion = "Did you mean ";
if (e.candidates.size() != 1)
candidatesSuggestion += "one of ";
bool first = true;
for (Name name : e.candidates)
{
if (first)
first = false;
else
candidatesSuggestion += ", ";
candidatesSuggestion += "'" + name + "'";
}
std::string s = "Key '" + e.key + "' not found in ";
TypeId t = follow(e.table);
if (get<ClassType>(t))
s += "class";
else
s += "table";
s += " '" + toString(e.table) + "'. " + candidatesSuggestion + "?";
return s;
}
std::string operator()(const Luau::GenericError& e) const
{
return e.message;
}
std::string operator()(const Luau::InternalError& e) const
{
return e.message;
}
std::string operator()(const Luau::CannotCallNonFunction& e) const
{
return "Cannot call non-function " + toString(e.ty);
}
std::string operator()(const Luau::ExtraInformation& e) const
{
return e.message;
}
std::string operator()(const Luau::DeprecatedApiUsed& e) const
{
return "The property ." + e.symbol + " is deprecated. Use ." + e.useInstead + " instead.";
}
std::string operator()(const Luau::ModuleHasCyclicDependency& e) const
{
if (e.cycle.empty())
return "Cyclic module dependency detected";
std::string s = "Cyclic module dependency: ";
bool first = true;
for (const ModuleName& name : e.cycle)
{
if (first)
first = false;
else
s += " -> ";
s += name;
}
return s;
}
std::string operator()(const Luau::FunctionExitsWithoutReturning& e) const
{
return "Not all codepaths in this function return '" + toString(e.expectedReturnType) + "'.";
}
std::string operator()(const Luau::IllegalRequire& e) const
{
return "Cannot require module " + e.moduleName + ": " + e.reason;
}
std::string operator()(const Luau::MissingProperties& e) const
{
std::string s = "Table type '" + toString(e.subType) + "' not compatible with type '" + toString(e.superType) + "' because the former";
switch (e.context)
{
case MissingProperties::Missing:
s += " is missing field";
break;
case MissingProperties::Extra:
s += " has extra field";
break;
}
if (e.properties.size() > 1)
s += "s";
s += " ";
for (size_t i = 0; i < e.properties.size(); ++i)
{
if (i > 0)
s += ", ";
if (i > 0 && i == e.properties.size() - 1)
s += "and ";
s += "'" + e.properties[i] + "'";
}
return s;
}
std::string operator()(const Luau::DuplicateGenericParameter& e) const
{
return "Duplicate type parameter '" + e.parameterName + "'";
}
std::string operator()(const Luau::CannotInferBinaryOperation& e) const
{
std::string ss = "Unknown type used in " + toString(e.op);
switch (e.kind)
{
case Luau::CannotInferBinaryOperation::Comparison:
ss += " comparison";
break;
case Luau::CannotInferBinaryOperation::Operation:
ss += " operation";
}
if (e.suggestedToAnnotate)
ss += "; consider adding a type annotation to '" + *e.suggestedToAnnotate + "'";
return ss;
}
std::string operator()(const Luau::SwappedGenericTypeParameter& e) const
{
switch (e.kind)
{
case Luau::SwappedGenericTypeParameter::Type:
return "Variadic type parameter '" + e.name + "...' is used as a regular generic type; consider changing '" + e.name + "...' to '" +
e.name + "' in the generic argument list";
case Luau::SwappedGenericTypeParameter::Pack:
return "Generic type '" + e.name + "' is used as a variadic type parameter; consider changing '" + e.name + "' to '" + e.name +
"...' in the generic argument list";
default:
LUAU_ASSERT(!"Unknown kind");
return "";
}
}
std::string operator()(const Luau::OptionalValueAccess& e) const
{
return "Value of type '" + toString(e.optional) + "' could be nil";
}
std::string operator()(const Luau::MissingUnionProperty& e) const
{
std::string ss = "Key '" + e.key + "' is missing from ";
bool first = true;
for (auto ty : e.missing)
{
if (first)
first = false;
else
ss += ", ";
ss += "'" + toString(ty) + "'";
}
return ss + " in the type '" + toString(e.type) + "'";
}
std::string operator()(const TypesAreUnrelated& e) const
{
return "Cannot cast '" + toString(e.left) + "' into '" + toString(e.right) + "' because the types are unrelated";
}
std::string operator()(const NormalizationTooComplex&) const
{
return "Code is too complex to typecheck! Consider simplifying the code around this area";
}
std::string operator()(const TypePackMismatch& e) const
{
return "Type pack '" + toString(e.givenTp) + "' could not be converted into '" + toString(e.wantedTp) + "'";
}
std::string operator()(const DynamicPropertyLookupOnClassesUnsafe& e) const
{
return "Attempting a dynamic property access on type '" + Luau::toString(e.ty) + "' is unsafe and may cause exceptions at runtime";
}
};
struct InvalidNameChecker
{
std::string invalidName = "%error-id%";
bool operator()(const Luau::UnknownProperty& e) const
{
return e.key == invalidName;
}
bool operator()(const Luau::CannotExtendTable& e) const
{
return e.prop == invalidName;
}
bool operator()(const Luau::DuplicateTypeDefinition& e) const
{
return e.name == invalidName;
}
template<typename T>
bool operator()(const T& other) const
{
return false;
}
};
TypeMismatch::TypeMismatch(TypeId wantedType, TypeId givenType)
: wantedType(wantedType)
, givenType(givenType)
{
}
TypeMismatch::TypeMismatch(TypeId wantedType, TypeId givenType, std::string reason)
: wantedType(wantedType)
, givenType(givenType)
, reason(reason)
{
}
TypeMismatch::TypeMismatch(TypeId wantedType, TypeId givenType, std::string reason, std::optional<TypeError> error)
: wantedType(wantedType)
, givenType(givenType)
, reason(reason)
, error(error ? std::make_shared<TypeError>(std::move(*error)) : nullptr)
{
}
TypeMismatch::TypeMismatch(TypeId wantedType, TypeId givenType, TypeMismatch::Context context)
: wantedType(wantedType)
, givenType(givenType)
, context(context)
{
}
TypeMismatch::TypeMismatch(TypeId wantedType, TypeId givenType, std::string reason, TypeMismatch::Context context)
: wantedType(wantedType)
, givenType(givenType)
, context(context)
, reason(reason)
{
}
TypeMismatch::TypeMismatch(TypeId wantedType, TypeId givenType, std::string reason, std::optional<TypeError> error, TypeMismatch::Context context)
: wantedType(wantedType)
, givenType(givenType)
, context(context)
, reason(reason)
, error(error ? std::make_shared<TypeError>(std::move(*error)) : nullptr)
{
}
bool TypeMismatch::operator==(const TypeMismatch& rhs) const
{
if (!!error != !!rhs.error)
return false;
if (error && !(*error == *rhs.error))
return false;
return *wantedType == *rhs.wantedType && *givenType == *rhs.givenType && reason == rhs.reason && context == rhs.context;
}
bool UnknownSymbol::operator==(const UnknownSymbol& rhs) const
{
return name == rhs.name;
}
bool UnknownProperty::operator==(const UnknownProperty& rhs) const
{
return *table == *rhs.table && key == rhs.key;
}
bool NotATable::operator==(const NotATable& rhs) const
{
return ty == rhs.ty;
}
bool CannotExtendTable::operator==(const CannotExtendTable& rhs) const
{
return *tableType == *rhs.tableType && prop == rhs.prop && context == rhs.context;
}
bool OnlyTablesCanHaveMethods::operator==(const OnlyTablesCanHaveMethods& rhs) const
{
return *tableType == *rhs.tableType;
}
bool DuplicateTypeDefinition::operator==(const DuplicateTypeDefinition& rhs) const
{
return name == rhs.name && previousLocation == rhs.previousLocation;
}
bool CountMismatch::operator==(const CountMismatch& rhs) const
{
return expected == rhs.expected && maximum == rhs.maximum && actual == rhs.actual && context == rhs.context && function == rhs.function;
}
bool FunctionDoesNotTakeSelf::operator==(const FunctionDoesNotTakeSelf&) const
{
return true;
}
bool FunctionRequiresSelf::operator==(const FunctionRequiresSelf& e) const
{
return true;
}
bool OccursCheckFailed::operator==(const OccursCheckFailed&) const
{
return true;
}
bool UnknownRequire::operator==(const UnknownRequire& rhs) const
{
return modulePath == rhs.modulePath;
}
bool IncorrectGenericParameterCount::operator==(const IncorrectGenericParameterCount& rhs) const
{
if (name != rhs.name)
return false;
if (typeFun.type != rhs.typeFun.type)
return false;
if (typeFun.typeParams.size() != rhs.typeFun.typeParams.size())
return false;
if (typeFun.typePackParams.size() != rhs.typeFun.typePackParams.size())
return false;
for (size_t i = 0; i < typeFun.typeParams.size(); ++i)
{
if (typeFun.typeParams[i].ty != rhs.typeFun.typeParams[i].ty)
return false;
}
for (size_t i = 0; i < typeFun.typePackParams.size(); ++i)
{
if (typeFun.typePackParams[i].tp != rhs.typeFun.typePackParams[i].tp)
return false;
}
return true;
}
bool SyntaxError::operator==(const SyntaxError& rhs) const
{
return message == rhs.message;
}
bool CodeTooComplex::operator==(const CodeTooComplex&) const
{
return true;
}
bool UnificationTooComplex::operator==(const UnificationTooComplex&) const
{
return true;
}
bool UnknownPropButFoundLikeProp::operator==(const UnknownPropButFoundLikeProp& rhs) const
{
return *table == *rhs.table && key == rhs.key && candidates.size() == rhs.candidates.size() &&
std::equal(candidates.begin(), candidates.end(), rhs.candidates.begin());
}
bool GenericError::operator==(const GenericError& rhs) const
{
return message == rhs.message;
}
bool InternalError::operator==(const InternalError& rhs) const
{
return message == rhs.message;
}
bool CannotCallNonFunction::operator==(const CannotCallNonFunction& rhs) const
{
return ty == rhs.ty;
}
bool ExtraInformation::operator==(const ExtraInformation& rhs) const
{
return message == rhs.message;
}
bool DeprecatedApiUsed::operator==(const DeprecatedApiUsed& rhs) const
{
return symbol == rhs.symbol && useInstead == rhs.useInstead;
}
bool FunctionExitsWithoutReturning::operator==(const FunctionExitsWithoutReturning& rhs) const
{
return expectedReturnType == rhs.expectedReturnType;
}
int TypeError::code() const
{
return minCode() + int(data.index());
}
int TypeError::minCode()
{
return 1000;
}
TypeErrorSummary TypeError::summary() const
{
return TypeErrorSummary{location, moduleName, code()};
}
bool TypeError::operator==(const TypeError& rhs) const
{
return location == rhs.location && data == rhs.data;
}
bool ModuleHasCyclicDependency::operator==(const ModuleHasCyclicDependency& rhs) const
{
return cycle.size() == rhs.cycle.size() && std::equal(cycle.begin(), cycle.end(), rhs.cycle.begin());
}
bool IllegalRequire::operator==(const IllegalRequire& rhs) const
{
return moduleName == rhs.moduleName && reason == rhs.reason;
}
bool MissingProperties::operator==(const MissingProperties& rhs) const
{
return *superType == *rhs.superType && *subType == *rhs.subType && properties.size() == rhs.properties.size() &&
std::equal(properties.begin(), properties.end(), rhs.properties.begin()) && context == rhs.context;
}
bool DuplicateGenericParameter::operator==(const DuplicateGenericParameter& rhs) const
{
return parameterName == rhs.parameterName;
}
bool CannotInferBinaryOperation::operator==(const CannotInferBinaryOperation& rhs) const
{
return op == rhs.op && suggestedToAnnotate == rhs.suggestedToAnnotate && kind == rhs.kind;
}
bool SwappedGenericTypeParameter::operator==(const SwappedGenericTypeParameter& rhs) const
{
return name == rhs.name && kind == rhs.kind;
}
bool OptionalValueAccess::operator==(const OptionalValueAccess& rhs) const
{
return *optional == *rhs.optional;
}
bool MissingUnionProperty::operator==(const MissingUnionProperty& rhs) const
{
if (missing.size() != rhs.missing.size())
return false;
for (size_t i = 0; i < missing.size(); ++i)
{
if (*missing[i] != *rhs.missing[i])
return false;
}
return *type == *rhs.type && key == rhs.key;
}
bool TypesAreUnrelated::operator==(const TypesAreUnrelated& rhs) const
{
return left == rhs.left && right == rhs.right;
}
bool TypePackMismatch::operator==(const TypePackMismatch& rhs) const
{
return *wantedTp == *rhs.wantedTp && *givenTp == *rhs.givenTp;
}
bool DynamicPropertyLookupOnClassesUnsafe::operator==(const DynamicPropertyLookupOnClassesUnsafe& rhs) const
{
return ty == rhs.ty;
}
std::string toString(const TypeError& error)
{
return toString(error, TypeErrorToStringOptions{});
}
std::string toString(const TypeError& error, TypeErrorToStringOptions options)
{
ErrorConverter converter{options.fileResolver};
return Luau::visit(converter, error.data);
}
bool containsParseErrorName(const TypeError& error)
{
return Luau::visit(InvalidNameChecker{}, error.data);
}
template<typename T>
void copyError(T& e, TypeArena& destArena, CloneState cloneState)
{
auto clone = [&](auto&& ty) {
return ::Luau::clone(ty, destArena, cloneState);
};
auto visitErrorData = [&](auto&& e) {
copyError(e, destArena, cloneState);
};
if constexpr (false)
{
}
else if constexpr (std::is_same_v<T, TypeMismatch>)
{
e.wantedType = clone(e.wantedType);
e.givenType = clone(e.givenType);
if (e.error)
visit(visitErrorData, e.error->data);
}
else if constexpr (std::is_same_v<T, UnknownSymbol>)
{
}
else if constexpr (std::is_same_v<T, UnknownProperty>)
{
e.table = clone(e.table);
}
else if constexpr (std::is_same_v<T, NotATable>)
{
e.ty = clone(e.ty);
}
else if constexpr (std::is_same_v<T, CannotExtendTable>)
{
e.tableType = clone(e.tableType);
}
else if constexpr (std::is_same_v<T, OnlyTablesCanHaveMethods>)
{
e.tableType = clone(e.tableType);
}
else if constexpr (std::is_same_v<T, DuplicateTypeDefinition>)
{
}
else if constexpr (std::is_same_v<T, CountMismatch>)
{
}
else if constexpr (std::is_same_v<T, FunctionDoesNotTakeSelf>)
{
}
else if constexpr (std::is_same_v<T, FunctionRequiresSelf>)
{
}
else if constexpr (std::is_same_v<T, OccursCheckFailed>)
{
}
else if constexpr (std::is_same_v<T, UnknownRequire>)
{
}
else if constexpr (std::is_same_v<T, IncorrectGenericParameterCount>)
{
e.typeFun = clone(e.typeFun);
}
else if constexpr (std::is_same_v<T, SyntaxError>)
{
}
else if constexpr (std::is_same_v<T, CodeTooComplex>)
{
}
else if constexpr (std::is_same_v<T, UnificationTooComplex>)
{
}
else if constexpr (std::is_same_v<T, UnknownPropButFoundLikeProp>)
{
e.table = clone(e.table);
}
else if constexpr (std::is_same_v<T, GenericError>)
{
}
else if constexpr (std::is_same_v<T, InternalError>)
{
}
else if constexpr (std::is_same_v<T, CannotCallNonFunction>)
{
e.ty = clone(e.ty);
}
else if constexpr (std::is_same_v<T, ExtraInformation>)
{
}
else if constexpr (std::is_same_v<T, DeprecatedApiUsed>)
{
}
else if constexpr (std::is_same_v<T, ModuleHasCyclicDependency>)
{
}
else if constexpr (std::is_same_v<T, IllegalRequire>)
{
}
else if constexpr (std::is_same_v<T, FunctionExitsWithoutReturning>)
{
e.expectedReturnType = clone(e.expectedReturnType);
}
else if constexpr (std::is_same_v<T, DuplicateGenericParameter>)
{
}
else if constexpr (std::is_same_v<T, CannotInferBinaryOperation>)
{
}
else if constexpr (std::is_same_v<T, MissingProperties>)
{
e.superType = clone(e.superType);
e.subType = clone(e.subType);
}
else if constexpr (std::is_same_v<T, SwappedGenericTypeParameter>)
{
}
else if constexpr (std::is_same_v<T, OptionalValueAccess>)
{
e.optional = clone(e.optional);
}
else if constexpr (std::is_same_v<T, MissingUnionProperty>)
{
e.type = clone(e.type);
for (auto& ty : e.missing)
ty = clone(ty);
}
else if constexpr (std::is_same_v<T, TypesAreUnrelated>)
{
e.left = clone(e.left);
e.right = clone(e.right);
}
else if constexpr (std::is_same_v<T, NormalizationTooComplex>)
{
}
else if constexpr (std::is_same_v<T, TypePackMismatch>)
{
e.wantedTp = clone(e.wantedTp);
e.givenTp = clone(e.givenTp);
}
else if constexpr (std::is_same_v<T, DynamicPropertyLookupOnClassesUnsafe>)
e.ty = clone(e.ty);
else
static_assert(always_false_v<T>, "Non-exhaustive type switch");
}
void copyErrors(ErrorVec& errors, TypeArena& destArena)
{
CloneState cloneState;
auto visitErrorData = [&](auto&& e) {
copyError(e, destArena, cloneState);
};
LUAU_ASSERT(!destArena.types.isFrozen());
LUAU_ASSERT(!destArena.typePacks.isFrozen());
for (TypeError& error : errors)
visit(visitErrorData, error.data);
}
void InternalErrorReporter::ice(const std::string& message, const Location& location) const
{
InternalCompilerError error(message, moduleName, location);
if (onInternalError)
onInternalError(error.what());
throw error;
}
void InternalErrorReporter::ice(const std::string& message) const
{
InternalCompilerError error(message, moduleName);
if (onInternalError)
onInternalError(error.what());
throw error;
}
const char* InternalCompilerError::what() const throw()
{
return this->message.data();
}
} // namespace Luau