Skip to content

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

Merged
merged 2 commits into from
Sep 22, 2016
Merged

Emit a warning when optionals are coerced to Any. #4900

merged 2 commits into from
Sep 22, 2016

Conversation

rudkx
Copy link
Contributor

@rudkx rudkx commented Sep 21, 2016

Pulling this diagnostic change for SE-0140 into swift-3.0-branch.

Emit a warning for optionals that are implicitly converted to Any, and
add fixits giving options to:

  • Add '??' with a default value after
  • Force-unwrap the optional with '!'
  • Explicitly cast to 'as Any' to silence the warning

This covers diagnostics aspect of SE-0140.

rdar://problem/28196843

Emit a warning for optionals that are implicitly converted to Any, and
add fixits giving options to:
- Add '??' with a default value after
- Force-unwrap the optional with '!'
- Explicitly cast to 'as Any' to silence the warning

This covers diagnostics aspect of SE-0140.

rdar://problem/28196843
@rudkx rudkx added this to the Swift 3.0 milestone Sep 21, 2016
@rudkx
Copy link
Contributor Author

rudkx commented Sep 21, 2016

@swift-ci Test OS X.

@rudkx
Copy link
Contributor Author

rudkx commented Sep 21, 2016

@DougGregor Can you review for swift-3.0-branch?

auto subExpr = cast<ErasureExpr>(E)->getSubExpr();
auto erasedTy = subExpr->getType()->getDesugaredType();
if (erasedTy->getOptionalObjectType()) {
TC.diagnose(subExpr->getStartLoc(), diag::optional_to_any_coercion,
Copy link
Contributor

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.

Copy link
Contributor

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)

Copy link
Contributor Author

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.

NOTE(force_optional_to_any,none,
"force-unwrap the value to avoid the warning", ())
NOTE(silence_optional_to_any,none,
"explicitly cast to Any with 'as Any' to silence the warning", ())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: Clang typically says "this warning" rather than "the warning". I don't think Swift has enough of these to count as having a convention.

@jrose-apple
Copy link
Contributor

Do we want this warning for AnyObject as well?

@jckarter
Copy link
Contributor

You can't put an optional into an AnyObject without explicit coercion already.

@jrose-apple
Copy link
Contributor

Ah, I didn't realize that. Okay, thanks.

Don't explicitly desguar types when it is not needed and/or results in
worse types displayed in diagnostics.

Tweak the warning messages to use "this warning" rather than "the
warning".

Addresses feedback from Jordan on commit 401ca24.

(cherry picked from commit d813579)
@rudkx
Copy link
Contributor Author

rudkx commented Sep 21, 2016

@swift-ci Test OS X platform.

@rudkx
Copy link
Contributor Author

rudkx commented Sep 21, 2016

@swift-ci Test OS X.

@rudkx
Copy link
Contributor Author

rudkx commented Sep 21, 2016

@jrose-apple Thanks, I think everything is fixed now.

@rudkx
Copy link
Contributor Author

rudkx commented Sep 22, 2016

@DougGregor Doug, I think this is ready for review now (although tests are still running at the moment).

Copy link
Member

@DougGregor DougGregor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just bumped into this warning on master earlier today, and it's a big improvement. My one comment can be fixed on master if it expedites things.

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}}
Copy link
Member

Choose a reason for hiding this comment

The 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 -verify syntax, e.g.,

// expected-note@-1 {{provide a default value to avoid this warning}}{{23-23= ?? <#default value#>}}

to make sure the locations are reasonable and don't break in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you're right! I'll add one on master.

@tkremenek tkremenek merged commit e4adcc9 into swiftlang:swift-3.0-branch Sep 22, 2016
@rudkx rudkx deleted the optional-to-any-warning-swift-3.0-branch branch September 22, 2016 17:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants