Files
swift-mirror/test/expr/capture/order.swift
Jordan Rose f54e3570a3 Preserve the location of the 'in' when doing void-conversion on a closure.
This location is used when deciding whether a capture has already been
initialized, and without it the compiler decides that the reference to
a name from the capture list should be rejected.

rdar://problem/19776255&20153574

Swift SVN r26103
2015-03-13 17:35:14 +00:00

42 lines
942 B
Swift

// RUN: %target-parse-verify-swift
func makeIncrementor(amount: Int) -> () -> Int {
func incrementor() -> Int {
currentTotal += amount // expected-error{{cannot capture 'currentTotal' before it is declared}}
return currentTotal // note: redundant diagnostic suppressed
}
var currentTotal = 0 // expected-note{{'currentTotal' declared here}}
return incrementor
}
func outOfOrderEnum() {
func f() -> Suit {
return .Club
}
enum Suit {
case Club
case Diamond
case Heart
case Spade
}
}
func captureInClosure() {
var x = { (i: Int) in
currentTotal += i // expected-error{{use of local variable 'currentTotal' before its declaration}}
}
var currentTotal = 0 // expected-note{{'currentTotal' declared here}}
}
class X {
func foo() { }
}
func captureLists(x: X) {
{ [unowned x] in x.foo() }();
let _: Void = { [unowned x] in x.foo() }()
let _: Void = { [weak x] in x?.foo() }()
}