Skip to content

Commit fd2dbe3

Browse files
[tests] Adding literal downcast tests
1 parent 968a37d commit fd2dbe3

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// RUN: %target-typecheck-verify-swift -enable-objc-interop
2+
3+
import Foundation
4+
5+
let ok = "A" as Character // OK
6+
let succeed = "A" as? String // expected-warning {{always succeeds}}
7+
let bad = "A" as? Character // expected-warning {{conditional downcast from literal to 'Character' always fails; consider using 'as' coercion}} {{none}}
8+
let bad2 = "Aa" as? Character // expected-warning {{cast from 'String' to unrelated type 'Character' always fails}}
9+
let bad1 = 1 as? Character // expected-warning {{cast from 'Int' to unrelated type 'Character' always fails}}
10+
11+
let okInt = 1 as Int // OK
12+
let badInt = 1 as? Int // expected-warning {{always succeeds}}
13+
let badInt1 = 1.0 as? Int // expected-warning {{cast from 'Double' to unrelated type 'Int' always fails}}
14+
let badInt2 = 1 as? Double // expected-warning {{conditional downcast from literal to 'Double' always fails; consider using 'as' coercion}} {{none}}
15+
16+
let okUInt = 1 as UInt // OK
17+
let badUInt = 1 as? UInt // expected-warning {{conditional downcast from literal to 'UInt' always fails; consider using 'as' coercion}} {{none}}
18+
let badUInt1 = 1.0 as? UInt // expected-warning {{cast from 'Double' to unrelated type 'UInt' always fails}}
19+
20+
// Can downcast by bridging
21+
let bridge = "A" as? NSString // expected-warning {{always succeeds}}
22+
let bridge1 = 1 as? NSNumber // expected-warning {{always succeeds}}
23+
24+
// Custom protocol adoption
25+
struct S: ExpressibleByStringLiteral {
26+
typealias StringLiteralType = String
27+
init(stringLiteral value: Self.StringLiteralType) {}
28+
}
29+
30+
let a = "A" as? S // expected-warning {{conditional downcast from literal to 'S' always fails; consider using 'as' coercion}} {{none}}

0 commit comments

Comments
 (0)