Skip to content

[Property wrappers] Diagnose unavailable wrappedValue #28857

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
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
16 changes: 15 additions & 1 deletion lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include "CodeSynthesis.h"
#include "TypeChecker.h"
#include "TypeCheckAvailability.h"
#include "TypeCheckDecl.h"
#include "TypeCheckType.h"
#include "swift/AST/ASTContext.h"
Expand Down Expand Up @@ -694,7 +695,20 @@ static Expr *buildStorageReference(AccessorDecl *accessor,
// Perform accesses to the wrappedValues along the composition chain.
for (unsigned i : range(firstWrapperIdx, lastWrapperIdx)) {
auto wrapperInfo = var->getAttachedPropertyWrapperTypeInfo(i);
underlyingVars.push_back(wrapperInfo.valueVar);
auto wrappedValue = wrapperInfo.valueVar;

// Check for availability of wrappedValue.
if (accessor->getAccessorKind() == AccessorKind::Get ||
accessor->getAccessorKind() == AccessorKind::Read) {
if (auto *attr = wrappedValue->getAttrs().getUnavailable(ctx)) {
diagnoseExplicitUnavailability(
wrappedValue,
var->getAttachedPropertyWrappers()[i]->getRangeWithAt(),
var->getDeclContext(), nullptr);
}
}

underlyingVars.push_back(wrappedValue);
}
semantics = AccessSemantics::DirectToStorage;
selfAccessKind = SelfAccessorKind::Peer;
Expand Down
39 changes: 37 additions & 2 deletions test/decl/var/property_wrappers.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ struct Wrapper<T> {
init(stored: T) {
self._stored = stored
}

var wrappedValue: T {
get { _stored }
set { _stored = newValue }
}

}

@propertyWrapper
Expand Down Expand Up @@ -837,6 +836,42 @@ struct UsesExplicitClosures {
var y: Int
}

// ---------------------------------------------------------------------------
// Enclosing instance diagnostics
// ---------------------------------------------------------------------------
@propertyWrapper
struct Observable<Value> {
private var stored: Value

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

@available(*, unavailable, message: "must be in a class")
var wrappedValue: Value { // expected-note{{'wrappedValue' has been explicitly marked unavailable here}}
get { fatalError("called wrappedValue getter") }
set { fatalError("called wrappedValue setter") }
}

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
}
}
}

struct MyObservedValueType {
@Observable // expected-error{{'wrappedValue' is unavailable: must be in a class}}
var observedProperty = 17
}

// ---------------------------------------------------------------------------
// Miscellaneous bugs
// ---------------------------------------------------------------------------
Expand Down