You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Since 'try?' no longer unconditionally adds a layer of optional, converting it
to 'try!' will no longer unconditionally remove a layer of optional. So let's not
suggest it. This leads to better diagnostics anyway.
Copy file name to clipboardExpand all lines: test/Parse/try.swift
+8-7Lines changed: 8 additions & 7 deletions
Original file line number
Diff line number
Diff line change
@@ -238,25 +238,26 @@ struct ThingProducer {
238
238
239
239
letoptProducer:ThingProducer?=ThingProducer()
240
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 '?'?}}
241
+
let _:Int=try? optProducer?.produceInt() // expected-error {{cannot convert value of type 'Int?' to specified type 'Int'}}
242
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
243
let _:Int??=try? optProducer?.produceInt() // This was the expected type before Swift 5, but this still works; just adds more optional-ness
246
244
245
+
let _:Int?=try? optProducer?.produceIntNoThrowing() // expected-warning {{no calls to throwing functions occur within 'try' expression}}
246
+
247
247
let _:Int?=(try? optProducer?.produceAny())as?Int // good
248
248
let _:Int?=try? optProducer?.produceAny()as?Int // good
249
249
let _:Int??=try? optProducer?.produceAny()as?Int // good
250
250
let _:String=try? optProducer?.produceAny()as?Int // expected-error {{cannot convert value of type 'Int?' to specified type 'String'}}
251
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
252
let _:String=try? optProducer?.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}
255
253
256
254
letproducer=ThingProducer()
257
255
258
256
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 '?'?}}
257
+
258
+
// We don't offer 'try!' here because it would not change the type of the expression in Swift 5+
259
+
let _:Int?=try? producer.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'Int?'}}
260
+
260
261
let _:Int??=try? producer.produceDoubleOptionalInt() // good
261
262
let _:Int???=try? producer.produceDoubleOptionalInt() // good
262
-
let _:String=try? producer.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}
263
+
let _:String=try? producer.produceDoubleOptionalInt() // expected-error {{cannot convert value of type 'Int??' to specified type 'String'}}
0 commit comments