25
25
#include " swift/AST/Types.h"
26
26
#include " swift/Basic/Debug.h"
27
27
#include " swift/Sema/ConstraintLocator.h"
28
+ #include " swift/Sema/FixBehavior.h"
28
29
#include " llvm/ADT/ArrayRef.h"
29
30
#include " llvm/ADT/SmallVector.h"
30
31
#include " llvm/Support/TrailingObjects.h"
@@ -402,13 +403,12 @@ class ConstraintFix {
402
403
ConstraintLocator *Locator;
403
404
404
405
// / The behavior limit to apply to the diagnostics emitted.
405
- DiagnosticBehavior behaviorLimit ;
406
+ FixBehavior fixBehavior ;
406
407
407
408
public:
408
409
ConstraintFix (ConstraintSystem &cs, FixKind kind, ConstraintLocator *locator,
409
- DiagnosticBehavior behaviorLimit =
410
- DiagnosticBehavior::Unspecified)
411
- : CS(cs), Kind(kind), Locator(locator), behaviorLimit(behaviorLimit) {}
410
+ FixBehavior fixBehavior = FixBehavior::Error)
411
+ : CS(cs), Kind(kind), Locator(locator), fixBehavior(fixBehavior) {}
412
412
413
413
virtual ~ConstraintFix ();
414
414
@@ -419,14 +419,36 @@ class ConstraintFix {
419
419
420
420
FixKind getKind () const { return Kind; }
421
421
422
- bool isWarning () const {
423
- return behaviorLimit == DiagnosticBehavior::Warning ||
424
- behaviorLimit == DiagnosticBehavior::Ignore;
422
+ // / Whether it is still possible to "apply" a solution containing this kind
423
+ // / of fix to get a usable AST.
424
+ bool canApplySolution () const {
425
+ switch (fixBehavior) {
426
+ case FixBehavior::AlwaysWarning:
427
+ case FixBehavior::DowngradeToWarning:
428
+ case FixBehavior::Suppress:
429
+ return true ;
430
+
431
+ case FixBehavior::Error:
432
+ return false ;
433
+ }
434
+ }
435
+
436
+ // / Whether this kind of fix affects the solution score.
437
+ bool affectsSolutionScore () const {
438
+ switch (fixBehavior) {
439
+ case FixBehavior::AlwaysWarning:
440
+ case FixBehavior::DowngradeToWarning:
441
+ case FixBehavior::Suppress:
442
+ return false ;
443
+
444
+ case FixBehavior::Error:
445
+ return true ;
446
+ }
425
447
}
426
448
427
449
// / The diagnostic behavior limit that will be applied to any emitted
428
450
// / diagnostics.
429
- DiagnosticBehavior diagBehaviorLimit () const { return behaviorLimit ; }
451
+ FixBehavior diagfixBehavior () const { return fixBehavior ; }
430
452
431
453
virtual std::string getName () const = 0;
432
454
@@ -672,16 +694,15 @@ class ContextualMismatch : public ConstraintFix {
672
694
673
695
ContextualMismatch (ConstraintSystem &cs, Type lhs, Type rhs,
674
696
ConstraintLocator *locator,
675
- DiagnosticBehavior behaviorLimit )
676
- : ConstraintFix(cs, FixKind::ContextualMismatch, locator, behaviorLimit ),
697
+ FixBehavior fixBehavior )
698
+ : ConstraintFix(cs, FixKind::ContextualMismatch, locator, fixBehavior ),
677
699
LHS (lhs), RHS(rhs) {}
678
700
679
701
protected:
680
702
ContextualMismatch (ConstraintSystem &cs, FixKind kind, Type lhs, Type rhs,
681
703
ConstraintLocator *locator,
682
- DiagnosticBehavior behaviorLimit =
683
- DiagnosticBehavior::Unspecified)
684
- : ConstraintFix(cs, kind, locator, behaviorLimit), LHS(lhs), RHS(rhs) {}
704
+ FixBehavior fixBehavior = FixBehavior::Error)
705
+ : ConstraintFix(cs, kind, locator, fixBehavior), LHS(lhs), RHS(rhs) {}
685
706
686
707
public:
687
708
std::string getName () const override { return " fix contextual mismatch" ; }
@@ -766,9 +787,9 @@ class MarkExplicitlyEscaping final : public ContextualMismatch {
766
787
class MarkGlobalActorFunction final : public ContextualMismatch {
767
788
MarkGlobalActorFunction (ConstraintSystem &cs, Type lhs, Type rhs,
768
789
ConstraintLocator *locator,
769
- DiagnosticBehavior behaviorLimit )
790
+ FixBehavior fixBehavior )
770
791
: ContextualMismatch(cs, FixKind::MarkGlobalActorFunction, lhs, rhs,
771
- locator, behaviorLimit ) {
792
+ locator, fixBehavior ) {
772
793
}
773
794
774
795
public:
@@ -778,7 +799,7 @@ class MarkGlobalActorFunction final : public ContextualMismatch {
778
799
779
800
static MarkGlobalActorFunction *create (ConstraintSystem &cs, Type lhs,
780
801
Type rhs, ConstraintLocator *locator,
781
- DiagnosticBehavior behaviorLimit );
802
+ FixBehavior fixBehavior );
782
803
783
804
static bool classof (ConstraintFix *fix) {
784
805
return fix->getKind () == FixKind::MarkGlobalActorFunction;
@@ -814,9 +835,9 @@ class ForceOptional final : public ContextualMismatch {
814
835
class AddSendableAttribute final : public ContextualMismatch {
815
836
AddSendableAttribute (ConstraintSystem &cs, FunctionType *fromType,
816
837
FunctionType *toType, ConstraintLocator *locator,
817
- DiagnosticBehavior behaviorLimit )
838
+ FixBehavior fixBehavior )
818
839
: ContextualMismatch(cs, FixKind::AddSendableAttribute, fromType, toType,
819
- locator, behaviorLimit ) {
840
+ locator, fixBehavior ) {
820
841
assert (fromType->isSendable () != toType->isSendable ());
821
842
}
822
843
@@ -829,7 +850,7 @@ class AddSendableAttribute final : public ContextualMismatch {
829
850
FunctionType *fromType,
830
851
FunctionType *toType,
831
852
ConstraintLocator *locator,
832
- DiagnosticBehavior behaviorLimit );
853
+ FixBehavior fixBehavior );
833
854
834
855
static bool classof (ConstraintFix *fix) {
835
856
return fix->getKind () == FixKind::AddSendableAttribute;
@@ -1392,11 +1413,14 @@ class AllowTypeOrInstanceMember final : public AllowInvalidMemberRef {
1392
1413
};
1393
1414
1394
1415
class AllowInvalidPartialApplication final : public ConstraintFix {
1416
+ bool isWarning;
1417
+
1395
1418
AllowInvalidPartialApplication (bool isWarning, ConstraintSystem &cs,
1396
1419
ConstraintLocator *locator)
1397
1420
: ConstraintFix(cs, FixKind::AllowInvalidPartialApplication, locator,
1398
- isWarning ? DiagnosticBehavior::Warning
1399
- : DiagnosticBehavior::Unspecified) {}
1421
+ isWarning ? FixBehavior::AlwaysWarning
1422
+ : FixBehavior::Error),
1423
+ isWarning (isWarning) {}
1400
1424
1401
1425
public:
1402
1426
std::string getName () const override {
@@ -2130,10 +2154,9 @@ class AllowArgumentMismatch : public ContextualMismatch {
2130
2154
2131
2155
AllowArgumentMismatch (ConstraintSystem &cs, FixKind kind, Type argType,
2132
2156
Type paramType, ConstraintLocator *locator,
2133
- DiagnosticBehavior behaviorLimit =
2134
- DiagnosticBehavior::Unspecified)
2157
+ FixBehavior fixBehavior = FixBehavior::Error)
2135
2158
: ContextualMismatch(
2136
- cs, kind, argType, paramType, locator, behaviorLimit ) {}
2159
+ cs, kind, argType, paramType, locator, fixBehavior ) {}
2137
2160
2138
2161
public:
2139
2162
std::string getName () const override {
@@ -2281,9 +2304,9 @@ class TreatEphemeralAsNonEphemeral final : public AllowArgumentMismatch {
2281
2304
TreatEphemeralAsNonEphemeral (ConstraintSystem &cs, ConstraintLocator *locator,
2282
2305
Type srcType, Type dstType,
2283
2306
ConversionRestrictionKind conversionKind,
2284
- DiagnosticBehavior behaviorLimit )
2307
+ FixBehavior fixBehavior )
2285
2308
: AllowArgumentMismatch(cs, FixKind::TreatEphemeralAsNonEphemeral,
2286
- srcType, dstType, locator, behaviorLimit ),
2309
+ srcType, dstType, locator, fixBehavior ),
2287
2310
ConversionKind (conversionKind) {}
2288
2311
2289
2312
public:
@@ -2447,7 +2470,7 @@ class AllowCoercionToForceCast final : public ContextualMismatch {
2447
2470
AllowCoercionToForceCast (ConstraintSystem &cs, Type fromType, Type toType,
2448
2471
ConstraintLocator *locator)
2449
2472
: ContextualMismatch(cs, FixKind::AllowCoercionToForceCast, fromType,
2450
- toType, locator, DiagnosticBehavior::Warning ) {}
2473
+ toType, locator, FixBehavior::AlwaysWarning ) {}
2451
2474
2452
2475
public:
2453
2476
std::string getName () const override {
@@ -2561,7 +2584,7 @@ class SpecifyLabelToAssociateTrailingClosure final : public ConstraintFix {
2561
2584
SpecifyLabelToAssociateTrailingClosure (ConstraintSystem &cs,
2562
2585
ConstraintLocator *locator)
2563
2586
: ConstraintFix(cs, FixKind::SpecifyLabelToAssociateTrailingClosure,
2564
- locator, DiagnosticBehavior::Warning ) {}
2587
+ locator, FixBehavior::AlwaysWarning ) {}
2565
2588
2566
2589
public:
2567
2590
std::string getName () const override {
@@ -2731,7 +2754,7 @@ class SpecifyBaseTypeForOptionalUnresolvedMember final : public ConstraintFix {
2731
2754
DeclNameRef memberName,
2732
2755
ConstraintLocator *locator)
2733
2756
: ConstraintFix(cs, FixKind::SpecifyBaseTypeForOptionalUnresolvedMember,
2734
- locator, DiagnosticBehavior::Warning ),
2757
+ locator, FixBehavior::AlwaysWarning ),
2735
2758
MemberName (memberName) {}
2736
2759
DeclNameRef MemberName;
2737
2760
@@ -2762,7 +2785,7 @@ class CheckedCastContextualMismatchWarning : public ContextualMismatch {
2762
2785
CheckedCastKind kind,
2763
2786
ConstraintLocator *locator)
2764
2787
: ContextualMismatch(cs, fixKind, fromType, toType, locator,
2765
- DiagnosticBehavior::Warning ),
2788
+ FixBehavior::AlwaysWarning ),
2766
2789
CastKind (kind) {}
2767
2790
CheckedCastKind CastKind;
2768
2791
};
@@ -2873,7 +2896,7 @@ class AllowTupleLabelMismatch final : public ContextualMismatch {
2873
2896
AllowTupleLabelMismatch (ConstraintSystem &cs, Type fromType, Type toType,
2874
2897
ConstraintLocator *locator)
2875
2898
: ContextualMismatch(cs, FixKind::AllowTupleLabelMismatch, fromType,
2876
- toType, locator, DiagnosticBehavior::Warning ) {}
2899
+ toType, locator, FixBehavior::AlwaysWarning ) {}
2877
2900
2878
2901
public:
2879
2902
std::string getName () const override { return " allow tuple label mismatch" ; }
0 commit comments