From 495d96d27da6fff8e4f0aceaf35815314cb17b9a Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Thu, 26 Mar 2026 13:50:25 -0700 Subject: [PATCH 1/3] [ContainersPreview] Better illuminate the `nextSpan` problem on ContainerIterator --- .../Container/ContainerIterator.swift | 25 ++++++++++--------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift b/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift index 59acffbd..d4edd518 100644 --- a/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift +++ b/Sources/ContainersPreview/Protocols/Container/ContainerIterator.swift @@ -52,23 +52,24 @@ where { public typealias Element_ = Base.Element +#if true @_unsafeNonescapableResult // FIXME: we cannot convert from a borrow to an inout dependence?! @_lifetime(&self) public mutating func nextSpan_(maximumCount: Int) -> Span { _base.value.nextSpan(after: &self._position, maximumCount: maximumCount) } - -// @_lifetime(copy self) -// public mutating func nextSpan2(maximumCount: Int) -> Span { -// var i = self._position -// let span = _base.value.nextSpan( -// after: &i, -// maximumCount: maximumCount) -// _ = consume span -// let result = _overrideLifetime(span, copying: self) -// self._position = i -// return .init() -// } +#else + @_lifetime(copy self) + public mutating func nextSpan(maximumCount: Int) -> Span { + var i = self._position + let span = _base.value.nextSpan( + after: &i, + maximumCount: maximumCount) + self._position = i + let result = _overrideLifetime(span, copying: self) + return result + } +#endif @_lifetime(self: copy self) public mutating func skip_(by maximumOffset: Int) -> Int { From 35b7d8fba478a047e9702c35c8d6e5a432f8b395 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 3 Apr 2026 17:01:23 -0700 Subject: [PATCH 2/3] Fix lifetime requirements rigidly enforced in the latest nightlies --- .../RigidDictionary/RigidDictionary+Indices.swift | 2 +- .../BasicContainers/RigidSet/RigidSet+BorrowingSequence.swift | 4 +++- .../Protocols/Container/Container+Filter.swift | 4 +++- Sources/DequeModule/RigidDeque/RigidDeque+Container.swift | 4 +++- 4 files changed, 10 insertions(+), 4 deletions(-) diff --git a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift index 120dd8d7..fcb331eb 100644 --- a/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift +++ b/Sources/BasicContainers/RigidDictionary/RigidDictionary+Indices.swift @@ -70,7 +70,7 @@ where Key: ~Copyable, Value: ~Copyable @inlinable public var underestimatedCount_: Int { self._base.value.count } - @_lifetime(copy self) + @_lifetime(borrow self) // FIXME: Should be @_lifetime(copy self) public func makeBorrowingIterator_() -> BorrowingIterator_ { let bit = self._base.value.makeBucketIterator() // FIXME: This override really should not be necessary. Check if the real `struct Borrow` fixes it. diff --git a/Sources/BasicContainers/RigidSet/RigidSet+BorrowingSequence.swift b/Sources/BasicContainers/RigidSet/RigidSet+BorrowingSequence.swift index ac0e382f..6c9aba63 100644 --- a/Sources/BasicContainers/RigidSet/RigidSet+BorrowingSequence.swift +++ b/Sources/BasicContainers/RigidSet/RigidSet+BorrowingSequence.swift @@ -43,6 +43,8 @@ extension RigidSet: BorrowingSequence_ where Element: ~Copyable { ~Copyable, ~Escapable { + public typealias Element_ = Element + @_alwaysEmitIntoClient internal var _baseAddress: UnsafePointer? @@ -68,7 +70,7 @@ extension RigidSet: BorrowingSequence_ where Element: ~Copyable { } @_alwaysEmitIntoClient - @_lifetime(copy self) + @_lifetime(&self) // FIXME: This should be `@_lifetime(copy self)` public mutating func nextSpan_(maximumCount: Int) -> Span { precondition(maximumCount > 0, "maximumCount must be positive") guard diff --git a/Sources/ContainersPreview/Protocols/Container/Container+Filter.swift b/Sources/ContainersPreview/Protocols/Container/Container+Filter.swift index 1a04fa45..6b3b72ba 100644 --- a/Sources/ContainersPreview/Protocols/Container/Container+Filter.swift +++ b/Sources/ContainersPreview/Protocols/Container/Container+Filter.swift @@ -87,7 +87,9 @@ where Base.Element: ~Copyable @available(SwiftStdlib 5.0, *) extension ContainerFilter: BorrowingIteratorProtocol_ where Element: ~Copyable { - @_lifetime(copy self) + public typealias Element_ = Base.Element + + @_lifetime(&self) // FIXME: This should be `@_lifetime(copy self)` public mutating func nextSpan_(maximumCount: Int) -> Span { precondition(maximumCount > 0) while true { diff --git a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift index e887e280..fb9b07c2 100644 --- a/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift +++ b/Sources/DequeModule/RigidDeque/RigidDeque+Container.swift @@ -24,6 +24,8 @@ extension RigidDeque where Element: ~Copyable { #if compiler(>=6.4) && COLLECTIONS_UNSTABLE_CONTAINERS_PREVIEW @frozen public struct BorrowingIterator: ~Escapable, BorrowingIteratorProtocol_ { + public typealias Element_ = Element + @usableFromInline internal var _currentSegment: Span @@ -44,7 +46,7 @@ extension RigidDeque where Element: ~Copyable { } @_alwaysEmitIntoClient - @_lifetime(copy self) + @_lifetime(&self) // FIXME: This should be `@_lifetime(copy self)` @_lifetime(self: copy self) public mutating func nextSpan_(maximumCount: Int) -> Span { let result = _currentSegment._trim(first: maximumCount) From 2417a006e27bb2806ab06c78a26562e3685e8579 Mon Sep 17 00:00:00 2001 From: Karoy Lorentey Date: Fri, 3 Apr 2026 17:23:11 -0700 Subject: [PATCH 3/3] Also adjust tests --- .../MinimalTypes/StaccatoContainer.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift b/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift index 4bf7b5dd..39c9ef67 100644 --- a/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift +++ b/Tests/_CollectionsTestSupport/MinimalTypes/StaccatoContainer.swift @@ -77,6 +77,8 @@ package struct _StaccatoParameters { @available(SwiftStdlib 5.0, *) public struct _StaccatoBorrowingIterator: BorrowingIteratorProtocol_, ~Escapable { + public typealias Element_ = Element + internal let _contents: Span internal let _params: _StaccatoParameters internal var _offset: Int @@ -88,7 +90,7 @@ public struct _StaccatoBorrowingIterator: BorrowingIteratorP self._offset = 0 } - @_lifetime(copy self) + @_lifetime(&self) // FIXME: Should be `@_lifetime(copy self)` public mutating func nextSpan_(maximumCount: Int) -> Span { let endOffset = _params.endOffset(fromOffset: _offset, maximumCount: maximumCount) let startOffset = _offset