Skip to content

Commit ff3e421

Browse files
committed
[region-isolation] Add a SILIsolationInfo::isNonSendableType API that takes CanTypes and use it to avoid needing to lower certain CanTypes before performing the query.
rdar://138667211
1 parent c42989a commit ff3e421

File tree

4 files changed

+38
-16
lines changed

4 files changed

+38
-16
lines changed

include/swift/SILOptimizer/Utils/SILIsolationInfo.h

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -434,17 +434,21 @@ class SILIsolationInfo {
434434
return {};
435435
}
436436

437+
static bool isNonSendableType(SILType type, SILFunction *fn) {
438+
return isNonSendableType(type.getASTType(), fn);
439+
}
440+
441+
static bool isNonSendableType(SILValue value) {
442+
return isNonSendableType(value->getType(), value->getFunction());
443+
}
444+
437445
/// A helper that is used to ensure that we treat certain builtin values as
438446
/// non-Sendable that the AST level otherwise thinks are non-Sendable.
439447
///
440448
/// E.x.: Builtin.RawPointer and Builtin.NativeObject
441449
///
442450
/// TODO: Fix the type checker.
443-
static bool isNonSendableType(SILType type, SILFunction *fn);
444-
445-
static bool isNonSendableType(SILValue value) {
446-
return isNonSendableType(value->getType(), value->getFunction());
447-
}
451+
static bool isNonSendableType(CanType type, SILFunction *fn);
448452

449453
bool hasSameIsolation(ActorIsolation actorIsolation) const;
450454

lib/SILOptimizer/Mandatory/SendNonSendable.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1668,12 +1668,11 @@ bool SentNeverSendableDiagnosticInferrer::initForSendingPartialApply(
16681668
auto *decl = capture.getDecl();
16691669
auto type = decl->getInterfaceType()->getCanonicalType();
16701670
type = pai->getFunction()->mapTypeIntoContext(type)->getCanonicalType();
1671-
auto silType = SILType::getPrimitiveObjectType(type);
1672-
if (!SILIsolationInfo::isNonSendableType(silType, pai->getFunction()))
1671+
if (!SILIsolationInfo::isNonSendableType(type, pai->getFunction()))
16731672
continue;
16741673

16751674
auto *fromDC = decl->getInnermostDeclContext();
1676-
auto *nom = silType.getNominalOrBoundGenericNominal();
1675+
auto *nom = type.getNominalOrBoundGenericNominal();
16771676
if (nom && fromDC) {
16781677
if (auto diagnosticBehavior =
16791678
getConcurrencyDiagnosticBehaviorLimit(nom, fromDC)) {

lib/SILOptimizer/Utils/SILIsolationInfo.cpp

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,18 +1215,18 @@ void SILIsolationInfo::printForOneLineLogging(llvm::raw_ostream &os) const {
12151215
//
12161216
// NOTE: We special case RawPointer and NativeObject to ensure they are
12171217
// treated as non-Sendable and strict checking is applied to it.
1218-
bool SILIsolationInfo::isNonSendableType(SILType type, SILFunction *fn) {
1218+
bool SILIsolationInfo::isNonSendableType(CanType type, SILFunction *fn) {
12191219
// Treat Builtin.NativeObject, Builtin.RawPointer, and Builtin.BridgeObject as
12201220
// non-Sendable.
1221-
if (type.getASTType()->is<BuiltinNativeObjectType>() ||
1222-
type.getASTType()->is<BuiltinRawPointerType>() ||
1223-
type.getASTType()->is<BuiltinBridgeObjectType>()) {
1221+
if (type->is<BuiltinNativeObjectType>() ||
1222+
type->is<BuiltinRawPointerType>() ||
1223+
type->is<BuiltinBridgeObjectType>()) {
12241224
return true;
12251225
}
12261226

12271227
// Treat Builtin.SILToken as Sendable. It cannot escape from the current
12281228
// function. We should change isSendable to hardwire this.
1229-
if (type.getASTType()->is<SILTokenType>()) {
1229+
if (type->is<SILTokenType>()) {
12301230
return false;
12311231
}
12321232

@@ -1237,12 +1237,17 @@ bool SILIsolationInfo::isNonSendableType(SILType type, SILFunction *fn) {
12371237
// getConcurrencyDiagnosticBehavior could cause us to prevent a
12381238
// "preconcurrency" unneeded diagnostic when just using Sendable values. We
12391239
// only want to trigger that if we analyze a non-Sendable type.
1240-
if (type.isSendable(fn))
1240+
if (type->isSendableType())
12411241
return false;
12421242

12431243
// Grab out behavior. If it is none, then we have a type that we want to treat
12441244
// as non-Sendable.
1245-
auto behavior = type.getConcurrencyDiagnosticBehavior(fn);
1245+
auto declRef = fn->getDeclRef();
1246+
if (!declRef)
1247+
return {};
1248+
1249+
auto *fromDC = declRef.getInnermostDeclContext();
1250+
auto behavior = type->getConcurrencyDiagnosticBehaviorLimit(fromDC);
12461251
if (!behavior)
12471252
return true;
12481253

test/Concurrency/transfernonsendable.swift

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1907,7 +1907,7 @@ func offByOneWithImplicitPartialApply() {
19071907
}
19081908
}
19091909

1910-
protocol Progress {
1910+
protocol Progress { // expected-note {{}}
19111911
associatedtype AssocType
19121912
var x: AssocType { get }
19131913
func checkCancellation() throws
@@ -1923,3 +1923,17 @@ func testCaptureDiagnosticMapsTypeIntoContext<T : Progress>(_ x: NonSendableKlas
19231923
}
19241924
}
19251925
}
1926+
1927+
// We used to crash here since we were using an API that wanted a SIL Type and
1928+
// we were creating a SILType from a CanType that needed to be lowered.
1929+
func testUseCanTypeNonSendableCheckAPI(y: any Progress) async {
1930+
@Sendable func test() {
1931+
print(y) // expected-warning {{capture of 'y' with non-sendable type 'any Progress' in a `@Sendable` local function}}
1932+
}
1933+
await withTaskGroup(of: Void.self) { group in
1934+
group.addTask { // expected-tns-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
1935+
// expected-tns-note @-1 {{Passing task-isolated value of non-Sendable type '() async -> ()' as a 'sending' parameter to instance method 'addTask(priority:operation:)' risks causing races inbetween task-isolated uses and uses reachable from 'addTask(priority:operation:)'}}
1936+
test()
1937+
}
1938+
}
1939+
}

0 commit comments

Comments
 (0)