Skip to content

Commit 02eec64

Browse files
committed
[Concurrency] Prevent misuse of isolated keyword to avoid crashes
Resolves #80363
1 parent 7bf2994 commit 02eec64

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

include/swift/AST/DiagnosticsSema.def

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6035,6 +6035,9 @@ ERROR(isolated_deinit_on_value_type,none,
60356035
ERROR(isolated_deinit_unavailable,none,
60366036
"isolated deinit is only available in %0 %1 or newer",
60376037
(AvailabilityDomain, AvailabilityRange))
6038+
ERROR(isolated_misuse_in_decl,none,
6039+
"invalid use of isolated in this context",
6040+
())
60386041

60396042
//------------------------------------------------------------------------------
60406043
// MARK: String Processing

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4943,6 +4943,18 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
49434943
// If the declaration is explicitly marked 'isolated', infer actor isolation
49444944
// from the context. Currently applies only to DestructorDecl
49454945
if (isolatedAttr) {
4946+
if (auto varDecl = dyn_cast<VarDecl>(decl)) {
4947+
ASTContext &ctx = varDecl->getASTContext();
4948+
ctx.Diags.diagnose(isolatedAttr->getLocation(),
4949+
diag::isolated_misuse_in_decl);
4950+
return std::nullopt;
4951+
}
4952+
if (auto nominalTypeDecl = dyn_cast<NominalTypeDecl>(decl)) {
4953+
ASTContext &ctx = nominalTypeDecl->getASTContext();
4954+
ctx.Diags.diagnose(isolatedAttr->getLocation(),
4955+
diag::isolated_misuse_in_decl);
4956+
return std::nullopt;
4957+
}
49464958
assert(isa<DestructorDecl>(decl));
49474959

49484960
auto dc = decl->getDeclContext();
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// 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-
2+
3+
// 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-
4+
5+
// REQUIRES: asserts
6+
// REQUIRES: concurrency
7+
// REQUIRES: swift_swift_parser
8+
9+
isolated struct S {} // expected-error {{invalid use of isolated in this context}}
10+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
11+
12+
isolated class C {} // expected-error {{'isolated' may only be used on 'deinit' declarations}}
13+
14+
func f() {
15+
isolated let c = C() // expected-error {{invalid use of isolated in this context}}
16+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
17+
// expected-warning@-2 {{initialization of immutable value 'c' was never used; consider replacing with assignment to '_' or removing it}}
18+
}
19+
20+
actor A {
21+
isolated var state: Int? // expected-error {{invalid use of isolated in this context}}
22+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
23+
}

0 commit comments

Comments
 (0)