mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
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
42 lines
942 B
Swift
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() }()
|
|
}
|