Skip to content

Commit f18fe09

Browse files
[CSFix] Create AllowKeyPathRootTypeMismatch to diagnose key path root fail on key path application
1 parent 6e5be5b commit f18fe09

File tree

2 files changed

+44
-0
lines changed

2 files changed

+44
-0
lines changed

lib/Sema/CSFix.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,3 +1293,17 @@ AllowCoercionToForceCast::create(ConstraintSystem &cs, Type fromType,
12931293
return new (cs.getAllocator())
12941294
AllowCoercionToForceCast(cs, fromType, toType, locator);
12951295
}
1296+
1297+
bool AllowKeyPathRootTypeMismatch::diagnose(const Solution &solution,
1298+
bool asNote) const {
1299+
KeyPathRootTypeMismatchFailure failure(solution, getFromType(), getToType(),
1300+
getLocator());
1301+
return failure.diagnose(asNote);
1302+
}
1303+
1304+
AllowKeyPathRootTypeMismatch *
1305+
AllowKeyPathRootTypeMismatch::create(ConstraintSystem &cs, Type lhs, Type rhs,
1306+
ConstraintLocator *locator) {
1307+
return new (cs.getAllocator())
1308+
AllowKeyPathRootTypeMismatch(cs, lhs, rhs, locator);
1309+
}

lib/Sema/CSFix.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,10 @@ enum class FixKind : uint8_t {
251251

252252
/// A warning fix that allows a coercion to perform a force-cast.
253253
AllowCoercionToForceCast,
254+
255+
/// Allow key path root type mismatch when applying a key path with a
256+
/// root type not convertible to the type of the base instance.
257+
AllowKeyPathRootTypeMismatch,
254258
};
255259

256260
class ConstraintFix {
@@ -1768,6 +1772,32 @@ class AllowCoercionToForceCast final : public ContextualMismatch {
17681772
ConstraintLocator *locator);
17691773
};
17701774

1775+
/// Attempt to fix a key path application where the key path type cannot be
1776+
/// applied to a base instance of another type.
1777+
///
1778+
/// \code
1779+
/// func f (_ bar: Bar , keyPath: KeyPath<Foo, Int> ) {
1780+
/// bar[keyPath: keyPath]
1781+
/// }
1782+
/// \endcode
1783+
class AllowKeyPathRootTypeMismatch : public ContextualMismatch {
1784+
protected:
1785+
AllowKeyPathRootTypeMismatch(ConstraintSystem &cs, Type lhs, Type rhs,
1786+
ConstraintLocator *locator)
1787+
: ContextualMismatch(cs, FixKind::AllowKeyPathRootTypeMismatch, lhs, rhs,
1788+
locator) {}
1789+
1790+
public:
1791+
std::string getName() const override {
1792+
return "allow key path root type mismatch";
1793+
}
1794+
1795+
bool diagnose(const Solution &solution, bool asNote = false) const override;
1796+
1797+
static AllowKeyPathRootTypeMismatch *
1798+
create(ConstraintSystem &cs, Type lhs, Type rhs, ConstraintLocator *locator);
1799+
};
1800+
17711801
} // end namespace constraints
17721802
} // end namespace swift
17731803

0 commit comments

Comments
 (0)