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"
@@ -45,6 +46,7 @@ class ConstraintSystem;
45
46
class ConstraintLocator ;
46
47
class ConstraintLocatorBuilder ;
47
48
enum class ConversionRestrictionKind ;
49
+ enum ScoreKind: unsigned int ;
48
50
class Solution ;
49
51
struct MemberLookupResult ;
50
52
@@ -410,13 +412,12 @@ class ConstraintFix {
410
412
ConstraintLocator *Locator;
411
413
412
414
// / The behavior limit to apply to the diagnostics emitted.
413
- DiagnosticBehavior behaviorLimit ;
415
+ FixBehavior fixBehavior ;
414
416
415
417
public:
416
418
ConstraintFix (ConstraintSystem &cs, FixKind kind, ConstraintLocator *locator,
417
- DiagnosticBehavior behaviorLimit =
418
- DiagnosticBehavior::Unspecified)
419
- : CS(cs), Kind(kind), Locator(locator), behaviorLimit(behaviorLimit) {}
419
+ FixBehavior fixBehavior = FixBehavior::Error)
420
+ : CS(cs), Kind(kind), Locator(locator), fixBehavior(fixBehavior) {}
420
421
421
422
virtual ~ConstraintFix ();
422
423
@@ -427,14 +428,27 @@ class ConstraintFix {
427
428
428
429
FixKind getKind () const { return Kind; }
429
430
430
- bool isWarning () const {
431
- return behaviorLimit == DiagnosticBehavior::Warning ||
432
- behaviorLimit == DiagnosticBehavior::Ignore;
431
+ // / Whether it is still possible to "apply" a solution containing this kind
432
+ // / of fix to get a usable AST.
433
+ bool canApplySolution () const {
434
+ switch (fixBehavior) {
435
+ case FixBehavior::AlwaysWarning:
436
+ case FixBehavior::DowngradeToWarning:
437
+ case FixBehavior::Suppress:
438
+ return true ;
439
+
440
+ case FixBehavior::Error:
441
+ return false ;
442
+ }
433
443
}
434
444
445
+ // / Whether this kind of fix affects the solution score, and which score
446
+ // / it affects.
447
+ Optional<ScoreKind> affectsSolutionScore () const ;
448
+
435
449
// / The diagnostic behavior limit that will be applied to any emitted
436
450
// / diagnostics.
437
- DiagnosticBehavior diagBehaviorLimit () const { return behaviorLimit ; }
451
+ FixBehavior diagfixBehavior () const { return fixBehavior ; }
438
452
439
453
virtual std::string getName () const = 0;
440
454
@@ -680,16 +694,15 @@ class ContextualMismatch : public ConstraintFix {
680
694
681
695
ContextualMismatch (ConstraintSystem &cs, Type lhs, Type rhs,
682
696
ConstraintLocator *locator,
683
- DiagnosticBehavior behaviorLimit )
684
- : ConstraintFix(cs, FixKind::ContextualMismatch, locator, behaviorLimit ),
697
+ FixBehavior fixBehavior )
698
+ : ConstraintFix(cs, FixKind::ContextualMismatch, locator, fixBehavior ),
685
699
LHS (lhs), RHS(rhs) {}
686
700
687
701
protected:
688
702
ContextualMismatch (ConstraintSystem &cs, FixKind kind, Type lhs, Type rhs,
689
703
ConstraintLocator *locator,
690
- DiagnosticBehavior behaviorLimit =
691
- DiagnosticBehavior::Unspecified)
692
- : ConstraintFix(cs, kind, locator, behaviorLimit), LHS(lhs), RHS(rhs) {}
704
+ FixBehavior fixBehavior = FixBehavior::Error)
705
+ : ConstraintFix(cs, kind, locator, fixBehavior), LHS(lhs), RHS(rhs) {}
693
706
694
707
public:
695
708
std::string getName () const override { return " fix contextual mismatch" ; }
@@ -777,9 +790,9 @@ class MarkExplicitlyEscaping final : public ContextualMismatch {
777
790
class MarkGlobalActorFunction final : public ContextualMismatch {
778
791
MarkGlobalActorFunction (ConstraintSystem &cs, Type lhs, Type rhs,
779
792
ConstraintLocator *locator,
780
- DiagnosticBehavior behaviorLimit )
793
+ FixBehavior fixBehavior )
781
794
: ContextualMismatch(cs, FixKind::MarkGlobalActorFunction, lhs, rhs,
782
- locator, behaviorLimit ) {
795
+ locator, fixBehavior ) {
783
796
}
784
797
785
798
public:
@@ -789,7 +802,7 @@ class MarkGlobalActorFunction final : public ContextualMismatch {
789
802
790
803
static MarkGlobalActorFunction *create (ConstraintSystem &cs, Type lhs,
791
804
Type rhs, ConstraintLocator *locator,
792
- DiagnosticBehavior behaviorLimit );
805
+ FixBehavior fixBehavior );
793
806
794
807
static bool classof (ConstraintFix *fix) {
795
808
return fix->getKind () == FixKind::MarkGlobalActorFunction;
@@ -825,9 +838,9 @@ class ForceOptional final : public ContextualMismatch {
825
838
class AddSendableAttribute final : public ContextualMismatch {
826
839
AddSendableAttribute (ConstraintSystem &cs, FunctionType *fromType,
827
840
FunctionType *toType, ConstraintLocator *locator,
828
- DiagnosticBehavior behaviorLimit )
841
+ FixBehavior fixBehavior )
829
842
: ContextualMismatch(cs, FixKind::AddSendableAttribute, fromType, toType,
830
- locator, behaviorLimit ) {
843
+ locator, fixBehavior ) {
831
844
assert (fromType->isSendable () != toType->isSendable ());
832
845
}
833
846
@@ -840,7 +853,7 @@ class AddSendableAttribute final : public ContextualMismatch {
840
853
FunctionType *fromType,
841
854
FunctionType *toType,
842
855
ConstraintLocator *locator,
843
- DiagnosticBehavior behaviorLimit );
856
+ FixBehavior fixBehavior );
844
857
845
858
static bool classof (ConstraintFix *fix) {
846
859
return fix->getKind () == FixKind::AddSendableAttribute;
@@ -1403,11 +1416,14 @@ class AllowTypeOrInstanceMember final : public AllowInvalidMemberRef {
1403
1416
};
1404
1417
1405
1418
class AllowInvalidPartialApplication final : public ConstraintFix {
1419
+ bool isWarning;
1420
+
1406
1421
AllowInvalidPartialApplication (bool isWarning, ConstraintSystem &cs,
1407
1422
ConstraintLocator *locator)
1408
1423
: ConstraintFix(cs, FixKind::AllowInvalidPartialApplication, locator,
1409
- isWarning ? DiagnosticBehavior::Warning
1410
- : DiagnosticBehavior::Unspecified) {}
1424
+ isWarning ? FixBehavior::AlwaysWarning
1425
+ : FixBehavior::Error),
1426
+ isWarning (isWarning) {}
1411
1427
1412
1428
public:
1413
1429
std::string getName () const override {
@@ -2141,10 +2157,9 @@ class AllowArgumentMismatch : public ContextualMismatch {
2141
2157
2142
2158
AllowArgumentMismatch (ConstraintSystem &cs, FixKind kind, Type argType,
2143
2159
Type paramType, ConstraintLocator *locator,
2144
- DiagnosticBehavior behaviorLimit =
2145
- DiagnosticBehavior::Unspecified)
2160
+ FixBehavior fixBehavior = FixBehavior::Error)
2146
2161
: ContextualMismatch(
2147
- cs, kind, argType, paramType, locator, behaviorLimit ) {}
2162
+ cs, kind, argType, paramType, locator, fixBehavior ) {}
2148
2163
2149
2164
public:
2150
2165
std::string getName () const override {
@@ -2292,9 +2307,9 @@ class TreatEphemeralAsNonEphemeral final : public AllowArgumentMismatch {
2292
2307
TreatEphemeralAsNonEphemeral (ConstraintSystem &cs, ConstraintLocator *locator,
2293
2308
Type srcType, Type dstType,
2294
2309
ConversionRestrictionKind conversionKind,
2295
- DiagnosticBehavior behaviorLimit )
2310
+ FixBehavior fixBehavior )
2296
2311
: AllowArgumentMismatch(cs, FixKind::TreatEphemeralAsNonEphemeral,
2297
- srcType, dstType, locator, behaviorLimit ),
2312
+ srcType, dstType, locator, fixBehavior ),
2298
2313
ConversionKind (conversionKind) {}
2299
2314
2300
2315
public:
@@ -2458,7 +2473,7 @@ class AllowCoercionToForceCast final : public ContextualMismatch {
2458
2473
AllowCoercionToForceCast (ConstraintSystem &cs, Type fromType, Type toType,
2459
2474
ConstraintLocator *locator)
2460
2475
: ContextualMismatch(cs, FixKind::AllowCoercionToForceCast, fromType,
2461
- toType, locator, DiagnosticBehavior::Warning ) {}
2476
+ toType, locator, FixBehavior::AlwaysWarning ) {}
2462
2477
2463
2478
public:
2464
2479
std::string getName () const override {
@@ -2572,7 +2587,7 @@ class SpecifyLabelToAssociateTrailingClosure final : public ConstraintFix {
2572
2587
SpecifyLabelToAssociateTrailingClosure (ConstraintSystem &cs,
2573
2588
ConstraintLocator *locator)
2574
2589
: ConstraintFix(cs, FixKind::SpecifyLabelToAssociateTrailingClosure,
2575
- locator, DiagnosticBehavior::Warning ) {}
2590
+ locator, FixBehavior::AlwaysWarning ) {}
2576
2591
2577
2592
public:
2578
2593
std::string getName () const override {
@@ -2742,7 +2757,7 @@ class SpecifyBaseTypeForOptionalUnresolvedMember final : public ConstraintFix {
2742
2757
DeclNameRef memberName,
2743
2758
ConstraintLocator *locator)
2744
2759
: ConstraintFix(cs, FixKind::SpecifyBaseTypeForOptionalUnresolvedMember,
2745
- locator, DiagnosticBehavior::Warning ),
2760
+ locator, FixBehavior::AlwaysWarning ),
2746
2761
MemberName (memberName) {}
2747
2762
DeclNameRef MemberName;
2748
2763
@@ -2773,7 +2788,7 @@ class CheckedCastContextualMismatchWarning : public ContextualMismatch {
2773
2788
CheckedCastKind kind,
2774
2789
ConstraintLocator *locator)
2775
2790
: ContextualMismatch(cs, fixKind, fromType, toType, locator,
2776
- DiagnosticBehavior::Warning ),
2791
+ FixBehavior::AlwaysWarning ),
2777
2792
CastKind (kind) {}
2778
2793
CheckedCastKind CastKind;
2779
2794
};
@@ -2932,7 +2947,7 @@ class AllowTupleLabelMismatch final : public ContextualMismatch {
2932
2947
AllowTupleLabelMismatch (ConstraintSystem &cs, Type fromType, Type toType,
2933
2948
ConstraintLocator *locator)
2934
2949
: ContextualMismatch(cs, FixKind::AllowTupleLabelMismatch, fromType,
2935
- toType, locator, DiagnosticBehavior::Warning ) {}
2950
+ toType, locator, FixBehavior::AlwaysWarning ) {}
2936
2951
2937
2952
public:
2938
2953
std::string getName () const override { return " allow tuple label mismatch" ; }
0 commit comments