Skip to content

[Property Wrappers] Fix availability inference for enclosing self property wrappers. #36934

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1930,11 +1930,17 @@ static void addPropertyWrapperAccessorAvailability(VarDecl *var, AccessorKind ac
SmallVectorImpl<const Decl *> &asAvailableAs) {
AccessorDecl *synthesizedFrom = nullptr;
if (var->hasAttachedPropertyWrapper()) {
AbstractStorageDecl *wrappedValueImpl;
if (auto access = getEnclosingSelfPropertyWrapperAccess(var, /*forProjected=*/false)) {
wrappedValueImpl = access->subscript;
} else {
wrappedValueImpl = var->getAttachedPropertyWrapperTypeInfo(0).valueVar;
}

// The property wrapper info may not actually link back to a wrapper
// implementation, if there was a semantic error checking the wrapper.
auto info = var->getAttachedPropertyWrapperTypeInfo(0);
if (info.valueVar) {
synthesizedFrom = info.valueVar->getOpaqueAccessor(accessorKind);
if (wrappedValueImpl) {
synthesizedFrom = wrappedValueImpl->getOpaqueAccessor(accessorKind);
}
} else if (auto wrapperSynthesizedKind
= var->getPropertyWrapperSynthesizedPropertyKind()) {
Expand All @@ -1944,11 +1950,17 @@ static void addPropertyWrapperAccessorAvailability(VarDecl *var, AccessorKind ac

case PropertyWrapperSynthesizedPropertyKind::Projection: {
if (auto origVar = var->getOriginalWrappedProperty(wrapperSynthesizedKind)) {
AbstractStorageDecl *projectedValueImpl;
if (auto access = getEnclosingSelfPropertyWrapperAccess(origVar, /*forProjected=*/true)) {
projectedValueImpl = access->subscript;
} else {
projectedValueImpl = origVar->getAttachedPropertyWrapperTypeInfo(0).projectedValueVar;
}

// The property wrapper info may not actually link back to a wrapper
// implementation, if there was a semantic error checking the wrapper.
auto info = origVar->getAttachedPropertyWrapperTypeInfo(0);
if (info.projectedValueVar) {
synthesizedFrom = info.projectedValueVar->getOpaqueAccessor(accessorKind);
if (projectedValueImpl) {
synthesizedFrom = projectedValueImpl->getOpaqueAccessor(accessorKind);
}
}
break;
Expand Down
39 changes: 39 additions & 0 deletions test/Sema/generalized_accessors_availability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -110,3 +110,42 @@ func testInferredAvailability(x: inout LessAvailable) { // expected-error {{'Les
x.nested.$wrapped_modify_more_available = 0
}
}

@propertyWrapper
struct Observable<Value> {
private var stored: Value

init(wrappedValue: Value) { self.stored = wrappedValue }

@available(*, unavailable)
var wrappedValue: Value {
get { fatalError() }
set { fatalError() }
}

static subscript<EnclosingSelf>(
_enclosingInstance observed: EnclosingSelf,
wrapped wrappedKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Value>,
storage storageKeyPath: ReferenceWritableKeyPath<EnclosingSelf, Self>
) -> Value {
get { observed[keyPath: storageKeyPath].stored }
set { observed[keyPath: storageKeyPath].stored = newValue }
}
}

protocol P {}

class Base<T: P> {
@Observable var item: T?
}

struct Item: P {}

class Subclass: Base<Item> {
// Make sure this override isn't incorrectly diagnosed as
// unavailable. Wrapper availability inference should infer
// availability from the static subscript in this case.
override var item: Item? {
didSet {}
}
}