Skip to content

Commit d3f86b4

Browse files
authored
Merge pull request #82593 from xedin/rdar-148076903
[Concurrency] Fix a crash caused by misuse of `isolated` modifier
2 parents d203591 + 3580679 commit d3f86b4

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
@@ -5071,7 +5071,13 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
50715071
bool onlyExplicit = false) {
50725072
// Look up attributes on the declaration that can affect its actor isolation.
50735073
// If any of them are present, use that attribute.
5074-
auto isolatedAttr = decl->getAttrs().getAttribute<IsolatedAttr>();
5074+
5075+
// 'isolated` attribute in the declaration context currently applies
5076+
// only to `deinit` declarations, invalid uses are going to
5077+
// be diagnosed as part of attribute checking.
5078+
auto isolatedAttr = isa<DestructorDecl>(decl)
5079+
? decl->getAttrs().getAttribute<IsolatedAttr>()
5080+
: nullptr;
50755081
auto nonisolatedAttr = decl->getAttrs().getAttribute<NonisolatedAttr>();
50765082
auto globalActorAttr = decl->getGlobalActorAttr();
50775083
auto concurrentAttr = decl->getAttrs().getAttribute<ConcurrentAttr>();
@@ -5124,10 +5130,8 @@ getIsolationFromAttributes(const Decl *decl, bool shouldDiagnose = true,
51245130
}
51255131

51265132
// If the declaration is explicitly marked 'isolated', infer actor isolation
5127-
// from the context. Currently applies only to DestructorDecl
5133+
// from the context. Currently applies only to DestructorDecl.
51285134
if (isolatedAttr) {
5129-
assert(isa<DestructorDecl>(decl));
5130-
51315135
auto dc = decl->getDeclContext();
51325136
auto selfTypeDecl = dc->getSelfNominalTypeDecl();
51335137
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)