Files
swift-mirror/test/Interpreter/SDK/objc_fast_enumeration.swift
Doug Gregor 67ca1c9ea1 Implement the new casting syntaxes "as" and "as?".
There's a bit of a reshuffle of the ExplicitCastExpr subclasses:
  - The existing ConditionalCheckedCastExpr expression node now represents
"as?". 
  - A new ForcedCheckedCastExpr node represents "as" when it is a
  downcast.
  - CoerceExpr represents "as" when it is a coercion.
  - A new UnresolvedCheckedCastExpr node describes "as" before it has
  been type-checked down to ForcedCheckedCastExpr or CoerceExpr. This
  wasn't a strictly necessary change, but it helps us detangle what's
  going on.

There are a few new diagnostics to help users avoid getting bitten by
as/as? mistakes:
  - Custom errors when a forced downcast (as) is used as the operand
  of postfix '!' or '?', with Fix-Its to remove the '!' or make the
  downcast conditional (with as?), respectively.
  - A warning when a forced downcast is injected into an optional,
  with a suggestion to use a conditional downcast.
  - A new error when the postfix '!' is used for a contextual
  downcast, with a Fix-It to replace it with "as T" with the
  contextual type T.

Lots of test updates, none of which felt like regressions. The new
tests are in test/expr/cast/optionals.swift. 

Addresses <rdar://problem/17000058>


Swift SVN r18556
2014-05-22 06:15:29 +00:00

95 lines
1.8 KiB
Swift

// RUN: %target-run-simple-swift | FileCheck %s
import Foundation
var a: NSArray = ["one", 2, [1,2,3]]
var a_m: NSMutableArray = ["two", 12, [11,12,13]]
// CHECK: one
// CHECK: 2
// CHECK: (
// CHECK: 1,
// CHECK: 2,
// CHECK: 3
// CHECK: )
for x: AnyObject in a {
println(x.description!)
}
// CHECK: two
// CHECK: 12
// CHECK: (
// CHECK: 11,
// CHECK: 12,
// CHECK: 13
// CHECK: )
for x: AnyObject in a_m {
println(x.description!)
}
class Canary {
var x: Int
deinit { println("dead \(x)") }
init(_ x: Int) { self.x = x }
func chirp() { println("\(x)") }
}
autoreleasepool {
println("making array")
var b: NSArray = NSArray(objects: [Canary(1), Canary(2), Canary(3)], count: 3)
println("iterating array")
// CHECK: 1
for x: AnyObject in b {
(x as Canary).chirp()
break
}
// CHECK: exiting
println("exiting")
}
// CHECK: dead
// CHECK: dead
// CHECK: dead
// CHECK: exited
println("exited")
var d : NSDictionary = [415 : "Giants", 510 : "A's"]
var d_m : NSMutableDictionary = [1415 : "Big Giants", 11510 : "B's"]
// CHECK: 510 => A's
for (key, value) in d {
println("\(key.description!) => \(value.description!)")
}
// CHECK: 11510 => B's
for (key, value) in d_m {
println("\(key.description!) => \(value.description!)")
}
var s = NSSet(object: "the most forward-thinking test yet")
var s_m = NSMutableSet(object: "the next most forward-thinking test yet")
// CHECK: the most forward-thinking test yet
for x: AnyObject in s {
println(x.description!)
}
// CHECK: the next most forward-thinking test yet
for x: AnyObject in s_m {
println(x.description!)
}
// Enumeration over an _NSSwiftArray
// CHECK: hello
// CHECK: bridged
// CHECK: array
var a2 = ["hello", "bridged", "array"]
var nsa2 = (a2._asCocoaArray() as AnyObject) as NSArray
for x: AnyObject in nsa2 {
println(x.description!)
}