2525#include " swift/AST/Types.h"
2626#include " swift/Basic/Debug.h"
2727#include " swift/Sema/ConstraintLocator.h"
28+ #include " swift/Sema/FixBehavior.h"
2829#include " llvm/ADT/ArrayRef.h"
2930#include " llvm/ADT/SmallVector.h"
3031#include " llvm/Support/TrailingObjects.h"
@@ -45,6 +46,7 @@ class ConstraintSystem;
4546class ConstraintLocator ;
4647class ConstraintLocatorBuilder ;
4748enum class ConversionRestrictionKind ;
49+ enum ScoreKind: unsigned int ;
4850class Solution ;
4951struct MemberLookupResult ;
5052
@@ -402,13 +404,12 @@ class ConstraintFix {
402404 ConstraintLocator *Locator;
403405
404406 // / The behavior limit to apply to the diagnostics emitted.
405- DiagnosticBehavior behaviorLimit ;
407+ FixBehavior fixBehavior ;
406408
407409public:
408410 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) {}
412413
413414 virtual ~ConstraintFix ();
414415
@@ -419,14 +420,27 @@ class ConstraintFix {
419420
420421 FixKind getKind () const { return Kind; }
421422
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+ }
425435 }
426436
437+ // / Whether this kind of fix affects the solution score, and which score
438+ // / it affects.
439+ Optional<ScoreKind> affectsSolutionScore () const ;
440+
427441 // / The diagnostic behavior limit that will be applied to any emitted
428442 // / diagnostics.
429- DiagnosticBehavior diagBehaviorLimit () const { return behaviorLimit ; }
443+ FixBehavior diagfixBehavior () const { return fixBehavior ; }
430444
431445 virtual std::string getName () const = 0;
432446
@@ -672,16 +686,15 @@ class ContextualMismatch : public ConstraintFix {
672686
673687 ContextualMismatch (ConstraintSystem &cs, Type lhs, Type rhs,
674688 ConstraintLocator *locator,
675- DiagnosticBehavior behaviorLimit )
676- : ConstraintFix(cs, FixKind::ContextualMismatch, locator, behaviorLimit ),
689+ FixBehavior fixBehavior )
690+ : ConstraintFix(cs, FixKind::ContextualMismatch, locator, fixBehavior ),
677691 LHS (lhs), RHS(rhs) {}
678692
679693protected:
680694 ContextualMismatch (ConstraintSystem &cs, FixKind kind, Type lhs, Type rhs,
681695 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) {}
685698
686699public:
687700 std::string getName () const override { return " fix contextual mismatch" ; }
@@ -766,9 +779,9 @@ class MarkExplicitlyEscaping final : public ContextualMismatch {
766779class MarkGlobalActorFunction final : public ContextualMismatch {
767780 MarkGlobalActorFunction (ConstraintSystem &cs, Type lhs, Type rhs,
768781 ConstraintLocator *locator,
769- DiagnosticBehavior behaviorLimit )
782+ FixBehavior fixBehavior )
770783 : ContextualMismatch(cs, FixKind::MarkGlobalActorFunction, lhs, rhs,
771- locator, behaviorLimit ) {
784+ locator, fixBehavior ) {
772785 }
773786
774787public:
@@ -778,7 +791,7 @@ class MarkGlobalActorFunction final : public ContextualMismatch {
778791
779792 static MarkGlobalActorFunction *create (ConstraintSystem &cs, Type lhs,
780793 Type rhs, ConstraintLocator *locator,
781- DiagnosticBehavior behaviorLimit );
794+ FixBehavior fixBehavior );
782795
783796 static bool classof (ConstraintFix *fix) {
784797 return fix->getKind () == FixKind::MarkGlobalActorFunction;
@@ -814,9 +827,9 @@ class ForceOptional final : public ContextualMismatch {
814827class AddSendableAttribute final : public ContextualMismatch {
815828 AddSendableAttribute (ConstraintSystem &cs, FunctionType *fromType,
816829 FunctionType *toType, ConstraintLocator *locator,
817- DiagnosticBehavior behaviorLimit )
830+ FixBehavior fixBehavior )
818831 : ContextualMismatch(cs, FixKind::AddSendableAttribute, fromType, toType,
819- locator, behaviorLimit ) {
832+ locator, fixBehavior ) {
820833 assert (fromType->isSendable () != toType->isSendable ());
821834 }
822835
@@ -829,7 +842,7 @@ class AddSendableAttribute final : public ContextualMismatch {
829842 FunctionType *fromType,
830843 FunctionType *toType,
831844 ConstraintLocator *locator,
832- DiagnosticBehavior behaviorLimit );
845+ FixBehavior fixBehavior );
833846
834847 static bool classof (ConstraintFix *fix) {
835848 return fix->getKind () == FixKind::AddSendableAttribute;
@@ -1392,11 +1405,14 @@ class AllowTypeOrInstanceMember final : public AllowInvalidMemberRef {
13921405};
13931406
13941407class AllowInvalidPartialApplication final : public ConstraintFix {
1408+ bool isWarning;
1409+
13951410 AllowInvalidPartialApplication (bool isWarning, ConstraintSystem &cs,
13961411 ConstraintLocator *locator)
13971412 : ConstraintFix(cs, FixKind::AllowInvalidPartialApplication, locator,
1398- isWarning ? DiagnosticBehavior::Warning
1399- : DiagnosticBehavior::Unspecified) {}
1413+ isWarning ? FixBehavior::AlwaysWarning
1414+ : FixBehavior::Error),
1415+ isWarning (isWarning) {}
14001416
14011417public:
14021418 std::string getName () const override {
@@ -2130,10 +2146,9 @@ class AllowArgumentMismatch : public ContextualMismatch {
21302146
21312147 AllowArgumentMismatch (ConstraintSystem &cs, FixKind kind, Type argType,
21322148 Type paramType, ConstraintLocator *locator,
2133- DiagnosticBehavior behaviorLimit =
2134- DiagnosticBehavior::Unspecified)
2149+ FixBehavior fixBehavior = FixBehavior::Error)
21352150 : ContextualMismatch(
2136- cs, kind, argType, paramType, locator, behaviorLimit ) {}
2151+ cs, kind, argType, paramType, locator, fixBehavior ) {}
21372152
21382153public:
21392154 std::string getName () const override {
@@ -2281,9 +2296,9 @@ class TreatEphemeralAsNonEphemeral final : public AllowArgumentMismatch {
22812296 TreatEphemeralAsNonEphemeral (ConstraintSystem &cs, ConstraintLocator *locator,
22822297 Type srcType, Type dstType,
22832298 ConversionRestrictionKind conversionKind,
2284- DiagnosticBehavior behaviorLimit )
2299+ FixBehavior fixBehavior )
22852300 : AllowArgumentMismatch(cs, FixKind::TreatEphemeralAsNonEphemeral,
2286- srcType, dstType, locator, behaviorLimit ),
2301+ srcType, dstType, locator, fixBehavior ),
22872302 ConversionKind (conversionKind) {}
22882303
22892304public:
@@ -2447,7 +2462,7 @@ class AllowCoercionToForceCast final : public ContextualMismatch {
24472462 AllowCoercionToForceCast (ConstraintSystem &cs, Type fromType, Type toType,
24482463 ConstraintLocator *locator)
24492464 : ContextualMismatch(cs, FixKind::AllowCoercionToForceCast, fromType,
2450- toType, locator, DiagnosticBehavior::Warning ) {}
2465+ toType, locator, FixBehavior::AlwaysWarning ) {}
24512466
24522467public:
24532468 std::string getName () const override {
@@ -2561,7 +2576,7 @@ class SpecifyLabelToAssociateTrailingClosure final : public ConstraintFix {
25612576 SpecifyLabelToAssociateTrailingClosure (ConstraintSystem &cs,
25622577 ConstraintLocator *locator)
25632578 : ConstraintFix(cs, FixKind::SpecifyLabelToAssociateTrailingClosure,
2564- locator, DiagnosticBehavior::Warning ) {}
2579+ locator, FixBehavior::AlwaysWarning ) {}
25652580
25662581public:
25672582 std::string getName () const override {
@@ -2731,7 +2746,7 @@ class SpecifyBaseTypeForOptionalUnresolvedMember final : public ConstraintFix {
27312746 DeclNameRef memberName,
27322747 ConstraintLocator *locator)
27332748 : ConstraintFix(cs, FixKind::SpecifyBaseTypeForOptionalUnresolvedMember,
2734- locator, DiagnosticBehavior::Warning ),
2749+ locator, FixBehavior::AlwaysWarning ),
27352750 MemberName (memberName) {}
27362751 DeclNameRef MemberName;
27372752
@@ -2762,7 +2777,7 @@ class CheckedCastContextualMismatchWarning : public ContextualMismatch {
27622777 CheckedCastKind kind,
27632778 ConstraintLocator *locator)
27642779 : ContextualMismatch(cs, fixKind, fromType, toType, locator,
2765- DiagnosticBehavior::Warning ),
2780+ FixBehavior::AlwaysWarning ),
27662781 CastKind (kind) {}
27672782 CheckedCastKind CastKind;
27682783};
@@ -2873,7 +2888,7 @@ class AllowTupleLabelMismatch final : public ContextualMismatch {
28732888 AllowTupleLabelMismatch (ConstraintSystem &cs, Type fromType, Type toType,
28742889 ConstraintLocator *locator)
28752890 : ContextualMismatch(cs, FixKind::AllowTupleLabelMismatch, fromType,
2876- toType, locator, DiagnosticBehavior::Warning ) {}
2891+ toType, locator, FixBehavior::AlwaysWarning ) {}
28772892
28782893public:
28792894 std::string getName () const override { return " allow tuple label mismatch" ; }
@@ -2951,8 +2966,10 @@ class AddExplicitExistentialCoercion final : public ConstraintFix {
29512966 Type ErasedResultType;
29522967
29532968 AddExplicitExistentialCoercion (ConstraintSystem &cs, Type erasedResultTy,
2954- ConstraintLocator *locator)
2955- : ConstraintFix(cs, FixKind::AddExplicitExistentialCoercion, locator),
2969+ ConstraintLocator *locator,
2970+ FixBehavior fixBehavior)
2971+ : ConstraintFix(cs, FixKind::AddExplicitExistentialCoercion, locator,
2972+ fixBehavior),
29562973 ErasedResultType (erasedResultTy) {}
29572974
29582975public:
0 commit comments