Skip to content

Commit 80d19ff

Browse files
committed
Fix LifetimeDependenceDiagnostics: allow inout assignment to Void.
Bypess lifetime dependence diagnostics completely for immortal values. We did not do this initially because we wanted to potentially consider a value with a missing dependency to mean that it could not escape the current function. But now we use `Void` as a stand-in for immortal values. This is needed for reassigning a Span/MutableSpan to an empty, immortal Span: func inoutToImmortal(_ s: inout RawSpan) { let tmp = RawSpan(_unsafeBytes: UnsafeRawBufferPointer(start: nil, count: 0)) s = _overrideLifetime(tmp, borrowing: ()) } Fixes rdar://152572002 ([GH:#81976] Cannot reinitialize inout parameter of type `MutableSpan<T>?`) (cherry picked from commit ac94d7d)
1 parent ee5f4df commit 80d19ff

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceDiagnostics.swift

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,20 @@ private func analyze(dependence: LifetimeDependence, _ context: FunctionPassCont
110110
}
111111
}
112112

113+
// Check for immortal dependence.
114+
switch dependence.scope {
115+
case .global:
116+
log("Immortal global dependence.")
117+
return true
118+
case let .unknown(value):
119+
if value.type.isVoid {
120+
log("Immortal void dependence.")
121+
return true
122+
}
123+
default:
124+
break
125+
}
126+
113127
// Compute this dependence scope.
114128
var range = dependence.computeRange(context)
115129
defer { range?.deinitialize() }
@@ -200,6 +214,9 @@ private struct DiagnoseDependence {
200214
return .continueWalk
201215
}
202216
// Check for immortal lifetime.
217+
//
218+
// FIXME: remove this immortal check. It should be redundant with the earlier check that bypasses dependence
219+
// diagnostics.
203220
switch dependence.scope {
204221
case .global:
205222
return .continueWalk

test/SILOptimizer/lifetime_dependence/verify_diagnostics.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,3 +274,13 @@ func testSwitchAddr<T>(holder: inout Holder, t: T) {
274274
mutate(&holder) // expected-note {{conflicting access is here}}
275275
mutableView.modify()
276276
}
277+
278+
// =============================================================================
279+
// inout
280+
// =============================================================================
281+
282+
@available(Span 0.1, *)
283+
func inoutToImmortal(_ s: inout RawSpan) {
284+
let tmp = RawSpan(_unsafeBytes: UnsafeRawBufferPointer(start: nil, count: 0))
285+
s = _overrideLifetime(tmp, borrowing: ())
286+
}

0 commit comments

Comments
 (0)