Files
swift-mirror/test/Interpreter/SDK/objc_nsstring_bridge.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

64 lines
1.6 KiB
Swift
Raw Blame History

// RUN: %target-run-simple-swift | FileCheck %s
import Foundation
// Add a method to String to make sure a value is really a String and not
// implicitly converted from an NSString, since method lookup doesn't see through
// implicit conversions.
extension String {
func reallyAString() -> String { return self }
}
func printString(x: String) {
println(x)
}
func printDescription(o: AnyObject) {
println(o.description!.reallyAString())
}
class Pootie : NSObject {
override var description : String {
return "cole me down on the panny sty"
}
func sinePittyOnRunnyKine(x: String) -> String {
return "\(x). sa-da-tay"
}
}
var a:NSNumber = 2001
printDescription(a) // CHECK: 2001
var p = Pootie()
printDescription(p) // CHECK: cole me down on the panny sty
var s1:String = "wa-da-ta"
// We don't say 'var s2:NSString = "..."' in order to keep this test independent of the
// ABI of NSString.convertFromStringLiteral.
var s2s:String = "kappa-chow"
var s2:NSString = s2s
printDescription(s2) // CHECK: kappa-chow
printDescription(p.sinePittyOnRunnyKine(s2)) // CHECK: kappa-chow. sa-da-tay
var s3:String = s2.stringByAppendingPathComponent(s1).reallyAString()
printDescription(s3) // CHECK: kappa-chow/wa-da-ta
// Unicode conversion
var s4 : String = NSString(string: "\uf8ff\ufffd")
printDescription(s4) // CHECK: <EFBFBD>
// NSCFConstantString conversion
var s5 : String = NSRangeException
printDescription(s5) // CHECK: NSRangeException
// Check conversions to AnyObject
var s6: NSString = "foo"
var ao: AnyObject = s6.copy()
var s7 = ao as NSString
var s8 = ao as? String
// CHECK-NEXT: done
println("done")