Skip to content

Commit 88e17a6

Browse files
authored
Merge pull request #32819 from jckarter/keypath-writable-availability
Availability-related fixes with keypaths, _modify, and/or property wrappers
2 parents bf5698f + 85b549a commit 88e17a6

File tree

5 files changed

+188
-4
lines changed

5 files changed

+188
-4
lines changed

lib/Sema/CSSimplify.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6268,7 +6268,7 @@ performMemberLookup(ConstraintKind constraintKind, DeclNameRef memberName,
62686268
// If this is an attempt to access read-only member via
62696269
// writable key path, let's fail this choice early.
62706270
auto &ctx = getASTContext();
6271-
if (isReadOnlyKeyPathComponent(storage) &&
6271+
if (isReadOnlyKeyPathComponent(storage, SourceLoc()) &&
62726272
(keyPath == ctx.getWritableKeyPathDecl() ||
62736273
keyPath == ctx.getReferenceWritableKeyPathDecl())) {
62746274
result.addUnviable(
@@ -8020,7 +8020,7 @@ ConstraintSystem::simplifyKeyPathConstraint(
80208020
if (!storage)
80218021
return SolutionKind::Error;
80228022

8023-
if (isReadOnlyKeyPathComponent(storage)) {
8023+
if (isReadOnlyKeyPathComponent(storage, component.getLoc())) {
80248024
capability = ReadOnly;
80258025
continue;
80268026
}

lib/Sema/ConstraintSystem.h

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

4674-
bool isReadOnlyKeyPathComponent(const AbstractStorageDecl *storage) {
4674+
bool isReadOnlyKeyPathComponent(const AbstractStorageDecl *storage,
4675+
SourceLoc referenceLoc) {
46754676
// See whether key paths can store to this component. (Key paths don't
46764677
// get any special power from being formed in certain contexts, such
46774678
// as the ability to assign to `let`s in initialization contexts, so
@@ -4692,6 +4693,17 @@ class ConstraintSystem {
46924693
// a reference-writable component shows up later.
46934694
return true;
46944695
}
4696+
4697+
// If the setter is unavailable, then the keypath ought to be read-only
4698+
// in this context.
4699+
if (auto setter = storage->getOpaqueAccessor(AccessorKind::Set)) {
4700+
auto maybeUnavail = TypeChecker::checkDeclarationAvailability(setter,
4701+
referenceLoc,
4702+
DC);
4703+
if (maybeUnavail.hasValue()) {
4704+
return true;
4705+
}
4706+
}
46954707

46964708
return false;
46974709
}

lib/Sema/TypeCheckStorage.cpp

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1659,7 +1659,7 @@ synthesizeSetterBody(AccessorDecl *setter, ASTContext &ctx) {
16591659
case WriteImplKind::Modify:
16601660
return synthesizeModifyCoroutineSetterBody(setter, ctx);
16611661
}
1662-
llvm_unreachable("bad ReadImplKind");
1662+
llvm_unreachable("bad WriteImplKind");
16631663
}
16641664

16651665
static std::pair<BraceStmt *, bool>
@@ -1940,7 +1940,75 @@ static AccessorDecl *createSetterPrototype(AbstractStorageDecl *storage,
19401940

19411941
// All mutable storage requires a setter.
19421942
assert(storage->requiresOpaqueAccessor(AccessorKind::Set));
1943+
1944+
// Copy availability from the accessor we'll synthesize the setter from.
1945+
SmallVector<Decl *, 2> asAvailableAs;
1946+
1947+
// That could be a property wrapper...
1948+
if (auto var = dyn_cast<VarDecl>(storage)) {
1949+
if (var->hasAttachedPropertyWrapper()) {
1950+
// The property wrapper info may not actually link back to a wrapper
1951+
// implementation, if there was a semantic error checking the wrapper.
1952+
auto info = var->getAttachedPropertyWrapperTypeInfo(0);
1953+
if (info.valueVar) {
1954+
if (auto setter = info.valueVar->getOpaqueAccessor(AccessorKind::Set)) {
1955+
asAvailableAs.push_back(setter);
1956+
}
1957+
}
1958+
} else if (auto wrapperSynthesizedKind
1959+
= var->getPropertyWrapperSynthesizedPropertyKind()) {
1960+
switch (*wrapperSynthesizedKind) {
1961+
case PropertyWrapperSynthesizedPropertyKind::Backing:
1962+
break;
1963+
1964+
case PropertyWrapperSynthesizedPropertyKind::StorageWrapper: {
1965+
if (auto origVar = var->getOriginalWrappedProperty(wrapperSynthesizedKind)) {
1966+
// The property wrapper info may not actually link back to a wrapper
1967+
// implementation, if there was a semantic error checking the wrapper.
1968+
auto info = origVar->getAttachedPropertyWrapperTypeInfo(0);
1969+
if (info.projectedValueVar) {
1970+
if (auto setter
1971+
= info.projectedValueVar->getOpaqueAccessor(AccessorKind::Set)){
1972+
asAvailableAs.push_back(setter);
1973+
}
1974+
}
1975+
}
1976+
break;
1977+
}
1978+
}
1979+
}
1980+
}
19431981

1982+
1983+
// ...or another accessor.
1984+
switch (storage->getWriteImpl()) {
1985+
case WriteImplKind::Immutable:
1986+
llvm_unreachable("synthesizing setter from immutable storage");
1987+
case WriteImplKind::Stored:
1988+
case WriteImplKind::StoredWithObservers:
1989+
case WriteImplKind::InheritedWithObservers:
1990+
case WriteImplKind::Set:
1991+
// Setter's availability shouldn't be externally influenced in these
1992+
// cases.
1993+
break;
1994+
1995+
case WriteImplKind::MutableAddress:
1996+
if (auto addr = storage->getOpaqueAccessor(AccessorKind::MutableAddress)) {
1997+
asAvailableAs.push_back(addr);
1998+
}
1999+
break;
2000+
case WriteImplKind::Modify:
2001+
if (auto mod = storage->getOpaqueAccessor(AccessorKind::Modify)) {
2002+
asAvailableAs.push_back(mod);
2003+
}
2004+
break;
2005+
}
2006+
2007+
if (!asAvailableAs.empty()) {
2008+
AvailabilityInference::applyInferredAvailableAttrs(
2009+
setter, asAvailableAs, ctx);
2010+
}
2011+
19442012
finishImplicitAccessor(setter, ctx);
19452013

19462014
return setter;
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -target x86_64-apple-macosx10.9 -typecheck -verify %s
3+
4+
// REQUIRES: OS=macosx
5+
6+
@propertyWrapper
7+
struct SetterConditionallyAvailable<T> {
8+
var wrappedValue: T {
9+
get { fatalError() }
10+
11+
@available(macOS 10.10, *)
12+
set { fatalError() }
13+
}
14+
15+
var projectedValue: T {
16+
get { fatalError() }
17+
18+
@available(macOS 10.10, *)
19+
set { fatalError() }
20+
}
21+
}
22+
23+
@propertyWrapper
24+
struct ModifyConditionallyAvailable<T> {
25+
var wrappedValue: T {
26+
get { fatalError() }
27+
28+
@available(macOS 10.10, *)
29+
_modify { fatalError() }
30+
}
31+
32+
var projectedValue: T {
33+
get { fatalError() }
34+
35+
@available(macOS 10.10, *)
36+
_modify { fatalError() }
37+
}
38+
}
39+
40+
struct Butt {
41+
var modify_conditionally_available: Int {
42+
get { fatalError() }
43+
44+
@available(macOS 10.10, *)
45+
_modify { fatalError() }
46+
}
47+
48+
@SetterConditionallyAvailable
49+
var wrapped_setter_conditionally_available: Int
50+
51+
@ModifyConditionallyAvailable
52+
var wrapped_modify_conditionally_available: Int
53+
}
54+
55+
func butt(x: inout Butt) { // expected-note*{{}}
56+
x.modify_conditionally_available = 0 // expected-error{{only available in macOS 10.10 or newer}} expected-note{{}}
57+
x.wrapped_setter_conditionally_available = 0 // expected-error{{only available in macOS 10.10 or newer}} expected-note{{}}
58+
x.wrapped_modify_conditionally_available = 0 // expected-error{{only available in macOS 10.10 or newer}} expected-note{{}}
59+
x.$wrapped_setter_conditionally_available = 0 // expected-error{{only available in macOS 10.10 or newer}} expected-note{{}}
60+
x.$wrapped_modify_conditionally_available = 0 // expected-error{{only available in macOS 10.10 or newer}} expected-note{{}}
61+
62+
if #available(macOS 10.10, *) {
63+
x.modify_conditionally_available = 0
64+
x.wrapped_setter_conditionally_available = 0
65+
x.wrapped_modify_conditionally_available = 0
66+
x.$wrapped_setter_conditionally_available = 0
67+
x.$wrapped_modify_conditionally_available = 0
68+
}
69+
}
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)