Skip to content

[TypeChecker] Prevent invalid attribute on accessor from failing if i… #70163

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
Dec 1, 2023
Merged
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
41 changes: 25 additions & 16 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -439,29 +439,38 @@ GlobalActorAttributeRequest::evaluate(
.warnUntilSwiftVersion(6)
.fixItRemove(globalActorAttr->getRangeWithAt());

auto &ctx = decl->getASTContext();
auto *storage = accessor->getStorage();
// Let's suggest to move the attribute to the storage if
// this is an accessor/addressor of a property of subscript.
if (storage->getDeclContext()->isTypeContext()) {
// If enclosing declaration has a global actor,
// skip the suggestion.
if (storage->getGlobalActorAttr())
return llvm::None;

// Global actor attribute cannot be applied to
// an instance stored property of a struct.
if (auto *var = dyn_cast<VarDecl>(storage)) {
if (isStoredInstancePropertyOfStruct(var))
return llvm::None;
}
auto canMoveAttr = [&]() {
// If enclosing declaration has a global actor,
// skip the suggestion.
if (storage->getGlobalActorAttr())
return false;

// Global actor attribute cannot be applied to
// an instance stored property of a struct.
if (auto *var = dyn_cast<VarDecl>(storage)) {
return !isStoredInstancePropertyOfStruct(var);
}

return true;
};

decl->diagnose(diag::move_global_actor_attr_to_storage_decl, storage)
.fixItInsert(
storage->getAttributeInsertionLoc(/*forModifier=*/false),
llvm::Twine("@", result->second->getNameStr()).str());
if (canMoveAttr()) {
decl->diagnose(diag::move_global_actor_attr_to_storage_decl,
storage)
.fixItInsert(
storage->getAttributeInsertionLoc(/*forModifier=*/false),
llvm::Twine("@", result->second->getNameStr()).str());
}
}

return llvm::None;
// In Swift 6, once the diag above is an error, it is disallowed.
if (ctx.isSwiftVersionAtLeast(6))
return llvm::None;
}
}
// Functions are okay.
Expand Down