Files
swift-mirror/test/Parse/foreach.swift
John McCall 7815892a76 Add unique typo corrections to the main diagnostic with a fix-it.
Continue to emit notes for the candidates, but use different text.
Note that we can emit a typo correction fix-it even if there are
multiple candidates with the same name.

Also, disable typo correction in the migrator, since the operation
is quite expensive, the notes are never presented to the user, and
the fix-its can interfere with the migrator's own edits.

Our general guidance is that fix-its should be added on the main
diagnostic only when the fix-it is highly likely to be correct.
The exact threshold is debateable.  Typo correction is certainly
capable of making mistakes, but most of its edits are right, and
when it's wrong it's usually obviously wrong.  On balance, I think
this is the right thing to do.  For what it's worth, it's also
what we do in Clang.
2018-04-07 16:01:39 -04:00

41 lines
1.4 KiB
Swift

// RUN: %target-typecheck-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>) { // expected-note {{'r' declared here}}
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'; did you mean 'r'?}}
// 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
// FIXME: Bad diagnostics; should be just 'expected 'in' after for-each patter'.
for i r { // expected-error {{found an unexpected second identifier in constant declaration}}
} // expected-note @-1 {{join the identifiers together}}
// expected-note @-2 {{join the identifiers together with camel-case}}
// expected-error @-3 {{expected 'in' after for-each pattern}}
// expected-error @-4 {{expected Sequence expression for for-each loop}}
for i in r sum = sum + i; // expected-error{{expected '{' to start the body of for-each loop}}
}