Skip to content

Commit a651d02

Browse files
committed
[Concurrency] Fix a crash caused by misuse of isolated modifier
Adjust isolation checking to handle misused `isolated` attribute and let attribute checker property diagnose it. Resolves: rdar://148076903 Resolves: #80363 (cherry picked from commit 3580679)
1 parent f299c6a commit a651d02

File tree

2 files changed

+55
-4
lines changed

2 files changed

+55
-4
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5064,7 +5064,13 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
50645064
bool onlyExplicit = false) {
50655065
// Look up attributes on the declaration that can affect its actor isolation.
50665066
// If any of them are present, use that attribute.
5067-
auto isolatedAttr = decl->getAttrs().getAttribute<IsolatedAttr>();
5067+
5068+
// 'isolated` attribute in the declaration context currently applies
5069+
// only to `deinit` declarations, invalid uses are going to
5070+
// be diagnosed as part of attribute checking.
5071+
auto isolatedAttr = isa<DestructorDecl>(decl)
5072+
? decl->getAttrs().getAttribute<IsolatedAttr>()
5073+
: nullptr;
50685074
auto nonisolatedAttr = decl->getAttrs().getAttribute<NonisolatedAttr>();
50695075
auto globalActorAttr = decl->getGlobalActorAttr();
50705076
auto concurrentAttr = decl->getAttrs().getAttribute<ConcurrentAttr>();
@@ -5117,10 +5123,8 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
51175123
}
51185124

51195125
// If the declaration is explicitly marked 'isolated', infer actor isolation
5120-
// from the context. Currently applies only to DestructorDecl
5126+
// from the context. Currently applies only to DestructorDecl.
51215127
if (isolatedAttr) {
5122-
assert(isa<DestructorDecl>(decl));
5123-
51245128
auto dc = decl->getDeclContext();
51255129
auto selfTypeDecl = dc->getSelfNominalTypeDecl();
51265130
std::optional<ActorIsolation> result;

test/Concurrency/issue-80363.swift

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// RUN: %target-typecheck-verify-swift
2+
3+
// https://github.com/swiftlang/swift/issues/80363
4+
5+
class C {}
6+
7+
func testLocal() {
8+
isolated let c = C()
9+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
10+
_ = c
11+
12+
isolated func test() {
13+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
14+
}
15+
}
16+
17+
isolated var x: Int = 42
18+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
19+
20+
isolated class Test {
21+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
22+
}
23+
24+
struct TestMembers {
25+
isolated var q: String {
26+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
27+
get {
28+
"ultimate question"
29+
}
30+
31+
isolated set {
32+
// expected-error@-1 {{expected 'get', 'set', 'willSet', or 'didSet' keyword to start an accessor definition}}
33+
}
34+
}
35+
36+
isolated let a: Int = 42
37+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
38+
39+
isolated subscript(x: Int) -> Bool {
40+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
41+
get { true }
42+
}
43+
44+
isolated func test() {
45+
// expected-error@-1 {{'isolated' may only be used on 'deinit' declarations}}
46+
}
47+
}

0 commit comments

Comments
 (0)