@@ -249,7 +249,7 @@ private struct ScopeExtension {
249249
250250private extension LifetimeDependence . Scope {
251251 /// The instruction that introduces an extendable scope. This returns a non-nil scope introducer for
252- /// Extendable .nestedScopes.
252+ /// ScopeExtension .nestedScopes.
253253 var extendableBegin : Instruction ? {
254254 switch self {
255255 case let . access( beginAccess) :
@@ -258,6 +258,17 @@ private extension LifetimeDependence.Scope {
258258 return beginBorrow. value. definingInstruction!
259259 case let . yield( yieldedValue) :
260260 return yieldedValue. definingInstruction!
261+ case let . initialized( initializer) :
262+ switch initializer {
263+ case let . store( initializingStore: store, initialAddress: _) :
264+ if let sb = store as? StoreBorrowInst {
265+ return sb
266+ }
267+ return nil
268+ case . argument, . yield:
269+ // TODO: extend indirectly yielded scopes.
270+ return nil
271+ }
261272 default :
262273 return nil
263274 }
@@ -277,29 +288,31 @@ private extension LifetimeDependence.Scope {
277288 let accessExtension = gatherAccessExtension ( beginAccess: beginAccess, innerScopes: & innerScopes)
278289 return SingleInlineArray ( element: accessExtension)
279290 case let . borrowed( beginBorrow) :
280- let borrowedValue = beginBorrow. baseOperand!. value
281- let enclosingScope = LifetimeDependence . Scope ( base: borrowedValue, context)
282- innerScopes. push ( self )
283- var innerBorrowScopes = innerScopes
284- innerBorrowScopes. push ( enclosingScope)
285- if let extensions = enclosingScope. gatherExtensions ( innerScopes: innerBorrowScopes, context) {
286- return extensions
287- }
288- // This is the outermost scope to be extended because gatherExtensions did not find an enclosing scope.
289- return SingleInlineArray ( element: getOuterExtension ( owner: enclosingScope. parentValue, nestedScopes: innerScopes,
290- context) )
291+ return gatherBorrowExtension ( borrowedValue: beginBorrow. baseOperand!. value, innerScopes: & innerScopes, context)
292+
291293 case let . yield( yieldedValue) :
292294 innerScopes. push ( self )
293295 var extensions = SingleInlineArray < ScopeExtension > ( )
294296 let applySite = yieldedValue. definingInstruction as! BeginApplyInst
295297 for operand in applySite. parameterOperands {
296- guard let dep = applySite. resultDependence ( on: operand) , dep == . scope else {
298+ guard let dep = applySite. resultDependence ( on: operand) , dep. isScoped else {
297299 continue
298300 }
299301 // Pass a copy of innerScopes without modifying this one.
300302 extensions. append ( contentsOf: gatherOperandExtension ( on: operand, innerScopes: innerScopes, context) )
301303 }
302304 return extensions
305+ case let . initialized( initializer) :
306+ switch initializer {
307+ case let . store( initializingStore: store, initialAddress: _) :
308+ if let sb = store as? StoreBorrowInst {
309+ return gatherBorrowExtension ( borrowedValue: sb. source, innerScopes: & innerScopes, context)
310+ }
311+ return nil
312+ case . argument, . yield:
313+ // TODO: extend indirectly yielded scopes.
314+ return nil
315+ }
303316 default :
304317 return nil
305318 }
@@ -360,6 +373,23 @@ private extension LifetimeDependence.Scope {
360373 }
361374 return ScopeExtension ( owner: outerBeginAccess, nestedScopes: innerScopes, dependsOnArg: nil )
362375 }
376+
377+ func gatherBorrowExtension( borrowedValue: Value ,
378+ innerScopes: inout SingleInlineArray < LifetimeDependence . Scope > ,
379+ _ context: FunctionPassContext )
380+ -> SingleInlineArray < ScopeExtension > {
381+
382+ let enclosingScope = LifetimeDependence . Scope ( base: borrowedValue, context)
383+ innerScopes. push ( self )
384+ var innerBorrowScopes = innerScopes
385+ innerBorrowScopes. push ( enclosingScope)
386+ if let extensions = enclosingScope. gatherExtensions ( innerScopes: innerBorrowScopes, context) {
387+ return extensions
388+ }
389+ // This is the outermost scope to be extended because gatherExtensions did not find an enclosing scope.
390+ return SingleInlineArray ( element: getOuterExtension ( owner: enclosingScope. parentValue, nestedScopes: innerScopes,
391+ context) )
392+ }
363393}
364394
365395/// Compute the range of the a scope owner. Nested scopes must stay within this range.
@@ -584,6 +614,17 @@ private extension LifetimeDependence.Scope {
584614 case let . yield( yieldedValue) :
585615 let beginApply = yieldedValue. definingInstruction as! BeginApplyInst
586616 return beginApply. createEnd ( builder, context)
617+ case let . initialized( initializer) :
618+ switch initializer {
619+ case let . store( initializingStore: store, initialAddress: _) :
620+ if let sb = store as? StoreBorrowInst {
621+ return builder. createEndBorrow ( of: sb)
622+ }
623+ return nil
624+ case . argument, . yield:
625+ // TODO: extend indirectly yielded scopes.
626+ return nil
627+ }
587628 default :
588629 return nil
589630 }
0 commit comments