Skip to content

Teach diagnostic about non-Sendable global lets to respect @preconcurrency #73676

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -5614,9 +5614,9 @@ NOTE(shared_mutable_state_decl_note,none,
"isolate %0 to a global actor, or convert it to a 'let' constant and "
"conform it to 'Sendable'", (const ValueDecl *))
ERROR(shared_immutable_state_decl,none,
"%kind0 is not concurrency-safe because non-'Sendable' type %1 may have "
"%kind1 is not concurrency-safe because non-'Sendable' type %0 may have "
"shared mutable state",
(const ValueDecl *, Type))
(Type, const ValueDecl *))
NOTE(shared_immutable_state_decl_note,none,
"isolate %0 to a global actor, or conform %1 to 'Sendable'",
(const ValueDecl *, Type))
Expand Down
14 changes: 9 additions & 5 deletions lib/Sema/TypeCheckConcurrency.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5013,13 +5013,17 @@ ActorIsolation ActorIsolationRequest::evaluate(
}
if (var->isLet()) {
auto type = var->getInterfaceType();
if (!type->isSendableType()) {
diagVar->diagnose(diag::shared_immutable_state_decl,
diagVar, type)
.warnUntilSwiftVersion(6);
bool diagnosed = diagnoseIfAnyNonSendableTypes(
type, SendableCheckContext(var->getDeclContext()),
/*inDerivedConformance=*/Type(), /*typeLoc=*/SourceLoc(),
/*diagnoseLoc=*/var->getLoc(),
diag::shared_immutable_state_decl, diagVar);

// If we diagnosed this 'let' as non-Sendable, tack on a note
// to suggest a course of action.
if (diagnosed)
diagVar->diagnose(diag::shared_immutable_state_decl_note,
diagVar, type);
}
} else {
diagVar->diagnose(diag::shared_mutable_state_decl, diagVar)
.warnUntilSwiftVersion(6);
Expand Down
62 changes: 53 additions & 9 deletions lib/Sema/TypeCheckConcurrency.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,20 +436,64 @@ bool diagnoseNonSendableTypes(
Diag<Type, DiagArgs...> diag,
typename detail::Identity<DiagArgs>::type ...diagArgs) {

ASTContext &ctx = fromContext.fromDC->getASTContext();
return diagnoseNonSendableTypes(
type, fromContext, derivedConformance, typeLoc,
[&](Type specificType, DiagnosticBehavior behavior) {
auto preconcurrency =
fromContext.preconcurrencyBehavior(type->getAnyNominal());
ASTContext &ctx = fromContext.fromDC->getASTContext();
return diagnoseNonSendableTypes(
type, fromContext, derivedConformance, typeLoc,
[&](Type specificType, DiagnosticBehavior behavior) {
auto preconcurrency =
fromContext.preconcurrencyBehavior(type->getAnyNominal());

ctx.Diags.diagnose(diagnoseLoc, diag, type, diagArgs...)
.limitBehaviorUntilSwiftVersion(behavior, 6)
.limitBehaviorIf(preconcurrency);

return (behavior == DiagnosticBehavior::Ignore ||
preconcurrency == DiagnosticBehavior::Ignore);
});
}

/// Emit a diagnostic if there are any non-Sendable types for which
/// the Sendable diagnostic wasn't suppressed. This diagnostic will
/// only be emitted once, but there might be additional notes for the
/// various occurrences of Sendable types.
///
/// \param typeLoc is the source location of the type being diagnosed
///
/// \param diagnoseLoc is the source location at which the main diagnostic should
/// be reported, which can differ from typeLoc
///
/// \returns \c true if any diagnostics.
template<typename ...DiagArgs>
bool diagnoseIfAnyNonSendableTypes(
Type type, SendableCheckContext fromContext,
Type derivedConformance,
SourceLoc typeLoc, SourceLoc diagnoseLoc,
Diag<Type, DiagArgs...> diag,
typename detail::Identity<DiagArgs>::type ...diagArgs) {

ASTContext &ctx = fromContext.fromDC->getASTContext();
bool diagnosed = false;
diagnoseNonSendableTypes(
type, fromContext, derivedConformance, typeLoc,
[&](Type specificType, DiagnosticBehavior behavior) {
auto preconcurrency =
fromContext.preconcurrencyBehavior(type->getAnyNominal());

if (behavior == DiagnosticBehavior::Ignore ||
preconcurrency == DiagnosticBehavior::Ignore)
return true;

if (!diagnosed) {
ctx.Diags.diagnose(diagnoseLoc, diag, type, diagArgs...)
.limitBehaviorUntilSwiftVersion(behavior, 6)
.limitBehaviorIf(preconcurrency);
diagnosed = true;
}

return false;
});

return (behavior == DiagnosticBehavior::Ignore ||
preconcurrency == DiagnosticBehavior::Ignore);
});
return diagnosed;
}

/// Diagnose any non-Sendable types that occur within the given type, using
Expand Down
1 change: 1 addition & 0 deletions test/Concurrency/Inputs/NonStrictModule.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
public struct NonStrictStruct { }

open class NonStrictClass {
public init() {}
open func send(_ body: @Sendable () -> Void) {}
open func dontSend(_ body: () -> Void) {}
}
Expand Down
4 changes: 3 additions & 1 deletion test/Concurrency/Inputs/StrictModule.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
public struct StrictStruct: Hashable { }
public struct StrictStruct: Hashable {
public init() {}
}

open class StrictClass {
open func send(_ body: @Sendable () -> Void) {}
Expand Down
2 changes: 1 addition & 1 deletion test/Concurrency/concurrency_warnings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
// REQUIRES: concurrency
// REQUIRES: asserts

class GlobalCounter {
class GlobalCounter { // expected-note{{class 'GlobalCounter' does not conform to the 'Sendable' protocol}}
var counter: Int = 0
}

Expand Down
2 changes: 1 addition & 1 deletion test/Concurrency/global_variables.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ final class TestSendable: Sendable {
init() {}
}

final class TestNonsendable {
final class TestNonsendable { // expected-note 2{{class 'TestNonsendable' does not conform to the 'Sendable' protocol}}
init() {}
}

Expand Down
13 changes: 9 additions & 4 deletions test/Concurrency/predates_concurrency_import.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/NonStrictModule.swiftmodule -module-name NonStrictModule %S/Inputs/NonStrictModule.swift
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/OtherActors.swiftmodule -module-name OtherActors %S/Inputs/OtherActors.swift -disable-availability-checking

// RUN: %target-swift-frontend -I %t %s -emit-sil -o /dev/null -verify
// RUN: %target-swift-frontend -I %t %s -emit-sil -o /dev/null -verify -strict-concurrency=targeted -disable-region-based-isolation-with-strict-concurrency
// RUN: %target-swift-frontend -I %t %s -emit-sil -o /dev/null -verify -strict-concurrency=complete -disable-region-based-isolation-with-strict-concurrency
// RUN: %target-swift-frontend -I %t %s -emit-sil -o /dev/null -verify -strict-concurrency=complete
// RUN: %target-swift-frontend -I %t %s -emit-sil -o /dev/null -verify -parse-as-library -enable-upcoming-feature GlobalConcurrency
// RUN: %target-swift-frontend -I %t %s -emit-sil -o /dev/null -verify -strict-concurrency=targeted -disable-region-based-isolation-with-strict-concurrency -parse-as-library -enable-upcoming-feature GlobalConcurrency
// RUN: %target-swift-frontend -I %t %s -emit-sil -o /dev/null -verify -strict-concurrency=complete -disable-region-based-isolation-with-strict-concurrency -parse-as-library -enable-upcoming-feature GlobalConcurrency
// RUN: %target-swift-frontend -I %t %s -emit-sil -o /dev/null -verify -strict-concurrency=complete -parse-as-library -enable-upcoming-feature GlobalConcurrency

// REQUIRES: concurrency
// REQUIRES: asserts
Expand All @@ -28,3 +28,8 @@ func test(
acceptSendable(oma) // okay
acceptSendable(ssc) // okay
}

let nonStrictGlobal = NonStrictClass() // no warning

let strictGlobal = StrictStruct() // expected-warning{{let 'strictGlobal' is not concurrency-safe because non-'Sendable' type 'StrictStruct' may have shared mutable state}}
// expected-note@-1{{isolate 'strictGlobal' to a global actor, or conform 'StrictStruct' to 'Sendable'}}
8 changes: 6 additions & 2 deletions test/Concurrency/predates_concurrency_import_swift6.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/StrictModule.swiftmodule -module-name StrictModule -swift-version 6 %S/Inputs/StrictModule.swift
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/NonStrictModule.swiftmodule -module-name NonStrictModule %S/Inputs/NonStrictModule.swift

// RUN: %target-swift-frontend -swift-version 6 -I %t %s %s -emit-sil -o /dev/null -verify
// RUN: %target-swift-frontend -swift-version 6 -I %t %s %s -emit-sil -o /dev/null -verify -enable-upcoming-feature RegionBasedIsolation
// RUN: %target-swift-frontend -swift-version 6 -I %t %s %s -emit-sil -o /dev/null -verify -parse-as-library
// RUN: %target-swift-frontend -swift-version 6 -I %t %s %s -emit-sil -o /dev/null -verify -enable-upcoming-feature RegionBasedIsolation -parse-as-library

@preconcurrency import NonStrictModule
@preconcurrency import StrictModule
Expand All @@ -15,3 +15,7 @@ func test(ss: StrictStruct, ns: NonStrictClass) {
acceptSendable(ss) // expected-warning{{type 'StrictStruct' does not conform to the 'Sendable' protocol}}
acceptSendable(ns)
}

let nonStrictGlobal = NonStrictClass()
let strictGlobal = StrictStruct() // expected-warning{{let 'strictGlobal' is not concurrency-safe because non-'Sendable' type 'StrictStruct' may have shared mutable state}}
// expected-note@-1{{isolate 'strictGlobal' to a global actor, or conform 'StrictStruct' to 'Sendable'}}
2 changes: 1 addition & 1 deletion test/Concurrency/sendable_cycle.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
// REQUIRES: concurrency
// REQUIRES: asserts

struct Bar {
struct Bar { // expected-note*{{consider making struct 'Bar' conform to the 'Sendable' protocol}}
lazy var foo = { // expected-error {{escaping closure captures mutating 'self' parameter}}
self.x() // expected-note {{captured here}}
}
Expand Down