Skip to content

Commit 71fcc78

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 d03fd67 commit 71fcc78

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
@@ -287,3 +287,13 @@ func testSpanMayThrow(buffer: inout [Int]) {
287287
let bufferSpan = buffer.mutableSpan
288288
try! mutableSpanMayThrow(bufferSpan)
289289
}
290+
291+
// =============================================================================
292+
// inout
293+
// =============================================================================
294+
295+
@available(Span 0.1, *)
296+
func inoutToImmortal(_ s: inout RawSpan) {
297+
let tmp = RawSpan(_unsafeBytes: UnsafeRawBufferPointer(start: nil, count: 0))
298+
s = _overrideLifetime(tmp, borrowing: ())
299+
}

0 commit comments

Comments
 (0)