Skip to content

Commit c2ce16d

Browse files
[tests] Additional tests for some checked cast edge cases
1 parent a6ab3b6 commit c2ce16d

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

test/Misc/misc_diagnostics.swift

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -170,10 +170,30 @@ func tuple_splat2(_ q : (a : Int, b : Int)) {
170170
}
171171

172172
// SR-1612: Type comparison of foreign types is always true.
173-
func is_foreign(a: AnyObject) -> Bool {
173+
protocol SR1612_P {}
174+
class SR1612: NSObject, SR1612_P {}
175+
// Existentials
176+
func is_foreign_anyobject(a: AnyObject) -> Bool {
174177
return a is CGColor // expected-warning {{'is' test is always true because 'CGColor' is a Core Foundation type}}
175178
}
176179

180+
func is_foreign_any(a: Any) -> Bool {
181+
return a is CGColor // expected-warning {{'is' test is always true because 'CGColor' is a Core Foundation type}}
182+
}
183+
184+
func is_foreign_p(a: SR1612_P) -> Bool {
185+
return a is CGColor // expected-warning {{'is' test is always true because 'CGColor' is a Core Foundation type}}
186+
}
187+
188+
// Concrete type.
189+
func is_foreign_concrete(a: SR1612) -> Bool {
190+
return a is CGColor // False at runtime
191+
}
192+
// Concrete foundation.
193+
func is_foreign_s(a: NSString) -> Bool {
194+
return a is CGColor // False at runtime
195+
}
196+
177197
func test_implicit_cgfloat_conversion() {
178198
func test_to(_: CGFloat) {}
179199
func test_from(_: Double) {}
@@ -187,14 +207,14 @@ func test_implicit_cgfloat_conversion() {
187207
test_to(d + d) // Ok (Double -> CGFloat for both arguments)
188208
test_to(d + cgf) // Ok
189209
test_to(d + cgf - d) // Ok (prefer CGFloat -> Double for `cgf`), it's a better solution than trying to convert `d`s to `CGFloat`
190-
test_to(d + cgf - cgf) // Ok (only one choice here to conver `d` to CGFloat)
210+
test_to(d + cgf - cgf) // Ok (only one choice here to convert `d` to CGFloat)
191211

192212
test_from(cgf) // Ok (CGFloat -> Double)
193213
test_from(f) // expected-error {{cannot convert value of type 'Float' to expected argument type 'Double'}}
194214
test_from(cgf + cgf) // Ok (CGFloat -> Double for both arguments)
195215
test_from(d + cgf) // Ok
196216
test_from(cgf + d - cgf) // (prefer Double -> CGFloat for `d`), it's a better solution than trying to convert `cgf`s to `Double`
197-
test_from(cgf + d - d) // Ok (only one choice here to conver `cgf` to Double)
217+
test_from(cgf + d - d) // Ok (only one choice here to convert `cgf` to Double)
198218

199219
func test_returns_double(_: CGFloat) -> Double {
200220
42.0

test/expr/cast/as_coerce.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,18 @@ func sr6022() -> Any { return 0 }
118118
func sr6022_1() { return; }
119119
protocol SR6022_P {}
120120

121+
_ = sr6022 is SR6022_P // expected-warning {{cast from '() -> Any' to unrelated type 'any SR6022_P' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'?}}{{11-11=()}}
121122
_ = sr6022 as! SR6022_P // expected-warning {{cast from '() -> Any' to unrelated type 'any SR6022_P' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'?}}{{11-11=()}}
122123
_ = sr6022 as? SR6022_P // expected-warning {{cast from '() -> Any' to unrelated type 'any SR6022_P' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'}}{{11-11=()}}
124+
_ = sr6022_1 is SR6022_P // expected-warning {{cast from '() -> ()' to unrelated type 'any SR6022_P' always fails}}
123125
_ = sr6022_1 as! SR6022_P // expected-warning {{cast from '() -> ()' to unrelated type 'any SR6022_P' always fails}}
124126
_ = sr6022_1 as? SR6022_P // expected-warning {{cast from '() -> ()' to unrelated type 'any SR6022_P' always fails}}
125127

126128
func testSR6022_P<T: SR6022_P>(_: T.Type) {
129+
_ = sr6022 is T // expected-warning {{cast from '() -> Any' to unrelated type 'T' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'?}}{{13-13=()}}
127130
_ = sr6022 as! T // expected-warning {{cast from '() -> Any' to unrelated type 'T' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'?}}{{13-13=()}}
128131
_ = sr6022 as? T // expected-warning {{cast from '() -> Any' to unrelated type 'T' always fails}} // expected-note {{did you mean to call 'sr6022' with '()'?}}{{13-13=()}}
132+
_ = sr6022_1 is T // expected-warning {{cast from '() -> ()' to unrelated type 'T' always fails}}
129133
_ = sr6022_1 as! T // expected-warning {{cast from '() -> ()' to unrelated type 'T' always fails}}
130134
_ = sr6022_1 as? T // expected-warning {{cast from '() -> ()' to unrelated type 'T' always fails}}
131135
}

0 commit comments

Comments
 (0)