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
@@ -402,13 +404,12 @@ class ConstraintFix {
402
404
ConstraintLocator *Locator;
403
405
404
406
// / The behavior limit to apply to the diagnostics emitted.
405
- DiagnosticBehavior behaviorLimit ;
407
+ FixBehavior fixBehavior ;
406
408
407
409
public:
408
410
ConstraintFix (ConstraintSystem &cs, FixKind kind, ConstraintLocator *locator,
409
- DiagnosticBehavior behaviorLimit =
410
- DiagnosticBehavior::Unspecified)
411
- : CS(cs), Kind(kind), Locator(locator), behaviorLimit(behaviorLimit) {}
411
+ FixBehavior fixBehavior = FixBehavior::Error)
412
+ : CS(cs), Kind(kind), Locator(locator), fixBehavior(fixBehavior) {}
412
413
413
414
virtual ~ConstraintFix ();
414
415
@@ -419,14 +420,27 @@ class ConstraintFix {
419
420
420
421
FixKind getKind () const { return Kind; }
421
422
422
- bool isWarning () const {
423
- return behaviorLimit == DiagnosticBehavior::Warning ||
424
- behaviorLimit == DiagnosticBehavior::Ignore;
423
+ // / Whether it is still possible to "apply" a solution containing this kind
424
+ // / of fix to get a usable AST.
425
+ bool canApplySolution () const {
426
+ switch (fixBehavior) {
427
+ case FixBehavior::AlwaysWarning:
428
+ case FixBehavior::DowngradeToWarning:
429
+ case FixBehavior::Suppress:
430
+ return true ;
431
+
432
+ case FixBehavior::Error:
433
+ return false ;
434
+ }
425
435
}
426
436
437
+ // / Whether this kind of fix affects the solution score, and which score
438
+ // / it affects.
439
+ Optional<ScoreKind> affectsSolutionScore () const ;
440
+
427
441
// / The diagnostic behavior limit that will be applied to any emitted
428
442
// / diagnostics.
429
- DiagnosticBehavior diagBehaviorLimit () const { return behaviorLimit ; }
443
+ FixBehavior diagfixBehavior () const { return fixBehavior ; }
430
444
431
445
virtual std::string getName () const = 0;
432
446
@@ -672,16 +686,15 @@ class ContextualMismatch : public ConstraintFix {
672
686
673
687
ContextualMismatch (ConstraintSystem &cs, Type lhs, Type rhs,
674
688
ConstraintLocator *locator,
675
- DiagnosticBehavior behaviorLimit )
676
- : ConstraintFix(cs, FixKind::ContextualMismatch, locator, behaviorLimit ),
689
+ FixBehavior fixBehavior )
690
+ : ConstraintFix(cs, FixKind::ContextualMismatch, locator, fixBehavior ),
677
691
LHS (lhs), RHS(rhs) {}
678
692
679
693
protected:
680
694
ContextualMismatch (ConstraintSystem &cs, FixKind kind, Type lhs, Type rhs,
681
695
ConstraintLocator *locator,
682
- DiagnosticBehavior behaviorLimit =
683
- DiagnosticBehavior::Unspecified)
684
- : ConstraintFix(cs, kind, locator, behaviorLimit), LHS(lhs), RHS(rhs) {}
696
+ FixBehavior fixBehavior = FixBehavior::Error)
697
+ : ConstraintFix(cs, kind, locator, fixBehavior), LHS(lhs), RHS(rhs) {}
685
698
686
699
public:
687
700
std::string getName () const override { return " fix contextual mismatch" ; }
@@ -766,9 +779,9 @@ class MarkExplicitlyEscaping final : public ContextualMismatch {
766
779
class MarkGlobalActorFunction final : public ContextualMismatch {
767
780
MarkGlobalActorFunction (ConstraintSystem &cs, Type lhs, Type rhs,
768
781
ConstraintLocator *locator,
769
- DiagnosticBehavior behaviorLimit )
782
+ FixBehavior fixBehavior )
770
783
: ContextualMismatch(cs, FixKind::MarkGlobalActorFunction, lhs, rhs,
771
- locator, behaviorLimit ) {
784
+ locator, fixBehavior ) {
772
785
}
773
786
774
787
public:
@@ -778,7 +791,7 @@ class MarkGlobalActorFunction final : public ContextualMismatch {
778
791
779
792
static MarkGlobalActorFunction *create (ConstraintSystem &cs, Type lhs,
780
793
Type rhs, ConstraintLocator *locator,
781
- DiagnosticBehavior behaviorLimit );
794
+ FixBehavior fixBehavior );
782
795
783
796
static bool classof (ConstraintFix *fix) {
784
797
return fix->getKind () == FixKind::MarkGlobalActorFunction;
@@ -814,9 +827,9 @@ class ForceOptional final : public ContextualMismatch {
814
827
class AddSendableAttribute final : public ContextualMismatch {
815
828
AddSendableAttribute (ConstraintSystem &cs, FunctionType *fromType,
816
829
FunctionType *toType, ConstraintLocator *locator,
817
- DiagnosticBehavior behaviorLimit )
830
+ FixBehavior fixBehavior )
818
831
: ContextualMismatch(cs, FixKind::AddSendableAttribute, fromType, toType,
819
- locator, behaviorLimit ) {
832
+ locator, fixBehavior ) {
820
833
assert (fromType->isSendable () != toType->isSendable ());
821
834
}
822
835
@@ -829,7 +842,7 @@ class AddSendableAttribute final : public ContextualMismatch {
829
842
FunctionType *fromType,
830
843
FunctionType *toType,
831
844
ConstraintLocator *locator,
832
- DiagnosticBehavior behaviorLimit );
845
+ FixBehavior fixBehavior );
833
846
834
847
static bool classof (ConstraintFix *fix) {
835
848
return fix->getKind () == FixKind::AddSendableAttribute;
@@ -1392,11 +1405,14 @@ class AllowTypeOrInstanceMember final : public AllowInvalidMemberRef {
1392
1405
};
1393
1406
1394
1407
class AllowInvalidPartialApplication final : public ConstraintFix {
1408
+ bool isWarning;
1409
+
1395
1410
AllowInvalidPartialApplication (bool isWarning, ConstraintSystem &cs,
1396
1411
ConstraintLocator *locator)
1397
1412
: ConstraintFix(cs, FixKind::AllowInvalidPartialApplication, locator,
1398
- isWarning ? DiagnosticBehavior::Warning
1399
- : DiagnosticBehavior::Unspecified) {}
1413
+ isWarning ? FixBehavior::AlwaysWarning
1414
+ : FixBehavior::Error),
1415
+ isWarning (isWarning) {}
1400
1416
1401
1417
public:
1402
1418
std::string getName () const override {
@@ -2130,10 +2146,9 @@ class AllowArgumentMismatch : public ContextualMismatch {
2130
2146
2131
2147
AllowArgumentMismatch (ConstraintSystem &cs, FixKind kind, Type argType,
2132
2148
Type paramType, ConstraintLocator *locator,
2133
- DiagnosticBehavior behaviorLimit =
2134
- DiagnosticBehavior::Unspecified)
2149
+ FixBehavior fixBehavior = FixBehavior::Error)
2135
2150
: ContextualMismatch(
2136
- cs, kind, argType, paramType, locator, behaviorLimit ) {}
2151
+ cs, kind, argType, paramType, locator, fixBehavior ) {}
2137
2152
2138
2153
public:
2139
2154
std::string getName () const override {
@@ -2281,9 +2296,9 @@ class TreatEphemeralAsNonEphemeral final : public AllowArgumentMismatch {
2281
2296
TreatEphemeralAsNonEphemeral (ConstraintSystem &cs, ConstraintLocator *locator,
2282
2297
Type srcType, Type dstType,
2283
2298
ConversionRestrictionKind conversionKind,
2284
- DiagnosticBehavior behaviorLimit )
2299
+ FixBehavior fixBehavior )
2285
2300
: AllowArgumentMismatch(cs, FixKind::TreatEphemeralAsNonEphemeral,
2286
- srcType, dstType, locator, behaviorLimit ),
2301
+ srcType, dstType, locator, fixBehavior ),
2287
2302
ConversionKind (conversionKind) {}
2288
2303
2289
2304
public:
@@ -2447,7 +2462,7 @@ class AllowCoercionToForceCast final : public ContextualMismatch {
2447
2462
AllowCoercionToForceCast (ConstraintSystem &cs, Type fromType, Type toType,
2448
2463
ConstraintLocator *locator)
2449
2464
: ContextualMismatch(cs, FixKind::AllowCoercionToForceCast, fromType,
2450
- toType, locator, DiagnosticBehavior::Warning ) {}
2465
+ toType, locator, FixBehavior::AlwaysWarning ) {}
2451
2466
2452
2467
public:
2453
2468
std::string getName () const override {
@@ -2561,7 +2576,7 @@ class SpecifyLabelToAssociateTrailingClosure final : public ConstraintFix {
2561
2576
SpecifyLabelToAssociateTrailingClosure (ConstraintSystem &cs,
2562
2577
ConstraintLocator *locator)
2563
2578
: ConstraintFix(cs, FixKind::SpecifyLabelToAssociateTrailingClosure,
2564
- locator, DiagnosticBehavior::Warning ) {}
2579
+ locator, FixBehavior::AlwaysWarning ) {}
2565
2580
2566
2581
public:
2567
2582
std::string getName () const override {
@@ -2731,7 +2746,7 @@ class SpecifyBaseTypeForOptionalUnresolvedMember final : public ConstraintFix {
2731
2746
DeclNameRef memberName,
2732
2747
ConstraintLocator *locator)
2733
2748
: ConstraintFix(cs, FixKind::SpecifyBaseTypeForOptionalUnresolvedMember,
2734
- locator, DiagnosticBehavior::Warning ),
2749
+ locator, FixBehavior::AlwaysWarning ),
2735
2750
MemberName (memberName) {}
2736
2751
DeclNameRef MemberName;
2737
2752
@@ -2762,7 +2777,7 @@ class CheckedCastContextualMismatchWarning : public ContextualMismatch {
2762
2777
CheckedCastKind kind,
2763
2778
ConstraintLocator *locator)
2764
2779
: ContextualMismatch(cs, fixKind, fromType, toType, locator,
2765
- DiagnosticBehavior::Warning ),
2780
+ FixBehavior::AlwaysWarning ),
2766
2781
CastKind (kind) {}
2767
2782
CheckedCastKind CastKind;
2768
2783
};
@@ -2873,7 +2888,7 @@ class AllowTupleLabelMismatch final : public ContextualMismatch {
2873
2888
AllowTupleLabelMismatch (ConstraintSystem &cs, Type fromType, Type toType,
2874
2889
ConstraintLocator *locator)
2875
2890
: ContextualMismatch(cs, FixKind::AllowTupleLabelMismatch, fromType,
2876
- toType, locator, DiagnosticBehavior::Warning ) {}
2891
+ toType, locator, FixBehavior::AlwaysWarning ) {}
2877
2892
2878
2893
public:
2879
2894
std::string getName () const override { return " allow tuple label mismatch" ; }
0 commit comments