Skip to content

Commit 3f180c6

Browse files
committed
[rbi] When translating a Store, make sure to require src even if the value being stored is Sendable
This is important to ensure that we require the base of src if the src is non-Sendable (e.x.: a non-Sendable box). rdar://163322459 #85107
1 parent 90c0509 commit 3f180c6

File tree

2 files changed

+42
-2
lines changed

2 files changed

+42
-2
lines changed

lib/SILOptimizer/Analysis/RegionAnalysis.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2993,7 +2993,7 @@ class PartitionOpTranslator {
29932993
/// dest to src. If the \p dest could be aliased, then we must instead treat
29942994
/// them as merges, to ensure any aliases of \p dest are also updated.
29952995
void translateSILStore(Operand *dest, Operand *src,
2996-
SILIsolationInfo resultIsolationInfoOverride = {}) {
2996+
SILIsolationInfo resultIsolationInfoOverride = {}) {
29972997
SILValue destValue = dest->get();
29982998

29992999
if (auto destResult = tryToTrackValue(destValue)) {
@@ -3021,7 +3021,14 @@ class PartitionOpTranslator {
30213021
resultIsolationInfoOverride);
30223022
}
30233023

3024-
// Stores to storage of non-Sendable type can be ignored.
3024+
// If we reached this point, our destination is something that is Sendable
3025+
// and is not an address that comes from a non-Sendable base... so we can
3026+
// ignore the store part. But we still need tosee if our src (which also
3027+
// must be Sendable) comes from a non-Sendable base. In such a case, we need
3028+
// to require that.
3029+
if (auto srcResult = tryToTrackValue(src->get())) {
3030+
builder.addRequire(*srcResult);
3031+
}
30253032
}
30263033

30273034
void translateSILTupleAddrConstructor(TupleAddrConstructorInst *inst) {

test/Concurrency/transfernonsendable.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2001,3 +2001,36 @@ nonisolated(nonsending) func testCallNonisolatedNonsending(_ x: NonSendableKlass
20012001
// expected-ni-note @-1 {{sending nonisolated(nonsending) task-isolated 'x' to nonisolated global function 'useValueAsyncConcurrent' risks causing data races between nonisolated and nonisolated(nonsending) task-isolated uses}}
20022002
// expected-ni-ns-note @-2 {{sending task-isolated 'x' to @concurrent global function 'useValueAsyncConcurrent' risks causing data races between @concurrent and task-isolated uses}}
20032003
}
2004+
2005+
enum RequireSrcWhenStoringEvenWhenSendable {
2006+
func test<T: Sendable>(t: T) {
2007+
var result: T = t
2008+
Task { // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
2009+
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to initializer 'init(name:priority:operation:)' risks causing races in between local and caller code}}
2010+
result = t
2011+
}
2012+
useValue(result) // expected-note {{access can happen concurrently}}
2013+
}
2014+
2015+
func test2() {
2016+
var result: Any = 0
2017+
Task { // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
2018+
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to initializer 'init(name:priority:operation:)' risks causing races in between local and caller code}}
2019+
result = 0
2020+
}
2021+
useValue(result) // expected-note {{access can happen concurrently}}
2022+
}
2023+
2024+
protocol Initializable {
2025+
init()
2026+
}
2027+
2028+
func test3<T: Initializable & SendableMetatype>(type: T.Type) {
2029+
var result = type.init()
2030+
Task { // expected-warning {{sending value of non-Sendable type '() async -> ()' risks causing data races}}
2031+
// expected-note @-1 {{Passing value of non-Sendable type '() async -> ()' as a 'sending' argument to initializer 'init(name:priority:operation:)' risks causing races in between local and caller code}}
2032+
result = type.init()
2033+
}
2034+
useValue(result) // expected-note {{access can happen concurrently}}
2035+
}
2036+
}

0 commit comments

Comments
 (0)