Skip to content

Commit fbae21f

Browse files
committed
LifetimeDependence: handle dependent values in throwing calls.
Fixes rdar://125564278 (~Escapable: crash in LifetimeDependenceInsertion)
1 parent 89063c9 commit fbae21f

File tree

3 files changed

+19
-5
lines changed

3 files changed

+19
-5
lines changed

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/LifetimeDependenceInsertion.swift

+2-4
Original file line numberDiff line numberDiff line change
@@ -105,15 +105,13 @@ extension LifetimeDependentApply {
105105
/// dependent value within each scope.
106106
private func insertDependencies(for apply: LifetimeDependentApply,
107107
_ context: FunctionPassContext ) {
108-
precondition(apply.applySite.results.count > 0,
109-
"a lifetime-dependent instruction must have at least one result")
110-
111108
let bases = findDependenceBases(of: apply, context)
112-
let builder = Builder(after: apply.applySite, context)
113109
for dependentValue in apply.applySite.resultOrYields {
110+
let builder = Builder(before: dependentValue.nextInstruction, context)
114111
insertMarkDependencies(value: dependentValue, initializer: nil,
115112
bases: bases, builder: builder, context)
116113
}
114+
let builder = Builder(after: apply.applySite, context)
117115
for resultOper in apply.applySite.indirectResultOperands {
118116
let accessBase = resultOper.value.accessBase
119117
guard let (initialAddress, initializingStore) =

SwiftCompilerSources/Sources/Optimizer/Utilities/LifetimeDependenceUtils.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ extension LifetimeDependence {
222222
if value.isEscapable {
223223
return nil
224224
}
225-
if (value.definingInstruction as! FullApplySite).hasResultDependence {
225+
if (value.definingInstructionOrTerminator as! FullApplySite).hasResultDependence {
226226
return nil
227227
}
228228
assert(value.ownership == .owned, "apply result must be owned")

test/SILOptimizer/lifetime_dependence_diagnostics.swift

+16
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,19 @@ func bv_borrow_borrow(bv: borrowing BV) -> dependsOn(scoped bv) BV {
8080
func ncint_capture(ncInt: inout NCInt) {
8181
takeClosure { _ = ncInt.i }
8282
}
83+
84+
func neint_throws(ncInt: borrowing NCInt) throws -> NEInt {
85+
return NEInt(owner: ncInt)
86+
}
87+
88+
// CHECK-LABEL: sil hidden @$s4test9neint_try5ncIntAA5NEIntVAA5NCIntVYls_tKF : $@convention(thin) (@guaranteed NCInt) -> _scope(1) (@owned NEInt, @error any Error) {
89+
// CHECK: try_apply %{{.*}}(%0) : $@convention(thin) (@guaranteed NCInt) -> _scope(1) (@owned NEInt, @error any Error), normal bb1, error bb2
90+
// CHECK: bb1([[R:%.*]] : $NEInt):
91+
// CHECK: [[MD:%.*]] = mark_dependence [nonescaping] %5 : $NEInt on %0 : $NCInt
92+
// CHECK: return [[MD]] : $NEInt
93+
// CHECK: bb2([[E:%.*]] : $any Error):
94+
// CHECK: throw [[E]] : $any Error
95+
// CHECK-LABEL: } // end sil function '$s4test9neint_try5ncIntAA5NEIntVAA5NCIntVYls_tKF'
96+
func neint_try(ncInt: borrowing NCInt) throws -> NEInt {
97+
try neint_throws(ncInt: ncInt)
98+
}

0 commit comments

Comments
 (0)