Skip to content

Commit 359ae52

Browse files
committed
[region-isolation] Move from old style to new style the typed error for passing never sendable types as a sending parameter.
1 parent 46b2224 commit 359ae52

File tree

3 files changed

+37
-18
lines changed

3 files changed

+37
-18
lines changed

include/swift/AST/DiagnosticsSIL.def

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -951,9 +951,6 @@ ERROR(regionbasedisolation_unknown_pattern, none,
951951
ERROR(regionbasedisolation_arg_transferred, none,
952952
"sending %0 value of type %1 with later accesses to %2 context risks causing data races",
953953
(StringRef, Type, ActorIsolation))
954-
ERROR(regionbasedisolation_arg_passed_to_strongly_transferred_param, none,
955-
"%0 value of type %1 passed as a strongly transferred parameter; later accesses could race",
956-
(StringRef, Type))
957954

958955
//===---
959956
// New Transfer Non Sendable Diagnostics
@@ -1029,6 +1026,12 @@ NOTE(regionbasedisolation_named_value_used_after_explicit_sending, none,
10291026
NOTE(regionbasedisolation_named_isolated_closure_yields_race, none,
10301027
"%0%1 is captured by a %2 closure. %2 uses in closure may race against later %3 uses",
10311028
(StringRef, Identifier, ActorIsolation, ActorIsolation))
1029+
NOTE(regionbasedisolation_typed_tns_passed_to_sending, none,
1030+
"Passing %0 value of non-Sendable type %1 as a 'sending' parameter risks causing races inbetween %0 uses and uses reachable from the callee",
1031+
(StringRef, Type))
1032+
NOTE(regionbasedisolation_typed_tns_passed_to_sending_callee, none,
1033+
"Passing %0 value of non-Sendable type %1 as a 'sending' parameter to %2 %3 risks causing races inbetween %0 uses and uses reachable from %2",
1034+
(StringRef, Type, DescriptiveDeclKind, DeclName))
10321035

10331036
NOTE(regionbasedisolation_named_transfer_nt_asynclet_capture, none,
10341037
"sending %1 %0 into async let risks causing data races between nonisolated and %1 uses",

lib/SILOptimizer/Mandatory/TransferNonSendable.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1346,18 +1346,28 @@ class TransferNonTransferrableDiagnosticEmitter {
13461346
.highlight(loc.getSourceRange());
13471347
}
13481348

1349-
void emitFunctionArgumentApplyStronglyTransferred(SILLocation loc,
1350-
Type type) {
1349+
void emitTypedSendingNeverSendableToSendingParam(SILLocation loc,
1350+
Type inferredType) {
1351+
diagnoseError(loc, diag::regionbasedisolation_type_transfer_yields_race,
1352+
inferredType)
1353+
.highlight(loc.getSourceRange())
1354+
.limitBehaviorIf(getBehaviorLimit());
1355+
13511356
SmallString<64> descriptiveKindStr;
13521357
{
13531358
llvm::raw_svector_ostream os(descriptiveKindStr);
13541359
getIsolationRegionInfo().printForDiagnostics(os);
13551360
}
1356-
auto diag =
1357-
diag::regionbasedisolation_arg_passed_to_strongly_transferred_param;
1358-
diagnoseError(loc, diag, descriptiveKindStr, type)
1359-
.highlight(getOperand()->getUser()->getLoc().getSourceRange())
1360-
.limitBehaviorIf(getBehaviorLimit());
1361+
1362+
if (auto calleeInfo = getTransferringCalleeInfo()) {
1363+
diagnoseNote(
1364+
loc, diag::regionbasedisolation_typed_tns_passed_to_sending_callee,
1365+
descriptiveKindStr, inferredType, calleeInfo->first,
1366+
calleeInfo->second);
1367+
} else {
1368+
diagnoseNote(loc, diag::regionbasedisolation_typed_tns_passed_to_sending,
1369+
descriptiveKindStr, inferredType);
1370+
}
13611371
}
13621372

13631373
void emitNamedOnlyError(SILLocation loc, Identifier name) {
@@ -1415,8 +1425,8 @@ class TransferNonTransferrableDiagnosticEmitter {
14151425
}
14161426
}
14171427

1418-
void emitNamedFunctionArgumentApplyStronglyTransferred(SILLocation loc,
1419-
Identifier varName) {
1428+
void emitNamedSendingNeverSendableToSendingParam(SILLocation loc,
1429+
Identifier varName) {
14201430
emitNamedOnlyError(loc, varName);
14211431
SmallString<64> descriptiveKindStr;
14221432
{
@@ -1631,7 +1641,7 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
16311641
// See if we can infer a name from the value.
16321642
SmallString<64> resultingName;
16331643
if (auto varName = inferNameHelper(op->get())) {
1634-
diagnosticEmitter.emitNamedFunctionArgumentApplyStronglyTransferred(
1644+
diagnosticEmitter.emitNamedSendingNeverSendableToSendingParam(
16351645
loc, *varName);
16361646
return true;
16371647
}
@@ -1641,8 +1651,8 @@ bool TransferNonTransferrableDiagnosticInferrer::run() {
16411651
inferArgumentExprFromApplyExpr(sourceApply, fas, op)) {
16421652
type = inferredArgExpr->findOriginalType();
16431653
}
1644-
diagnosticEmitter.emitFunctionArgumentApplyStronglyTransferred(loc,
1645-
type);
1654+
diagnosticEmitter.emitTypedSendingNeverSendableToSendingParam(loc,
1655+
type);
16461656
return true;
16471657
}
16481658
}

test/Concurrency/transfernonsendable_typed_errors.swift

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ class NonSendableKlass {}
2020
actor MyActor {}
2121

2222
@MainActor func transferToMain<T>(_ t: T) async {}
23+
func transferToSendingParam<T>(_ x: sending T) {}
2324

2425
/////////////////
2526
// MARK: Tests //
@@ -42,10 +43,9 @@ func isolatedClosureTest() async {
4243
}
4344

4445
func sendingError() async {
45-
func test(_ x: sending NonSendableKlass) {}
4646
let x = NonSendableKlass()
47-
test(x) // expected-error {{sending value of non-Sendable type 'NonSendableKlass' risks causing data races}}
48-
// expected-note @-1 {{Passing value of non-Sendable type 'NonSendableKlass' as a 'sending' argument to local function 'test' risks causing races in between local and caller code}}
47+
transferToSendingParam(x) // expected-error {{sending value of non-Sendable type 'NonSendableKlass' risks causing data races}}
48+
// expected-note @-1 {{Passing value of non-Sendable type 'NonSendableKlass' as a 'sending' argument to global function 'transferToSendingParam' risks causing races in between local and callee code}}
4949
print(x) // expected-note {{access can happen concurrently}}
5050
}
5151

@@ -71,3 +71,9 @@ extension MyActor {
7171
}
7272
}
7373
}
74+
75+
@MainActor
76+
func sendingTransferNonSendableError(_ x: NonSendableKlass) {
77+
transferToSendingParam(x) // expected-error {{sending value of non-Sendable type 'NonSendableKlass' risks causing data races}}
78+
// expected-note @-1 {{Passing main actor-isolated value of non-Sendable type 'NonSendableKlass' as a 'sending' parameter to global function 'transferToSendingParam' risks causing races inbetween main actor-isolated uses and uses reachable from 'transferToSendingParam'}}
79+
}

0 commit comments

Comments
 (0)