Skip to content

[Sema] Improve Optional-to-Any diagnostics for collections, nested optionals & IUOs #14221

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 6 commits into from
Feb 3, 2018

Conversation

hamishknight
Copy link
Contributor

@hamishknight hamishknight commented Jan 28, 2018

  • When implicitly coercing a collection of optional elements to a collection of less-optional Any elements, e.g [Any?] to [Any], emit a slightly better diagnostic:

    Expression implicitly coerced from '[Any?]' to '[Any]'

    and allow for silencing of the diagnostic with an explicit coercion, e.g as [Any].

  • When implicitly coercing a nested optional to a less optional Any, e.g Any?? to Any?, emit a slightly better diagnostic:

    Expression implicitly coerced from 'Any??' to 'Any?'

    and allow for silencing of the diagnostic with an explicit coercion, e.g as Any?.

(both diagnostics were previously "Expression implicitly coerced from 'Any?' to Any", and neither could be silenced by an explicit coercion to their destination types)

In addition, this PR enables Optional-to-Any warnings for IUOs, e.g the implicit coercion from Int! to Any now produces a warning. Previously a warning wasn't emitted despite the optional being implicitly erased to Any (but warnings were emitted in some other cases, such as Int?! to Any?; this PR fixes that inconsistency).

Resolves SR-2928 & SR-6859.

// to Any coercions (this doesn't take into consideration implicit
// coercions such as Any?! to Any?, however; we warn on those).
if (subExpr->getType()->getImplicitlyUnwrappedOptionalObjectType())
return;
Copy link
Contributor Author

@hamishknight hamishknight Jan 28, 2018

Choose a reason for hiding this comment

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

It would be great if we could remove this, which would enable warnings for implicit IUO to Any coercions, such as shown in the dontWarnIUOToAnyCoercion function in the tests (which should really produce warnings, as they're not being implicitly unwrapped; they're just being quietly erased to Any). With IUOs being removed from the type system, I believe this should happen at some point; but not sure if it's okay to just enable them this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

I would say feel free to warn about the IUOs as well. It will fall out from my last commit to always generate Optional from '!', but there is no reason not to do so now, and it's really inconsistent that we haven't been emitting the warnings in that case as well.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rudkx Thanks, I'll go ahead and make that change then. It looks like we have 3 tests that make such implicit IUO coercions, two of which look like they can be updated with as Any without affecting the test (1 & 2). Though I'm not sure about the third one, can it be updated with an explicit as Any or as [Any], or would it be best to just add an expected warning?

Copy link
Contributor

Choose a reason for hiding this comment

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

I would add the expected warning on the last one, but yes I think you're probably right that adding the coercions for the others is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@rudkx Thanks, I've gone ahead and done that now.

@hamishknight hamishknight changed the title Improve Optional-to-Any diagnostics for collections and nested optionals [Sema] Improve Optional-to-Any diagnostics for collections and nested optionals Jan 28, 2018
@hamishknight hamishknight changed the title [Sema] Improve Optional-to-Any diagnostics for collections and nested optionals [Sema] Improve Optional-to-Any diagnostics for collections, nested optionals & IUOs Jan 29, 2018
@rudkx
Copy link
Contributor

rudkx commented Jan 29, 2018

@swift-ci Please smoke test

…ion type

This will let us customise the diagnostic for collections and nested optionals.
When implicitly coercing a nested optional to a less optional `Any`, e.g `Any??` to `Any?`, emit a slightly better diagnostic:

Expression implicitly coerced from 'Any??' to 'Any?'

and allow for silencing of the diagnostic with an explicit coercion, e.g `as Any?`.
When implicitly coercing a collection of optional elements to a collection of less-optional `Any` elements, e.g `[Any?]` to `[Any]`, emit a slightly better diagnostic:

Expression implicitly coerced from '[Any?]' to '[Any]'

and allow for silencing of the diagnostic with an explicit coercion, e.g `as [Any]`.
For example, the implicit coercion from `Int!` to `Any` now produces a warning. Previously a warning wasn't emitted despite the optional being implicitly erased to `Any` (but warnings were emitted in some other cases, such as `Int?!` to `Any?`; this commit fixes that inconsistency).
@hamishknight
Copy link
Contributor Author

hamishknight commented Feb 2, 2018

@rudkx I've updated the tests to consider #14299 (so now the diagnostics only show strong optional, not IUO types), could you please kick off the CI bot again?

As IUOs types are no longer generated, we don't show them in diagnostics.
@rudkx
Copy link
Contributor

rudkx commented Feb 3, 2018

@swift-ci Please smoke test

@rudkx
Copy link
Contributor

rudkx commented Feb 3, 2018

I'll take a closer look at the changes as well.

@rudkx
Copy link
Contributor

rudkx commented Feb 3, 2018

LGTM. I'll merge. That will result in conflicts in another of my PR but I'll get that fixed up ASAP.

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.

2 participants