Skip to content

Commit f259286

Browse files
committed
[CSFix] Convert missing explicit @escaping fix to be a contextual mismatch
Sometimes diagnostic needs both sides of the conversion e.g. when there is an attempt to bind generic argument to non-escaping type.
1 parent 413dbbc commit f259286

File tree

3 files changed

+28
-34
lines changed

3 files changed

+28
-34
lines changed

lib/Sema/CSFix.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -159,16 +159,16 @@ CoerceToCheckedCast *CoerceToCheckedCast::attempt(ConstraintSystem &cs,
159159
}
160160

161161
bool MarkExplicitlyEscaping::diagnose(bool asNote) const {
162-
NoEscapeFuncToTypeConversionFailure failure(getConstraintSystem(),
163-
getLocator(), ConvertTo);
162+
auto &cs = getConstraintSystem();
163+
NoEscapeFuncToTypeConversionFailure failure(cs, getFromType(), getToType(),
164+
getLocator());
164165
return failure.diagnose(asNote);
165166
}
166167

167168
MarkExplicitlyEscaping *
168-
MarkExplicitlyEscaping::create(ConstraintSystem &cs, ConstraintLocator *locator,
169-
Type convertingTo) {
170-
return new (cs.getAllocator())
171-
MarkExplicitlyEscaping(cs, locator, convertingTo);
169+
MarkExplicitlyEscaping::create(ConstraintSystem &cs, Type lhs, Type rhs,
170+
ConstraintLocator *locator) {
171+
return new (cs.getAllocator()) MarkExplicitlyEscaping(cs, lhs, rhs, locator);
172172
}
173173

174174
bool RelabelArguments::diagnose(bool asNote) const {

lib/Sema/CSFix.h

Lines changed: 16 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -322,27 +322,6 @@ class TreatRValueAsLValue final : public ConstraintFix {
322322
ConstraintLocator *locator);
323323
};
324324

325-
/// Mark function type as explicitly '@escaping'.
326-
class MarkExplicitlyEscaping final : public ConstraintFix {
327-
/// Sometimes function type has to be marked as '@escaping'
328-
/// to be converted to some other generic type.
329-
Type ConvertTo;
330-
331-
MarkExplicitlyEscaping(ConstraintSystem &cs, ConstraintLocator *locator,
332-
Type convertingTo = Type())
333-
: ConstraintFix(cs, FixKind::ExplicitlyEscaping, locator),
334-
ConvertTo(convertingTo) {}
335-
336-
public:
337-
std::string getName() const override { return "add @escaping"; }
338-
339-
bool diagnose(bool asNote = false) const override;
340-
341-
static MarkExplicitlyEscaping *create(ConstraintSystem &cs,
342-
ConstraintLocator *locator,
343-
Type convertingTo = Type());
344-
};
345-
346325
/// Arguments have labeling failures - missing/extraneous or incorrect
347326
/// labels attached to the, fix it by suggesting proper labels.
348327
class RelabelArguments final
@@ -496,6 +475,22 @@ class ContextualMismatch : public ConstraintFix {
496475
ConstraintLocator *locator);
497476
};
498477

478+
/// Mark function type as explicitly '@escaping'.
479+
class MarkExplicitlyEscaping final : public ContextualMismatch {
480+
MarkExplicitlyEscaping(ConstraintSystem &cs, Type lhs, Type rhs,
481+
ConstraintLocator *locator)
482+
: ContextualMismatch(cs, FixKind::ExplicitlyEscaping, lhs, rhs, locator) {
483+
}
484+
485+
public:
486+
std::string getName() const override { return "add @escaping"; }
487+
488+
bool diagnose(bool asNote = false) const override;
489+
490+
static MarkExplicitlyEscaping *create(ConstraintSystem &cs, Type lhs,
491+
Type rhs, ConstraintLocator *locator);
492+
};
493+
499494
/// Introduce a '!' to force an optional unwrap.
500495
class ForceOptional final : public ContextualMismatch {
501496
ForceOptional(ConstraintSystem &cs, Type fromType, Type toType,

lib/Sema/CSSimplify.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,8 +1548,8 @@ ConstraintSystem::matchFunctionTypes(FunctionType *func1, FunctionType *func2,
15481548
if (!shouldAttemptFixes())
15491549
return getTypeMatchFailure(locator);
15501550

1551-
auto *fix = MarkExplicitlyEscaping::create(
1552-
*this, getConstraintLocator(locator), func2);
1551+
auto *fix = MarkExplicitlyEscaping::create(*this, func1, func2,
1552+
getConstraintLocator(locator));
15531553

15541554
if (recordFix(fix))
15551555
return getTypeMatchFailure(locator);
@@ -2084,9 +2084,8 @@ ConstraintSystem::matchExistentialTypes(Type type1, Type type2,
20842084
return getTypeMatchSuccess();
20852085

20862086
if (shouldAttemptFixes()) {
2087-
auto &ctx = getASTContext();
2088-
auto *fix = MarkExplicitlyEscaping::create(
2089-
*this, getConstraintLocator(locator), ctx.TheAnyType);
2087+
auto *fix = MarkExplicitlyEscaping::create(*this, type1, type2,
2088+
getConstraintLocator(locator));
20902089
if (!recordFix(fix))
20912090
return getTypeMatchSuccess();
20922091
}
@@ -2348,8 +2347,8 @@ ConstraintSystem::matchTypesBindTypeVar(
23482347
// but we still have a non-escaping type, fail.
23492348
if (!typeVar->getImpl().canBindToNoEscape() && type->isNoEscape()) {
23502349
if (shouldAttemptFixes()) {
2351-
auto *fix = MarkExplicitlyEscaping::create(
2352-
*this, getConstraintLocator(locator));
2350+
auto *fix = MarkExplicitlyEscaping::create(*this, typeVar, type,
2351+
getConstraintLocator(locator));
23532352
if (recordFix(fix))
23542353
return getTypeMatchFailure(locator);
23552354

0 commit comments

Comments
 (0)