mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
Previously, we were binding optional l-values only when performing an access. This meant that other evaluations emitted before the formal access were not being short-circuited, even if the language rules said they should be. For example, consider this code:: var array : [Int]? = ... array?[foo()] = bar() Neither foo nor bar should be called if the array is actually nil, because those calls are sequenced after the optional-chaining operator ?. The way that we currently do this is to project out the optional address during formal evaluation. This means that there's a formal access to that storage beginning with the formal evaluation of the l-value and lasting until the operation is complete. That's a little controversial, because it means that other formal accesses during that time to the optional storage will have unspecified behavior according to the rules I laid out in the accessors proposal; we should talk about it and make a decision about whether we're okay with this behavior. But for now, it's important to at least get the right short-circuiting behavior from ?. Swift SVN r23608
22 lines
669 B
Swift
22 lines
669 B
Swift
// RUN: %target-run-simple-swift | FileCheck %s
|
|
|
|
var x: Int! = 0
|
|
x! = 2
|
|
println(x) // CHECK: 2
|
|
x!++
|
|
println(x) // CHECK-NEXT: 3
|
|
|
|
var sequences = ["fibonacci": [1, 1, 2, 3, 0]]
|
|
println(sequences) // CHECK-NEXT: [fibonacci: [1, 1, 2, 3, 0]]
|
|
sequences["fibonacci"]![4] = 5
|
|
println(sequences) // CHECK-NEXT: [fibonacci: [1, 1, 2, 3, 5]]
|
|
sequences["fibonacci"]!.append(8)
|
|
println(sequences) // CHECK-NEXT: [fibonacci: [1, 1, 2, 3, 5, 8]]
|
|
|
|
func printAndReturn(x: Int) -> Int { println(x); return x }
|
|
|
|
println("optional binding") // CHECK-NEXT: optional binding
|
|
var y: Int? = nil
|
|
y? += printAndReturn(4)
|
|
println("done with binding test") // CHECK-NEXT: done with binding test
|