1717// ===----------------------------------------------------------------------===//
1818
1919#include " CSDiagnostics.h"
20+ #include " TypeCheckConcurrency.h"
2021#include " swift/AST/Expr.h"
2122#include " swift/AST/ParameterList.h"
2223#include " swift/AST/Type.h"
@@ -37,7 +38,7 @@ using namespace constraints;
3738
3839ConstraintFix::~ConstraintFix () {}
3940
40- Optional<ScoreKind> ConstraintFix::affectsSolutionScore () const {
41+ Optional<ScoreKind> ConstraintFix::impact () const {
4142 switch (fixBehavior) {
4243 case FixBehavior::AlwaysWarning:
4344 return None;
@@ -243,10 +244,42 @@ MarkExplicitlyEscaping::create(ConstraintSystem &cs, Type lhs, Type rhs,
243244bool MarkGlobalActorFunction::diagnose (const Solution &solution,
244245 bool asNote) const {
245246 DroppedGlobalActorFunctionAttr failure (
246- solution, getFromType (), getToType (), getLocator (), diagfixBehavior () );
247+ solution, getFromType (), getToType (), getLocator (), fixBehavior );
247248 return failure.diagnose (asNote);
248249}
249250
251+ // / The fix behavior to apply to a concurrency-related diagnostic.
252+ static Optional<FixBehavior>
253+ getConcurrencyFixBehavior (
254+ ConstraintSystem &cs, ConstraintKind constraintKind,
255+ ConstraintLocatorBuilder locator, bool forSendable) {
256+ // We can only handle the downgrade for conversions.
257+ switch (constraintKind) {
258+ case ConstraintKind::Conversion:
259+ case ConstraintKind::ArgumentConversion:
260+ break ;
261+
262+ default :
263+ if (!cs.shouldAttemptFixes ())
264+ return None;
265+
266+ return FixBehavior::Error;
267+ }
268+
269+ // For a @preconcurrency callee outside of a strict concurrency
270+ // context, ignore.
271+ if (cs.hasPreconcurrencyCallee (locator) &&
272+ !contextRequiresStrictConcurrencyChecking (
273+ cs.DC , GetClosureType{cs}, ClosureIsolatedByPreconcurrency{cs}))
274+ return FixBehavior::Suppress;
275+
276+ // Otherwise, warn until Swift 6.
277+ if (!cs.getASTContext ().LangOpts .isSwiftVersionAtLeast (6 ))
278+ return FixBehavior::DowngradeToWarning;
279+
280+ return FixBehavior::Error;
281+ }
282+
250283MarkGlobalActorFunction *
251284MarkGlobalActorFunction::create (ConstraintSystem &cs, Type lhs, Type rhs,
252285 ConstraintLocator *locator,
@@ -259,11 +292,28 @@ MarkGlobalActorFunction::create(ConstraintSystem &cs, Type lhs, Type rhs,
259292 cs, lhs, rhs, locator, fixBehavior);
260293}
261294
295+ bool MarkGlobalActorFunction::attempt (ConstraintSystem &cs,
296+ ConstraintKind constraintKind,
297+ FunctionType *fromType,
298+ FunctionType *toType,
299+ ConstraintLocatorBuilder locator) {
300+ auto fixBehavior = getConcurrencyFixBehavior (
301+ cs, constraintKind, locator, /* forSendable=*/ false );
302+ if (!fixBehavior)
303+ return true ;
304+
305+ auto *fix = MarkGlobalActorFunction::create (
306+ cs, fromType, toType, cs.getConstraintLocator (locator),
307+ *fixBehavior);
308+
309+ return cs.recordFix (fix);
310+ }
311+
262312bool AddSendableAttribute::diagnose (const Solution &solution,
263313 bool asNote) const {
264314 AttributedFuncToTypeConversionFailure failure (
265315 solution, getFromType (), getToType (), getLocator (),
266- AttributedFuncToTypeConversionFailure::Concurrent, diagfixBehavior () );
316+ AttributedFuncToTypeConversionFailure::Concurrent, fixBehavior );
267317 return failure.diagnose (asNote);
268318}
269319
@@ -280,6 +330,22 @@ AddSendableAttribute::create(ConstraintSystem &cs,
280330 return new (cs.getAllocator ()) AddSendableAttribute (
281331 cs, fromType, toType, locator, fixBehavior);
282332}
333+
334+ bool AddSendableAttribute::attempt (ConstraintSystem &cs,
335+ ConstraintKind constraintKind,
336+ FunctionType *fromType,
337+ FunctionType *toType,
338+ ConstraintLocatorBuilder locator) {
339+ auto fixBehavior = getConcurrencyFixBehavior (
340+ cs, constraintKind, locator, /* forSendable=*/ true );
341+ if (!fixBehavior)
342+ return true ;
343+
344+ auto *fix = AddSendableAttribute::create (
345+ cs, fromType, toType, cs.getConstraintLocator (locator), *fixBehavior);
346+ return cs.recordFix (fix);
347+ }
348+
283349bool RelabelArguments::diagnose (const Solution &solution, bool asNote) const {
284350 LabelingFailure failure (solution, getLocator (), getLabels ());
285351 return failure.diagnose (asNote);
@@ -1639,7 +1705,7 @@ bool TreatEphemeralAsNonEphemeral::diagnose(const Solution &solution,
16391705 bool asNote) const {
16401706 NonEphemeralConversionFailure failure (solution, getLocator (), getFromType (),
16411707 getToType (), ConversionKind,
1642- diagfixBehavior () );
1708+ fixBehavior );
16431709 return failure.diagnose (asNote);
16441710}
16451711
@@ -2233,7 +2299,7 @@ IgnoreDefaultExprTypeMismatch::create(ConstraintSystem &cs, Type argType,
22332299bool AddExplicitExistentialCoercion::diagnose (const Solution &solution,
22342300 bool asNote) const {
22352301 MissingExplicitExistentialCoercion failure (solution, ErasedResultType,
2236- getLocator (), diagfixBehavior () );
2302+ getLocator (), fixBehavior );
22372303 return failure.diagnose (asNote);
22382304}
22392305
0 commit comments