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"
@@ -410,13 +411,12 @@ class ConstraintFix {
410
411
ConstraintLocator *Locator;
411
412
412
413
// / The behavior limit to apply to the diagnostics emitted.
413
- DiagnosticBehavior behaviorLimit ;
414
+ FixBehavior fixBehavior ;
414
415
415
416
public:
416
417
ConstraintFix (ConstraintSystem &cs, FixKind kind, ConstraintLocator *locator,
417
- DiagnosticBehavior behaviorLimit =
418
- DiagnosticBehavior::Unspecified)
419
- : CS(cs), Kind(kind), Locator(locator), behaviorLimit(behaviorLimit) {}
418
+ FixBehavior fixBehavior = FixBehavior::Error)
419
+ : CS(cs), Kind(kind), Locator(locator), fixBehavior(fixBehavior) {}
420
420
421
421
virtual ~ConstraintFix ();
422
422
@@ -427,14 +427,36 @@ class ConstraintFix {
427
427
428
428
FixKind getKind () const { return Kind; }
429
429
430
- bool isWarning () const {
431
- return behaviorLimit == DiagnosticBehavior::Warning ||
432
- behaviorLimit == DiagnosticBehavior::Ignore;
430
+ // / Whether it is still possible to "apply" a solution containing this kind
431
+ // / of fix to get a usable AST.
432
+ bool canApplySolution () const {
433
+ switch (fixBehavior) {
434
+ case FixBehavior::AlwaysWarning:
435
+ case FixBehavior::DowngradeToWarning:
436
+ case FixBehavior::Suppress:
437
+ return true ;
438
+
439
+ case FixBehavior::Error:
440
+ return false ;
441
+ }
442
+ }
443
+
444
+ // / Whether this kind of fix affects the solution score.
445
+ bool affectsSolutionScore () const {
446
+ switch (fixBehavior) {
447
+ case FixBehavior::AlwaysWarning:
448
+ case FixBehavior::DowngradeToWarning:
449
+ case FixBehavior::Suppress:
450
+ return false ;
451
+
452
+ case FixBehavior::Error:
453
+ return true ;
454
+ }
433
455
}
434
456
435
457
// / The diagnostic behavior limit that will be applied to any emitted
436
458
// / diagnostics.
437
- DiagnosticBehavior diagBehaviorLimit () const { return behaviorLimit ; }
459
+ FixBehavior diagfixBehavior () const { return fixBehavior ; }
438
460
439
461
virtual std::string getName () const = 0;
440
462
@@ -680,16 +702,15 @@ class ContextualMismatch : public ConstraintFix {
680
702
681
703
ContextualMismatch (ConstraintSystem &cs, Type lhs, Type rhs,
682
704
ConstraintLocator *locator,
683
- DiagnosticBehavior behaviorLimit )
684
- : ConstraintFix(cs, FixKind::ContextualMismatch, locator, behaviorLimit ),
705
+ FixBehavior fixBehavior )
706
+ : ConstraintFix(cs, FixKind::ContextualMismatch, locator, fixBehavior ),
685
707
LHS (lhs), RHS(rhs) {}
686
708
687
709
protected:
688
710
ContextualMismatch (ConstraintSystem &cs, FixKind kind, Type lhs, Type rhs,
689
711
ConstraintLocator *locator,
690
- DiagnosticBehavior behaviorLimit =
691
- DiagnosticBehavior::Unspecified)
692
- : ConstraintFix(cs, kind, locator, behaviorLimit), LHS(lhs), RHS(rhs) {}
712
+ FixBehavior fixBehavior = FixBehavior::Error)
713
+ : ConstraintFix(cs, kind, locator, fixBehavior), LHS(lhs), RHS(rhs) {}
693
714
694
715
public:
695
716
std::string getName () const override { return " fix contextual mismatch" ; }
@@ -777,9 +798,9 @@ class MarkExplicitlyEscaping final : public ContextualMismatch {
777
798
class MarkGlobalActorFunction final : public ContextualMismatch {
778
799
MarkGlobalActorFunction (ConstraintSystem &cs, Type lhs, Type rhs,
779
800
ConstraintLocator *locator,
780
- DiagnosticBehavior behaviorLimit )
801
+ FixBehavior fixBehavior )
781
802
: ContextualMismatch(cs, FixKind::MarkGlobalActorFunction, lhs, rhs,
782
- locator, behaviorLimit ) {
803
+ locator, fixBehavior ) {
783
804
}
784
805
785
806
public:
@@ -789,7 +810,7 @@ class MarkGlobalActorFunction final : public ContextualMismatch {
789
810
790
811
static MarkGlobalActorFunction *create (ConstraintSystem &cs, Type lhs,
791
812
Type rhs, ConstraintLocator *locator,
792
- DiagnosticBehavior behaviorLimit );
813
+ FixBehavior fixBehavior );
793
814
794
815
static bool classof (ConstraintFix *fix) {
795
816
return fix->getKind () == FixKind::MarkGlobalActorFunction;
@@ -825,9 +846,9 @@ class ForceOptional final : public ContextualMismatch {
825
846
class AddSendableAttribute final : public ContextualMismatch {
826
847
AddSendableAttribute (ConstraintSystem &cs, FunctionType *fromType,
827
848
FunctionType *toType, ConstraintLocator *locator,
828
- DiagnosticBehavior behaviorLimit )
849
+ FixBehavior fixBehavior )
829
850
: ContextualMismatch(cs, FixKind::AddSendableAttribute, fromType, toType,
830
- locator, behaviorLimit ) {
851
+ locator, fixBehavior ) {
831
852
assert (fromType->isSendable () != toType->isSendable ());
832
853
}
833
854
@@ -840,7 +861,7 @@ class AddSendableAttribute final : public ContextualMismatch {
840
861
FunctionType *fromType,
841
862
FunctionType *toType,
842
863
ConstraintLocator *locator,
843
- DiagnosticBehavior behaviorLimit );
864
+ FixBehavior fixBehavior );
844
865
845
866
static bool classof (ConstraintFix *fix) {
846
867
return fix->getKind () == FixKind::AddSendableAttribute;
@@ -1403,11 +1424,14 @@ class AllowTypeOrInstanceMember final : public AllowInvalidMemberRef {
1403
1424
};
1404
1425
1405
1426
class AllowInvalidPartialApplication final : public ConstraintFix {
1427
+ bool isWarning;
1428
+
1406
1429
AllowInvalidPartialApplication (bool isWarning, ConstraintSystem &cs,
1407
1430
ConstraintLocator *locator)
1408
1431
: ConstraintFix(cs, FixKind::AllowInvalidPartialApplication, locator,
1409
- isWarning ? DiagnosticBehavior::Warning
1410
- : DiagnosticBehavior::Unspecified) {}
1432
+ isWarning ? FixBehavior::AlwaysWarning
1433
+ : FixBehavior::Error),
1434
+ isWarning (isWarning) {}
1411
1435
1412
1436
public:
1413
1437
std::string getName () const override {
@@ -2141,10 +2165,9 @@ class AllowArgumentMismatch : public ContextualMismatch {
2141
2165
2142
2166
AllowArgumentMismatch (ConstraintSystem &cs, FixKind kind, Type argType,
2143
2167
Type paramType, ConstraintLocator *locator,
2144
- DiagnosticBehavior behaviorLimit =
2145
- DiagnosticBehavior::Unspecified)
2168
+ FixBehavior fixBehavior = FixBehavior::Error)
2146
2169
: ContextualMismatch(
2147
- cs, kind, argType, paramType, locator, behaviorLimit ) {}
2170
+ cs, kind, argType, paramType, locator, fixBehavior ) {}
2148
2171
2149
2172
public:
2150
2173
std::string getName () const override {
@@ -2292,9 +2315,9 @@ class TreatEphemeralAsNonEphemeral final : public AllowArgumentMismatch {
2292
2315
TreatEphemeralAsNonEphemeral (ConstraintSystem &cs, ConstraintLocator *locator,
2293
2316
Type srcType, Type dstType,
2294
2317
ConversionRestrictionKind conversionKind,
2295
- DiagnosticBehavior behaviorLimit )
2318
+ FixBehavior fixBehavior )
2296
2319
: AllowArgumentMismatch(cs, FixKind::TreatEphemeralAsNonEphemeral,
2297
- srcType, dstType, locator, behaviorLimit ),
2320
+ srcType, dstType, locator, fixBehavior ),
2298
2321
ConversionKind (conversionKind) {}
2299
2322
2300
2323
public:
@@ -2458,7 +2481,7 @@ class AllowCoercionToForceCast final : public ContextualMismatch {
2458
2481
AllowCoercionToForceCast (ConstraintSystem &cs, Type fromType, Type toType,
2459
2482
ConstraintLocator *locator)
2460
2483
: ContextualMismatch(cs, FixKind::AllowCoercionToForceCast, fromType,
2461
- toType, locator, DiagnosticBehavior::Warning ) {}
2484
+ toType, locator, FixBehavior::AlwaysWarning ) {}
2462
2485
2463
2486
public:
2464
2487
std::string getName () const override {
@@ -2572,7 +2595,7 @@ class SpecifyLabelToAssociateTrailingClosure final : public ConstraintFix {
2572
2595
SpecifyLabelToAssociateTrailingClosure (ConstraintSystem &cs,
2573
2596
ConstraintLocator *locator)
2574
2597
: ConstraintFix(cs, FixKind::SpecifyLabelToAssociateTrailingClosure,
2575
- locator, DiagnosticBehavior::Warning ) {}
2598
+ locator, FixBehavior::AlwaysWarning ) {}
2576
2599
2577
2600
public:
2578
2601
std::string getName () const override {
@@ -2742,7 +2765,7 @@ class SpecifyBaseTypeForOptionalUnresolvedMember final : public ConstraintFix {
2742
2765
DeclNameRef memberName,
2743
2766
ConstraintLocator *locator)
2744
2767
: ConstraintFix(cs, FixKind::SpecifyBaseTypeForOptionalUnresolvedMember,
2745
- locator, DiagnosticBehavior::Warning ),
2768
+ locator, FixBehavior::AlwaysWarning ),
2746
2769
MemberName (memberName) {}
2747
2770
DeclNameRef MemberName;
2748
2771
@@ -2773,7 +2796,7 @@ class CheckedCastContextualMismatchWarning : public ContextualMismatch {
2773
2796
CheckedCastKind kind,
2774
2797
ConstraintLocator *locator)
2775
2798
: ContextualMismatch(cs, fixKind, fromType, toType, locator,
2776
- DiagnosticBehavior::Warning ),
2799
+ FixBehavior::AlwaysWarning ),
2777
2800
CastKind (kind) {}
2778
2801
CheckedCastKind CastKind;
2779
2802
};
@@ -2932,7 +2955,7 @@ class AllowTupleLabelMismatch final : public ContextualMismatch {
2932
2955
AllowTupleLabelMismatch (ConstraintSystem &cs, Type fromType, Type toType,
2933
2956
ConstraintLocator *locator)
2934
2957
: ContextualMismatch(cs, FixKind::AllowTupleLabelMismatch, fromType,
2935
- toType, locator, DiagnosticBehavior::Warning ) {}
2958
+ toType, locator, FixBehavior::AlwaysWarning ) {}
2936
2959
2937
2960
public:
2938
2961
std::string getName () const override { return " allow tuple label mismatch" ; }
0 commit comments