Skip to content

Commit c228504

Browse files
committed
Sema: KeyPath literals should be read-only when referring to unavailable setters.
Part of rdar://problem/65152582, where key paths were generating references to setters in contexts where they weren't available.
1 parent 8dd08e7 commit c228504

File tree

3 files changed

+50
-3
lines changed

3 files changed

+50
-3
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6135,7 +6135,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
61356135
// If this is an attempt to access read-only member via
61366136
// writable key path, let's fail this choice early.
61376137
auto &ctx = getASTContext();
6138-
if (isReadOnlyKeyPathComponent(storage) &&
6138+
if (isReadOnlyKeyPathComponent(storage, SourceLoc()) &&
61396139
(keyPath == ctx.getWritableKeyPathDecl() ||
61406140
keyPath == ctx.getReferenceWritableKeyPathDecl())) {
61416141
result.addUnviable(
@@ -7850,7 +7850,7 @@ ConstraintSystem::simplifyKeyPathConstraint(
78507850
if (!storage)
78517851
return SolutionKind::Error;
78527852

7853-
if (isReadOnlyKeyPathComponent(storage)) {
7853+
if (isReadOnlyKeyPathComponent(storage, component.getLoc())) {
78547854
capability = ReadOnly;
78557855
continue;
78567856
}

lib/Sema/ConstraintSystem.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4474,7 +4474,8 @@ class ConstraintSystem {
44744474
bool restoreOnFail,
44754475
llvm::function_ref<bool(Constraint *)> pred);
44764476

4477-
bool isReadOnlyKeyPathComponent(const AbstractStorageDecl *storage) {
4477+
bool isReadOnlyKeyPathComponent(const AbstractStorageDecl *storage,
4478+
SourceLoc referenceLoc) {
44784479
// See whether key paths can store to this component. (Key paths don't
44794480
// get any special power from being formed in certain contexts, such
44804481
// as the ability to assign to `let`s in initialization contexts, so
@@ -4495,6 +4496,17 @@ class ConstraintSystem {
44954496
// a reference-writable component shows up later.
44964497
return true;
44974498
}
4499+
4500+
// If the setter is unavailable, then the keypath ought to be read-only
4501+
// in this context.
4502+
if (auto setter = storage->getOpaqueAccessor(AccessorKind::Set)) {
4503+
auto maybeUnavail = TypeChecker::checkDeclarationAvailability(setter,
4504+
referenceLoc,
4505+
DC);
4506+
if (maybeUnavail.hasValue()) {
4507+
return true;
4508+
}
4509+
}
44984510

44994511
return false;
45004512
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.9 -typecheck -verify %s
2+
// REQUIRES: OS=macosx
3+
4+
struct Butt {
5+
var setter_conditionally_available: Int {
6+
get { fatalError() }
7+
8+
@available(macOS 10.10, *)
9+
set { fatalError() }
10+
}
11+
}
12+
13+
func assertExactType<T>(of _: inout T, is _: T.Type) {}
14+
15+
@available(macOS 10.9, *)
16+
public func unavailableSetterContext() {
17+
var kp = \Butt.setter_conditionally_available
18+
assertExactType(of: &kp, is: KeyPath<Butt, Int>.self)
19+
}
20+
@available(macOS 10.10, *)
21+
public func availableSetterContext() {
22+
var kp = \Butt.setter_conditionally_available
23+
assertExactType(of: &kp, is: WritableKeyPath<Butt, Int>.self)
24+
}
25+
@available(macOS 10.9, *)
26+
public func conditionalAvailableSetterContext() {
27+
if #available(macOS 10.10, *) {
28+
var kp = \Butt.setter_conditionally_available
29+
assertExactType(of: &kp, is: WritableKeyPath<Butt, Int>.self)
30+
} else {
31+
var kp = \Butt.setter_conditionally_available
32+
assertExactType(of: &kp, is: KeyPath<Butt, Int>.self)
33+
}
34+
}
35+

0 commit comments

Comments
 (0)