Skip to content

Commit 68d7278

Browse files
committed
[ConstraintSystem] NFC: Move implementation of isReadOnlyKeyPathComponent to .cpp
1 parent 05b223a commit 68d7278

File tree

2 files changed

+38
-35
lines changed

2 files changed

+38
-35
lines changed

lib/Sema/ConstraintSystem.cpp

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "CSDiagnostics.h"
2121
#include "CSFix.h"
2222
#include "SolutionResult.h"
23+
#include "TypeChecker.h"
2324
#include "TypeCheckType.h"
2425
#include "swift/AST/Initializer.h"
2526
#include "swift/AST/GenericEnvironment.h"
@@ -5215,3 +5216,39 @@ Type ConstraintSystem::getVarType(const VarDecl *var) {
52155216
return HoleType::get(Context, const_cast<VarDecl *>(var));
52165217
});
52175218
}
5219+
5220+
bool ConstraintSystem::isReadOnlyKeyPathComponent(
5221+
const AbstractStorageDecl *storage, SourceLoc referenceLoc) {
5222+
// See whether key paths can store to this component. (Key paths don't
5223+
// get any special power from being formed in certain contexts, such
5224+
// as the ability to assign to `let`s in initialization contexts, so
5225+
// we pass null for the DC to `isSettable` here.)
5226+
if (!getASTContext().isSwiftVersionAtLeast(5)) {
5227+
// As a source-compatibility measure, continue to allow
5228+
// WritableKeyPaths to be formed in the same conditions we did
5229+
// in previous releases even if we should not be able to set
5230+
// the value in this context.
5231+
if (!storage->isSettable(DC)) {
5232+
// A non-settable component makes the key path read-only, unless
5233+
// a reference-writable component shows up later.
5234+
return true;
5235+
}
5236+
} else if (!storage->isSettable(nullptr) ||
5237+
!storage->isSetterAccessibleFrom(DC)) {
5238+
// A non-settable component makes the key path read-only, unless
5239+
// a reference-writable component shows up later.
5240+
return true;
5241+
}
5242+
5243+
// If the setter is unavailable, then the keypath ought to be read-only
5244+
// in this context.
5245+
if (auto setter = storage->getOpaqueAccessor(AccessorKind::Set)) {
5246+
auto maybeUnavail =
5247+
TypeChecker::checkDeclarationAvailability(setter, referenceLoc, DC);
5248+
if (maybeUnavail.hasValue()) {
5249+
return true;
5250+
}
5251+
}
5252+
5253+
return false;
5254+
}

lib/Sema/ConstraintSystem.h

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4971,41 +4971,7 @@ class ConstraintSystem {
49714971
llvm::function_ref<bool(Constraint *)> pred);
49724972

49734973
bool isReadOnlyKeyPathComponent(const AbstractStorageDecl *storage,
4974-
SourceLoc referenceLoc) {
4975-
// See whether key paths can store to this component. (Key paths don't
4976-
// get any special power from being formed in certain contexts, such
4977-
// as the ability to assign to `let`s in initialization contexts, so
4978-
// we pass null for the DC to `isSettable` here.)
4979-
if (!getASTContext().isSwiftVersionAtLeast(5)) {
4980-
// As a source-compatibility measure, continue to allow
4981-
// WritableKeyPaths to be formed in the same conditions we did
4982-
// in previous releases even if we should not be able to set
4983-
// the value in this context.
4984-
if (!storage->isSettable(DC)) {
4985-
// A non-settable component makes the key path read-only, unless
4986-
// a reference-writable component shows up later.
4987-
return true;
4988-
}
4989-
} else if (!storage->isSettable(nullptr) ||
4990-
!storage->isSetterAccessibleFrom(DC)) {
4991-
// A non-settable component makes the key path read-only, unless
4992-
// a reference-writable component shows up later.
4993-
return true;
4994-
}
4995-
4996-
// If the setter is unavailable, then the keypath ought to be read-only
4997-
// in this context.
4998-
if (auto setter = storage->getOpaqueAccessor(AccessorKind::Set)) {
4999-
auto maybeUnavail = TypeChecker::checkDeclarationAvailability(setter,
5000-
referenceLoc,
5001-
DC);
5002-
if (maybeUnavail.hasValue()) {
5003-
return true;
5004-
}
5005-
}
5006-
5007-
return false;
5008-
}
4974+
SourceLoc referenceLoc);
50094975

50104976
public:
50114977
// Given a type variable, attempt to find the disjunction of

0 commit comments

Comments
 (0)