Skip to content

Commit 2467b93

Browse files
committed
ConstraintSystem: Move key path type utilities to AST.
1 parent 8f7af45 commit 2467b93

File tree

10 files changed

+28
-22
lines changed

10 files changed

+28
-22
lines changed

include/swift/AST/Types.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,12 @@ class alignas(1 << TypeAlignInBits) TypeBase
11031103
/// Check if this is a std.string type from C++.
11041104
bool isCxxString();
11051105

1106+
/// Check if this type is known to represent key paths.
1107+
bool isKnownKeyPathType();
1108+
1109+
/// Check if this type is known to represent immutable key paths.
1110+
bool isKnownImmutableKeyPathType();
1111+
11061112
/// Check if this is either an Array, Set or Dictionary collection type defined
11071113
/// at the top level of the Swift module
11081114
bool isKnownStdlibCollectionType();

include/swift/Sema/ConstraintSystem.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6525,10 +6525,6 @@ class TypeVarRefCollector : public ASTWalker {
65256525
}
65266526
};
65276527

6528-
/// Determine whether given type is a known one
6529-
/// for a key path `{Any, Partial, Writable, ReferenceWritable}KeyPath`.
6530-
bool isKnownKeyPathType(Type type);
6531-
65326528
/// Determine whether the given type is a PartialKeyPath and
65336529
/// AnyKeyPath or existential type thererof, for example,
65346530
/// `PartialKeyPath<...> & Sendable`.

lib/AST/Type.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1309,6 +1309,15 @@ bool TypeBase::isCxxString() {
13091309
ctx.Id_basic_string.is(clangDecl->getName());
13101310
}
13111311

1312+
bool TypeBase::isKnownKeyPathType() {
1313+
return isKeyPath() || isWritableKeyPath() || isReferenceWritableKeyPath() ||
1314+
isPartialKeyPath() || isAnyKeyPath();
1315+
}
1316+
1317+
bool TypeBase::isKnownImmutableKeyPathType() {
1318+
return isKeyPath() || isPartialKeyPath() || isAnyKeyPath();
1319+
}
1320+
13121321
bool TypeBase::isKnownStdlibCollectionType() {
13131322
if (isArray() || isDictionary() || isSet()) {
13141323
return true;

lib/IDE/SelectedOverloadInfo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ swift::ide::getSelectedOverloadInfo(const Solution &S,
7979
fnType->getParams()[0].getPlainType()->castTo<BoundGenericType>();
8080

8181
auto *keyPathDecl = keyPathTy->getAnyNominal();
82-
assert(isKnownKeyPathType(keyPathTy) &&
82+
assert(keyPathTy->isKnownKeyPathType() &&
8383
"parameter is supposed to be a keypath");
8484

8585
auto KeyPathDynamicLocator = CS.getConstraintLocator(

lib/Sema/CSApply.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2689,7 +2689,7 @@ namespace {
26892689
Type keyPathTy = paramType;
26902690
if (auto *existential = keyPathTy->getAs<ExistentialType>()) {
26912691
keyPathTy = existential->getSuperclass();
2692-
assert(isKnownKeyPathType(keyPathTy));
2692+
assert(keyPathTy->isKnownKeyPathType());
26932693
}
26942694

26952695
SmallVector<Component, 2> components;

lib/Sema/CSBindings.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -513,7 +513,7 @@ void BindingSet::inferTransitiveBindings() {
513513
auto bindingTy = binding.BindingType->lookThroughAllOptionalTypes();
514514

515515
Type inferredRootTy;
516-
if (isKnownKeyPathType(bindingTy)) {
516+
if (bindingTy->isKnownKeyPathType()) {
517517
// AnyKeyPath doesn't have a root type.
518518
if (bindingTy->isAnyKeyPath())
519519
continue;
@@ -727,7 +727,8 @@ bool BindingSet::finalize(bool transitive) {
727727
for (const auto &binding : Bindings) {
728728
auto bindingTy = binding.BindingType->lookThroughAllOptionalTypes();
729729

730-
assert(isKnownKeyPathType(bindingTy) || bindingTy->is<FunctionType>());
730+
assert(bindingTy->isKnownKeyPathType() ||
731+
bindingTy->is<FunctionType>());
731732

732733
// Functions don't have capability so we can simply add them.
733734
if (auto *fnType = bindingTy->getAs<FunctionType>()) {
@@ -1694,15 +1695,15 @@ PotentialBindings::inferFromRelational(ConstraintSystem &CS,
16941695
if (type->isExistentialType()) {
16951696
auto layout = type->getExistentialLayout();
16961697
if (auto superclass = layout.explicitSuperclass) {
1697-
if (isKnownKeyPathType(superclass)) {
1698+
if (superclass->isKnownKeyPathType()) {
16981699
type = superclass;
16991700
objectTy = superclass;
17001701
}
17011702
}
17021703
}
17031704
}
17041705

1705-
if (!(isKnownKeyPathType(objectTy) || objectTy->is<AnyFunctionType>()))
1706+
if (!(objectTy->isKnownKeyPathType() || objectTy->is<AnyFunctionType>()))
17061707
return std::nullopt;
17071708
}
17081709

lib/Sema/CSDiagnostics.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2262,7 +2262,7 @@ bool AssignmentFailure::diagnoseAsError() {
22622262

22632263
auto type = getType(immutableExpr);
22642264

2265-
if (isKnownKeyPathType(type))
2265+
if (type->isKnownKeyPathType())
22662266
message += " is read-only";
22672267
else if (VD->isCaptureList())
22682268
message += " is an immutable capture";
@@ -7487,7 +7487,7 @@ bool ArgumentMismatchFailure::diagnoseAsError() {
74877487

74887488
// Unresolved key path argument requires a tailored diagnostic
74897489
// that doesn't mention a fallback type - `KeyPath<_, _>`.
7490-
if (argType->isKeyPath() && !isKnownKeyPathType(paramType)) {
7490+
if (argType->isKeyPath() && !paramType->isKnownKeyPathType()) {
74917491
auto keyPathTy = argType->castTo<BoundGenericType>();
74927492
auto rootTy = keyPathTy->getGenericArgs()[0];
74937493
if (rootTy->is<UnresolvedType>()) {
@@ -7852,7 +7852,7 @@ bool ArgumentMismatchFailure::diagnoseKeyPathAsFunctionResultMismatch() const {
78527852
auto argType = getFromType();
78537853
auto paramType = getToType();
78547854

7855-
if (!isKnownKeyPathType(argType))
7855+
if (!argType->isKnownKeyPathType())
78567856
return false;
78577857

78587858
auto kpType = argType->castTo<BoundGenericType>();

lib/Sema/CSSimplify.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5598,7 +5598,7 @@ bool ConstraintSystem::repairFailures(
55985598
// fix-up here unless last component has already a invalid type or
55995599
// instance fix recorded.
56005600
if (isExpr<KeyPathExpr>(anchor)) {
5601-
if (isKnownKeyPathType(lhs) && isKnownKeyPathType(rhs)) {
5601+
if (lhs->isKnownKeyPathType() && rhs->isKnownKeyPathType()) {
56025602
// If we have a conversion happening here, we should let fix happen in
56035603
// simplifyRestrictedConstraint.
56045604
if (hasAnyRestriction())

lib/Sema/ConstraintSystem.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4363,12 +4363,6 @@ Solution::getFunctionArgApplyInfo(ConstraintLocator *locator) const {
43634363
fnInterfaceType, fnType, callee);
43644364
}
43654365

4366-
bool constraints::isKnownKeyPathType(Type type) {
4367-
return type->isKeyPath() || type->isWritableKeyPath() ||
4368-
type->isReferenceWritableKeyPath() || type->isPartialKeyPath() ||
4369-
type->isAnyKeyPath();
4370-
}
4371-
43724366
bool constraints::isTypeErasedKeyPathType(Type type) {
43734367
assert(type);
43744368

lib/Sema/TypeOfReference.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2392,13 +2392,13 @@ void ConstraintSystem::bindOverloadType(const SelectedOverload &overload,
23922392

23932393
if (auto *existential = paramTy->getAs<ExistentialType>()) {
23942394
paramTy = existential->getSuperclass();
2395-
assert(isKnownKeyPathType(paramTy));
2395+
assert(paramTy->isKnownKeyPathType());
23962396
}
23972397

23982398
auto keyPathTy = paramTy->castTo<BoundGenericType>();
23992399

24002400
auto *keyPathDecl = keyPathTy->getAnyNominal();
2401-
assert(isKnownKeyPathType(keyPathTy) &&
2401+
assert(keyPathTy->isKnownKeyPathType() &&
24022402
"parameter is supposed to be a keypath");
24032403

24042404
auto *keyPathLoc = getConstraintLocator(

0 commit comments

Comments
 (0)