Skip to content

Commit 3580679

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
1 parent 0999792 commit 3580679

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed

lib/Sema/TypeCheckConcurrency.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5046,7 +5046,13 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
50465046
bool onlyExplicit = false) {
50475047
// Look up attributes on the declaration that can affect its actor isolation.
50485048
// If any of them are present, use that attribute.
5049-
auto isolatedAttr = decl->getAttrs().getAttribute<IsolatedAttr>();
5049+
5050+
// 'isolated` attribute in the declaration context currently applies
5051+
// only to `deinit` declarations, invalid uses are going to
5052+
// be diagnosed as part of attribute checking.
5053+
auto isolatedAttr = isa<DestructorDecl>(decl)
5054+
? decl->getAttrs().getAttribute<IsolatedAttr>()
5055+
: nullptr;
50505056
auto nonisolatedAttr = decl->getAttrs().getAttribute<NonisolatedAttr>();
50515057
auto globalActorAttr = decl->getGlobalActorAttr();
50525058
auto concurrentAttr = decl->getAttrs().getAttribute<ConcurrentAttr>();
@@ -5099,10 +5105,8 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
50995105
}
51005106

51015107
// If the declaration is explicitly marked 'isolated', infer actor isolation
5102-
// from the context. Currently applies only to DestructorDecl
5108+
// from the context. Currently applies only to DestructorDecl.
51035109
if (isolatedAttr) {
5104-
assert(isa<DestructorDecl>(decl));
5105-
51065110
auto dc = decl->getDeclContext();
51075111
auto selfTypeDecl = dc->getSelfNominalTypeDecl();
51085112
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+
}
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
// {"signature":"getIsolationFromAttributes(swift::Decl const*, bool, bool)"}
2-
// RUN: not --crash %target-swift-frontend -typecheck %s
2+
// RUN: not %target-swift-frontend -typecheck %s
33
isolated let a

0 commit comments

Comments
 (0)