mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
37 lines
1.1 KiB
Swift
37 lines
1.1 KiB
Swift
// RUN: %target-parse-verify-swift
|
|
|
|
struct IntRange<Int> : Sequence, IteratorProtocol {
|
|
typealias Element = (Int, Int)
|
|
func next() -> (Int, Int)? {}
|
|
|
|
typealias Iterator = IntRange<Int>
|
|
func makeIterator() -> IntRange<Int> { return self }
|
|
}
|
|
|
|
func for_each(r: Range<Int>, iir: IntRange<Int>) {
|
|
var sum = 0
|
|
|
|
// Simple foreach loop, using the variable in the body
|
|
for i in r {
|
|
sum = sum + i
|
|
}
|
|
// Check scoping of variable introduced with foreach loop
|
|
i = 0 // expected-error{{use of unresolved identifier 'i'}}
|
|
|
|
// For-each loops with two variables and varying degrees of typedness
|
|
for (i, j) in iir {
|
|
sum = sum + i + j
|
|
}
|
|
for (i, j) in iir {
|
|
sum = sum + i + j
|
|
}
|
|
for (i, j) : (Int, Int) in iir {
|
|
sum = sum + i + j
|
|
}
|
|
|
|
// Parse errors
|
|
for i r { // expected-error 2{{expected ';' in 'for' statement}} expected-error {{use of unresolved identifier 'i'}} expected-error {{type 'Range<Int>' does not conform to protocol 'Boolean'}}
|
|
}
|
|
for i in r sum = sum + i; // expected-error{{expected '{' to start the body of for-each loop}}
|
|
}
|