-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Emit a warning when optionals are coerced to Any. #4900
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
// RUN: %target-parse-verify-swift | ||
|
||
func takeAny(_ left: Any, _ right: Any) -> Int? { | ||
return left as? Int | ||
} | ||
|
||
func throwing() throws -> Int? {} | ||
|
||
func warnOptionalToAnyCoercion(value x: Int?) -> Any { | ||
let a: Any = x // expected-warning {{expression implicitly coerced from 'Int?' to Any}} | ||
// expected-note@-1 {{provide a default value to avoid this warning}} | ||
// expected-note@-2 {{force-unwrap the value to avoid this warning}} | ||
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}} | ||
|
||
let b: Any = x as Any | ||
|
||
let c: Any = takeAny(a, b) // expected-warning {{expression implicitly coerced from 'Int?' to Any}} | ||
// expected-note@-1 {{provide a default value to avoid this warning}} | ||
// expected-note@-2 {{force-unwrap the value to avoid this warning}} | ||
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}} | ||
|
||
let _: Any = takeAny(c, c) as Any | ||
|
||
let _: Any = (x) // expected-warning {{expression implicitly coerced from 'Int?' to Any}} | ||
// expected-note@-1 {{provide a default value to avoid this warning}} | ||
// expected-note@-2 {{force-unwrap the value to avoid this warning}} | ||
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}} | ||
|
||
let f: Any = (x as Any) | ||
let g: Any = (x) as (Any) | ||
|
||
_ = takeAny(f as? Int, g) // expected-warning {{expression implicitly coerced from 'Int?' to Any}} | ||
// expected-note@-1 {{provide a default value to avoid this warning}} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'd like at least one of these note-cascades to test the Fix-Its themselves using the
to make sure the locations are reasonable and don't break in the future. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, you're right! I'll add one on master. |
||
// expected-note@-2 {{force-unwrap the value to avoid this warning}} | ||
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}} | ||
|
||
let _: Any = takeAny(f as? Int, g) as Any // expected-warning {{expression implicitly coerced from 'Int?' to Any}} | ||
// expected-note@-1 {{provide a default value to avoid this warning}} | ||
// expected-note@-2 {{force-unwrap the value to avoid this warning}} | ||
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}} | ||
|
||
let _: Any = takeAny(f as? Int as Any, g) as Any | ||
|
||
let _: Any = x! == x! ? 1 : x // expected-warning {{expression implicitly coerced from 'Int?' to Any}} | ||
// expected-note@-1 {{provide a default value to avoid this warning}} | ||
// expected-note@-2 {{force-unwrap the value to avoid this warning}} | ||
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}} | ||
|
||
do { | ||
let _: Any = try throwing() // expected-warning {{expression implicitly coerced from 'Int?' to Any}} | ||
// expected-note@-1 {{provide a default value to avoid this warning}} | ||
// expected-note@-2 {{force-unwrap the value to avoid this warning}} | ||
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}} | ||
} catch {} | ||
|
||
return x // expected-warning {{expression implicitly coerced from 'Int?' to Any}} | ||
// expected-note@-1 {{provide a default value to avoid this warning}} | ||
// expected-note@-2 {{force-unwrap the value to avoid this warning}} | ||
// expected-note@-3 {{explicitly cast to Any with 'as Any' to silence this warning}} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think we should be eagerly desugaring here; it makes the diagnostic worse.
getOptionalObjectType
should look through sugar already.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(as should
isAny
and any other such query)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yes, that's the type we're displaying.
I'll commit a follow-up to master and then cherry-pick and add that to this PR.