-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[Concurrency] Prevent misuse of isolated
keyword to avoid crashes
#80390
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4943,6 +4943,18 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true, | |
// If the declaration is explicitly marked 'isolated', infer actor isolation | ||
// from the context. Currently applies only to DestructorDecl | ||
if (isolatedAttr) { | ||
if (auto varDecl = dyn_cast<VarDecl>(decl)) { | ||
ASTContext &ctx = varDecl->getASTContext(); | ||
ctx.Diags.diagnose(isolatedAttr->getLocation(), | ||
diag::isolated_misuse_in_decl); | ||
return std::nullopt; | ||
} | ||
if (auto nominalTypeDecl = dyn_cast<NominalTypeDecl>(decl)) { | ||
ASTContext &ctx = nominalTypeDecl->getASTContext(); | ||
ctx.Diags.diagnose(isolatedAttr->getLocation(), | ||
diag::isolated_misuse_in_decl); | ||
return std::nullopt; | ||
} | ||
assert(isa<DestructorDecl>(decl)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think there's a reason why this is not 💭 I guess the cause of #80363 is that
I want to hear opinions from the author here. @ktoso There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, the first time when I too saw the backtrace it wasn't quite it to just add the |
||
|
||
auto dc = decl->getDeclContext(); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -swift-version 6 -strict-concurrency=complete %s -emit-sil -o /dev/null -verify -DALLOW_TYPECHECKER_ERRORS -verify-additional-prefix typechecker- | ||
|
||
// RUN: %target-swift-frontend -target %target-swift-5.1-abi-triple -swift-version 6 -strict-concurrency=complete %s -emit-sil -o /dev/null -verify -verify-additional-prefix tns- | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Let me leave some questions here:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was debugging and left this out accidentally. |
||
// REQUIRES: asserts | ||
// REQUIRES: concurrency | ||
// REQUIRES: swift_swift_parser | ||
|
||
isolated struct S {} // expected-error {{invalid use of isolated in this context}} | ||
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}} | ||
|
||
isolated class C {} // expected-error {{'isolated' may only be used on 'deinit' declarations}} | ||
|
||
func f() { | ||
isolated let c = C() // expected-error {{invalid use of isolated in this context}} | ||
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}} | ||
// expected-warning@-2 {{initialization of immutable value 'c' was never used; consider replacing with assignment to '_' or removing it}} | ||
} | ||
|
||
actor A { | ||
isolated var state: Int? // expected-error {{invalid use of isolated in this context}} | ||
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn'tCan't we useisolated
be single-quoted?Or...
attr_only_one_decl_kind
instead of new diagnosis?Edit: We might not need new diagnosis if we can trap invalid
isolated
declarations in a different way. Please see below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, that could be done.