Skip to content

Commit fc85f98

Browse files
committed
Respect @preconcurrency in check of nonisoalted let variables
1 parent 290ec79 commit fc85f98

File tree

4 files changed

+22
-9
lines changed

4 files changed

+22
-9
lines changed

lib/Sema/TypeCheckAttr.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6968,13 +6968,16 @@ void AttributeChecker::visitNonisolatedAttr(NonisolatedAttr *attr) {
69686968

69696969
// 'nonisolated' without '(unsafe)' is not allowed on non-Sendable variables.
69706970
auto type = var->getTypeInContext();
6971-
if (!attr->isUnsafe() && !type->hasError() &&
6972-
!type->isSendableType()) {
6973-
Ctx.Diags.diagnose(attr->getLocation(),
6974-
diag::nonisolated_non_sendable,
6975-
type)
6976-
.warnUntilSwiftVersion(6);
6977-
return;
6971+
if (!attr->isUnsafe() && !type->hasError()) {
6972+
bool diagnosed = diagnoseIfAnyNonSendableTypes(
6973+
type,
6974+
SendableCheckContext(dc),
6975+
Type(),
6976+
SourceLoc(),
6977+
attr->getLocation(),
6978+
diag::nonisolated_non_sendable);
6979+
if (diagnosed)
6980+
return;
69786981
}
69796982

69806983
if (auto nominal = dyn_cast<NominalTypeDecl>(dc)) {

test/Concurrency/concurrent_value_checking.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ func testUnsafeSendableInAsync() async {
215215
// ----------------------------------------------------------------------
216216
// Sendable restriction on key paths.
217217
// ----------------------------------------------------------------------
218-
class NC: Hashable { // expected-note 2{{class 'NC' does not conform to the 'Sendable' protocol}}
218+
class NC: Hashable { // expected-note 3{{class 'NC' does not conform to the 'Sendable' protocol}}
219219
func hash(into: inout Hasher) { }
220220
static func==(_: NC, _: NC) -> Bool { true }
221221
}

test/Concurrency/global_variables.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ final class TestSendable: Sendable {
2323
init() {}
2424
}
2525

26-
final class TestNonsendable { // expected-note 2{{class 'TestNonsendable' does not conform to the 'Sendable' protocol}}
26+
final class TestNonsendable { // expected-note 3{{class 'TestNonsendable' does not conform to the 'Sendable' protocol}}
2727
init() {}
2828
}
2929

test/Concurrency/predates_concurrency_import.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,13 @@ extension NonStrictClass {
4141
extension StrictStruct {
4242
@Sendable func f() { } // expected-warning{{instance method of non-Sendable type 'StrictStruct' cannot be marked as '@Sendable'}}
4343
}
44+
45+
46+
struct HasStatics {
47+
nonisolated static let ns: NonStrictClass = NonStrictClass()
48+
49+
nonisolated static let ss: StrictStruct = StrictStruct()
50+
// expected-warning@-1{{'nonisolated' can not be applied to variable with non-'Sendable' type 'StrictStruct'}}
51+
// expected-warning@-2{{static property 'ss' is not concurrency-safe because non-'Sendable' type 'StrictStruct' may have shared mutable state}}
52+
// expected-note@-3{{isolate 'ss' to a global actor, or conform 'StrictStruct' to 'Sendable'}}
53+
}

0 commit comments

Comments
 (0)