Files
swift-mirror/test/Sema/foreach_borrowing.swift
Nate CookandElsa Keirouz fbc2a41fe4 Enable BorrowingForLoop and BorrowingSequence flags by default (#90545)
This PR converts these two experimental feature flags to regular
language flags – includes #90017.

Co-authored-by: Elsa Keirouz <e_keirouz@apple.com>
2026-07-10 23:18:55 -05:00

53 lines
2.2 KiB
Swift

// RUN: %target-typecheck-verify-swift \
// RUN: -verify-ignore-unrelated
@available(SwiftStdlib 6.4, *)
struct DualConformanceSeq: Sequence, Iterable {
typealias Element = Int
typealias BorrowingIterator = BorrowingIteratorAdapter<IndexingIterator<[Int]>>
func makeIterator() -> IndexingIterator<[Int]> { return [].makeIterator() }
}
@available(SwiftStdlib 6.4, *)
func testSequencePreferredOverIterable(seq: DualConformanceSeq) {
for x in seq {
_ = x
}
}
// A Iterable conformance that is incomplete because the declared
// BorrowingIterator type does not conform to BorrowingIteratorProtocol.
// The type also conforms to Sequence.
@available(SwiftStdlib 6.4, *)
struct IncompleteBorrowingWithSequence: Sequence, Iterable { // expected-error {{type 'IncompleteBorrowingWithSequence' does not conform to protocol 'Iterable'}} expected-note {{add stubs for conformance}}
typealias Element = Int
typealias BorrowingIterator = Int // expected-note {{possibly intended match 'IncompleteBorrowingWithSequence.BorrowingIterator' (aka 'Int') does not conform to 'BorrowingIteratorProtocol'}}
func makeIterator() -> IndexingIterator<[Int]> { return [].makeIterator() }
}
@available(SwiftStdlib 6.4, *)
func testIncompleteBorrowingWithSequence(seq: IncompleteBorrowingWithSequence) {
for x in seq {
_ = x
}
}
// A Iterable conformance that is incomplete because a required member
// (makeBorrowingIterator) is not provided. The type has no Sequence
// conformance. Because the conformance is still registered, the compiler
// attempts the Iterable desugar path, and fails to infer the element
// type from the incomplete conformance.
@available(SwiftStdlib 6.4, *)
struct IncompleteBorrowingNoSequence: Iterable { // expected-error {{type 'IncompleteBorrowingNoSequence' does not conform to protocol 'Iterable'}} expected-note {{add stubs for conformance}}
typealias Element = Int
typealias BorrowingIterator = Span<Int>.BorrowingIterator
// makeBorrowingIterator() intentionally omitted
}
@available(SwiftStdlib 6.4, *)
func testIncompleteBorrowingNoSequence(seq: IncompleteBorrowingNoSequence) {
for x in seq { // expected-error {{generic parameter 'Element' could not be inferred}}
_ = x
}
}