I am hitting a compilation error, which I think is a false positive, attempting to upgrade some code to use BorrowingIteratorProtocol since it compiles fine if I remove the first condition from the if statement (tested with the 6.4.x-snapshot-2026-08-14, nightly, and devsnapshot toolchains on Arch Linux).
Is it actually a false positive, if so I'll open an issue on github, or am I holding it wrong?
Godbolt for small reproducer.
small reproducer
struct BitSet: Sendable {
typealias Element = UInt64
fileprivate var storage:Element
}
extension BitSet {
func makeBorrowingIterator() -> BorrowingIterator {
.init()
}
struct BorrowingIterator: BorrowingIteratorProtocol {
@_lifetime(borrow self)
func nextSpan(maxCount: Int) -> Span<Element> {
.init()
}
}
}
let set = BitSet(storage: .max)
var smallestValue:UInt64! = nil
var iterator = set.makeBorrowingIterator()
while true {
let iteratorSpan = iterator.nextSpan()
guard !iteratorSpan.isEmpty else { break }
for i in iteratorSpan.indices {
if smallestValue == nil || iteratorSpan[i] < smallestValue! {
smallestValue = iteratorSpan[i]
}
}
}
compilation error
22 | var iterator = set.makeBorrowingIterator()
23 | while true {
24 | let iteratorSpan = iterator.nextSpan()
| | - note: it depends on this scoped access to variable 'iterator'
| - error: lifetime-dependent variable 'iteratorSpan' escapes its scope
25 | guard !iteratorSpan.isEmpty else { break }
26 | for i in iteratorSpan.indices {
27 | if smallestValue == nil || iteratorSpan[i] < smallestValue! {
| - note: this use of the lifetime-dependent value is out of scope
28 | smallestValue = iteratorSpan[i]
29 | }
Edit:
small statement workaround that successfully compiles
if smallestValue == nil {
smallestValue = iteratorSpan[i]
} else if iteratorSpan[i] < smallestValue! {
smallestValue = iteratorSpan[i]
}