Skip to content

Commit 9422986

Browse files
committed
Add a bunch more 'try' tests.
Some of these are currently failing. For example: ```swift // expected-error {{cannot convert value of type 'Int??' to specified type 'Int'}} let _: Int = try? producer.produceDoubleOptionalInt() ``` This actually produces this warning: ``` value of optional type 'Int?' not unwrapped; did you mean to use 'try!' or chain with '?'? ``` Note that the value in question is not an `Int?`, so a suggestion of `try!` here is inappropriate. (And, due to the change in semantics of 'try?', wouldn't change anything anyway.)
1 parent ba03d12 commit 9422986

File tree

1 file changed

+60
-3
lines changed

1 file changed

+60
-3
lines changed

test/Parse/try.swift

Lines changed: 60 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,11 +161,10 @@ func producesOptional() throws -> Int? { return nil }
161161
let singleOptional = try? producesOptional()
162162
let _: String = singleOptional // expected-error {{cannot convert value of type 'Int?' to specified type 'String'}}
163163

164-
let _ = (try? foo())!!
164+
let _ = (try? foo())!! // expected-error {{cannot force unwrap value of non-optional type 'Int'}}
165165

166166
func producesDoubleOptional() throws -> Int?? { return 3 }
167-
168-
let _: String = try? producesDoubleOptional() // expected-error {{ cannot convert value of type 'Int??' to specified type 'String'}}
167+
let _: String = try? producesDoubleOptional() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}
169168

170169
func maybeThrow() throws {}
171170
try maybeThrow() // okay
@@ -203,3 +202,61 @@ _ = "a\()b" // expected-error {{interpolation can throw but is not marked with '
203202
// expected-note@-2 {{did you mean to handle error as optional value?}} {{5-5=try? }}
204203
// expected-note@-3 {{did you mean to disable error propagation?}} {{5-5=try! }}
205204
_ = try "\() \(1)"
205+
206+
func testGenericOptionalTry<T>(_ call: () throws -> T ) {
207+
let _: String = try? call() // expected-error {{cannot convert value of type 'T?' to specified type 'String'}}
208+
}
209+
210+
func genericOptionalTry<T>(_ call: () throws -> T ) -> T? {
211+
let x = try? call() // no error expected
212+
return x
213+
}
214+
215+
// Test with a non-optional type
216+
let _: String = genericOptionalTry({ () throws -> Int in return 3 }) // expected-error {{cannot convert value of type 'Int?' to specified type 'String'}}
217+
218+
// Test with an optional type
219+
let _: String = genericOptionalTry({ () throws -> Int? in return nil }) // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}
220+
221+
func produceAny() throws -> Any {
222+
return 3
223+
}
224+
225+
let _: Int? = try? produceAny() as? Int // good
226+
let _: Int? = (try? produceAny()) as? Int // good
227+
let _: String = try? produceAny() as? Int // expected-error {{cannot convert value of type 'Int?' to specified type 'String'}}
228+
let _: String = (try? produceAny()) as? Int // expected-error {{cannot convert value of type 'Int?' to specified type 'String'}}
229+
230+
231+
struct ThingProducer {
232+
func produceInt() throws -> Int { return 3 }
233+
func produceIntNoThrowing() -> Int { return 3 }
234+
func produceAny() throws -> Any { return 3 }
235+
func produceOptionalAny() throws -> Any? { return 3 }
236+
func produceDoubleOptionalInt() throws -> Int?? { return 3 }
237+
}
238+
239+
let optProducer: ThingProducer? = ThingProducer()
240+
let _: Int? = try? optProducer?.produceInt()
241+
let _: Int = try? optProducer?.produceInt() // expected-error {{value of optional type 'Int?' not unwrapped; did you mean to use 'try!' or chain with '?'?}}
242+
let _: String = try? optProducer?.produceInt() // expected-error {{cannot convert value of type 'Int?' to specified type 'String'}}
243+
let _: Int? = try? optProducer?.produceIntNoThrowing() // expected-warning {{no calls to throwing functions occur within 'try' expression}}
244+
245+
let _: Int?? = try? optProducer?.produceInt() // This was the expected type before Swift 5, but this still works; just adds more optional-ness
246+
247+
let _: Int? = (try? optProducer?.produceAny()) as? Int // good
248+
let _: Int? = try? optProducer?.produceAny() as? Int // good
249+
let _: Int?? = try? optProducer?.produceAny() as? Int // good
250+
let _: String = try? optProducer?.produceAny() as? Int // expected-error {{cannot convert value of type 'Int?' to specified type 'String'}}
251+
252+
let _: Int? = try? optProducer?.produceDoubleOptionalInt() // expected-error {{value of optional type 'Int??' not unwrapped; did you mean to use 'try!' or chain with '?'?}}
253+
let _: String = try? optProducer?.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}
254+
let _: String = try? optProducer?.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}
255+
256+
let producer = ThingProducer()
257+
258+
let _: Int = try? producer.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'Int'}}
259+
let _: Int? = try? producer.produceDoubleOptionalInt() // expected-error {{value of optional type 'Int??' not unwrapped; did you mean to use 'try!' or chain with '?'?}}
260+
let _: Int?? = try? producer.produceDoubleOptionalInt() // good
261+
let _: Int??? = try? producer.produceDoubleOptionalInt() // good
262+
let _: String = try? producer.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}

0 commit comments

Comments
 (0)