Skip to content

Commit fb30555

Browse files
committed
[ConstraintSystem] NFC: Extract check whether storage for keypath is read-only
1 parent 5295bdc commit fb30555

File tree

2 files changed

+32
-22
lines changed

2 files changed

+32
-22
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4916,34 +4916,18 @@ ConstraintSystem::simplifyKeyPathConstraint(Type keyPathTy,
49164916
if (!storage) {
49174917
return SolutionKind::Error;
49184918
}
4919-
4920-
// See whether key paths can store to this component. (Key paths don't
4921-
// get any special power from being formed in certain contexts, such
4922-
// as the ability to assign to `let`s in initialization contexts, so
4923-
// we pass null for the DC to `isSettable` here.)
4924-
if (!getASTContext().isSwiftVersionAtLeast(5)) {
4925-
// As a source-compatibility measure, continue to allow
4926-
// WritableKeyPaths to be formed in the same conditions we did
4927-
// in previous releases even if we should not be able to set
4928-
// the value in this context.
4929-
if (!storage->isSettable(DC)) {
4930-
// A non-settable component makes the key path read-only, unless
4931-
// a reference-writable component shows up later.
4932-
capability = ReadOnly;
4933-
continue;
4934-
}
4935-
} else if (!storage->isSettable(nullptr)
4936-
|| !storage->isSetterAccessibleFrom(DC)) {
4937-
// A non-settable component makes the key path read-only, unless
4938-
// a reference-writable component shows up later.
4919+
4920+
if (isReadOnlyKeyPathComponent(storage)) {
49394921
capability = ReadOnly;
49404922
continue;
49414923
}
4924+
49424925
// A nonmutating setter indicates a reference-writable base.
49434926
if (!storage->isSetterMutating()) {
49444927
capability = ReferenceWritable;
49454928
continue;
49464929
}
4930+
49474931
// Otherwise, the key path maintains its current capability.
49484932
break;
49494933
}
@@ -4991,8 +4975,9 @@ ConstraintSystem::simplifyKeyPathConstraint(Type keyPathTy,
49914975
if (keyPathBGT) {
49924976
if (keyPathBGT->getDecl() == getASTContext().getKeyPathDecl())
49934977
kpDecl = getASTContext().getKeyPathDecl();
4994-
else if (keyPathBGT->getDecl() == getASTContext().getWritableKeyPathDecl()
4995-
&& capability >= Writable)
4978+
else if (keyPathBGT->getDecl() ==
4979+
getASTContext().getWritableKeyPathDecl() &&
4980+
capability >= Writable)
49964981
kpDecl = getASTContext().getWritableKeyPathDecl();
49974982
}
49984983

lib/Sema/ConstraintSystem.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3214,6 +3214,31 @@ class ConstraintSystem {
32143214
bool restoreOnFail,
32153215
llvm::function_ref<bool(Constraint *)> pred);
32163216

3217+
bool isReadOnlyKeyPathComponent(const AbstractStorageDecl *storage) {
3218+
// See whether key paths can store to this component. (Key paths don't
3219+
// get any special power from being formed in certain contexts, such
3220+
// as the ability to assign to `let`s in initialization contexts, so
3221+
// we pass null for the DC to `isSettable` here.)
3222+
if (!getASTContext().isSwiftVersionAtLeast(5)) {
3223+
// As a source-compatibility measure, continue to allow
3224+
// WritableKeyPaths to be formed in the same conditions we did
3225+
// in previous releases even if we should not be able to set
3226+
// the value in this context.
3227+
if (!storage->isSettable(DC)) {
3228+
// A non-settable component makes the key path read-only, unless
3229+
// a reference-writable component shows up later.
3230+
return true;
3231+
}
3232+
} else if (!storage->isSettable(nullptr) ||
3233+
!storage->isSetterAccessibleFrom(DC)) {
3234+
// A non-settable component makes the key path read-only, unless
3235+
// a reference-writable component shows up later.
3236+
return true;
3237+
}
3238+
3239+
return false;
3240+
}
3241+
32173242
public:
32183243
// Given a type variable, attempt to find the disjunction of
32193244
// bind overloads associated with it. This may return null in cases where

0 commit comments

Comments
 (0)