Skip to content

[Sema] Fixes for mutability handling in property wrappers #76357

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 2 commits into from
Sep 10, 2024
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
31 changes: 18 additions & 13 deletions lib/Sema/TypeCheckStorage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2816,31 +2816,36 @@ static VarDecl *synthesizeLocalWrappedValueVar(VarDecl *var) {
}
Identifier name = ctx.getIdentifier(nameBuf);

VarDecl *localVar = new (ctx) VarDecl(/*IsStatic=*/false,
VarDecl::Introducer::Var,
var->getLoc(), name, dc);
localVar->setImplicit();
localVar->getAttrs() = var->getAttrs();
localVar->overwriteAccess(var->getFormalAccess());

std::optional<StorageImplInfo> mutability;
if (var->hasImplicitPropertyWrapper()) {
// FIXME: This can have a setter, but we need a resolved wrapper type
// to figure it out.
localVar->setImplInfo(StorageImplInfo::getImmutableComputed());
mutability.emplace(StorageImplInfo::getImmutableComputed());
} else {
auto mutability = *var->getPropertyWrapperMutability();
if (mutability.Getter == PropertyWrapperMutability::Mutating) {
auto possibleMutability = var->getPropertyWrapperMutability();
if (!possibleMutability)
return nullptr;

if (possibleMutability->Getter == PropertyWrapperMutability::Mutating) {
ctx.Diags.diagnose(var->getLoc(), diag::property_wrapper_param_mutating);
return nullptr;
}

if (mutability.Setter == PropertyWrapperMutability::Nonmutating) {
localVar->setImplInfo(StorageImplInfo::getMutableComputed());
if (possibleMutability->Setter == PropertyWrapperMutability::Nonmutating) {
mutability.emplace(StorageImplInfo::getMutableComputed());
} else {
localVar->setImplInfo(StorageImplInfo::getImmutableComputed());
mutability.emplace(StorageImplInfo::getImmutableComputed());
}
}

VarDecl *localVar = new (ctx) VarDecl(/*IsStatic=*/false,
VarDecl::Introducer::Var,
var->getLoc(), name, dc);
localVar->setImplicit();
localVar->getAttrs() = var->getAttrs();
localVar->overwriteAccess(var->getFormalAccess());
localVar->setImplInfo(*mutability);

return localVar;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// RUN: %target-typecheck-verify-swift -swift-version 5 -package-name myPkg

// https://github.com/swiftlang/swift/issues/65640

@propertyWrapper
struct Wrapper {
// expected-error@-1{{property wrapper type 'Wrapper' does not contain a non-static property named 'wrappedValue'}}
init(wrappedValue: Int) {}
}
func test(@Wrapper x: Int) {}