Skip to content
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

[SR-10199] Swift 5 Force unwrapped optional is getting warning of casting optional to Any #52599

Closed
swift-ci opened this issue Mar 27, 2019 · 18 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers regression swift 5.0 type checker Area → compiler: Semantic analysis

Comments

@swift-ci
Copy link
Collaborator

Previous ID SR-10199
Radar None
Original Reporter murthyveda2000 (JIRA User)
Type Bug
Status Resolved
Resolution Done
Additional Detail from JIRA
Votes 0
Component/s Compiler
Labels Bug, 5.0Regression, DiagnosticsQoI, StarterBug, TypeChecker
Assignee @theblixguy
Priority Medium

md5: 4fa37f2df1e096a8d1e88933647f691e

Issue Description:

After upgrading to XCode 10.2 and converting to Swift 5, I am getting a ton of new warnings related to force unwrapped optionals.

Here is a very simple example, but I'm getting the example this error all over the place (especially for the first argument in NSLayoutContraint constructor).

If a property is defined as such:

@IBOutlet weak var shutterButton : UIButton! 

And then later I reference it as an Any:

var views : [Any] = []
views.append(shutterButton) // Expression implicitly coerced from 'UIButton?' to 'Any'

I get the error which I put into the comment there.

Since shutterButter is actually a UIButton and not a UIButton?, I don't think I should be getting this warning and don't get this warning in Swift 4.2.

@belkadan
Copy link
Contributor

The rule for IUOs is "try to treat them as Optional before trying to unwrap", but I agree that the diagnostic doesn't make sense here. Still, you can work around it with an explicit ! or as.

cc @xedin

@xedin
Copy link
Member

xedin commented Mar 27, 2019

It looks like when IUO was made an attribute of the decl, we didn't fix all of the places to check for that attribute...

@xedin
Copy link
Member

xedin commented Mar 27, 2019

Do you want to take a look at this one, @theblixguy?

@theblixguy
Copy link
Collaborator

Sure. Do we not want to emit this diagnostic if we're implicitly unwrapping to Any?

@xedin
Copy link
Member

xedin commented Mar 27, 2019

@theblixguy No, we should keep that, but before emitting such diagnostic we should check if that optional type is actually optional and not IUO.

@theblixguy
Copy link
Collaborator

Hmm, seems like it's not possible to use `OptionalTypeKind` for this. Probably need to check the type repr.

@xedin
Copy link
Member

xedin commented Mar 27, 2019

@theblixguy Since the attribute is attached to declaration you'd have to get declaration for optional and go from there similar to https://github.com/apple/swift/blob/master/lib/Sema/ConstraintSystem.cpp#L2116

@theblixguy
Copy link
Collaborator

Hmm something like this doesn't work:

if (srcType->getOptionalObjectType()) {
  auto decl = srcType->getEnumOrBoundGenericEnum();
  if (decl->getAttrs().hasAttribute<ImplicitlyUnwrappedOptionalAttr>()) {
      return;
  }
}

I do get the declaration for optional, but it doesn't have that attribute.

@theblixguy
Copy link
Collaborator

Maybe what we need to do is

if (srcType->getOptionalObjectType() && hasImplicitlyUnwrappedResult(subExpr)) { 
  return;
} 

@xedin
Copy link
Member

xedin commented Mar 27, 2019

I'd expect that attribute to be associated with declaration of the entity IOU type is associated with e.g. property in this case, instead of Optional itself (which is what you are checking). I would be great if we could extract that warning from MiscDiagnostic and use a fix to diagnose it, it could be done in matchTypes.

@theblixguy
Copy link
Collaborator

Sounds good, I have a fix ready for it, just trying to figure out why it's not diagnosing (even after passing `warning=true` to ConstraintFix). Maybe the fix is being ignored somehow by matchTypes().

@theblixguy
Copy link
Collaborator

Nevermind, I fixed it. I'll create a PR now

@theblixguy
Copy link
Collaborator

PR: #23617

@belkadan
Copy link
Contributor

belkadan commented Apr 2, 2019

Can you cherry-pick this to 5.1 when you and Pavel are done with it?

@theblixguy
Copy link
Collaborator

Sure!

@theblixguy
Copy link
Collaborator

After a lot of discussion, we've decided to update the diagnostic to make the behaviour clearer, instead of removing the diagnostic completely. This is now resolved on master by PR #23617 and cherry-picked to the swift-5.1 branch as well.

@swift-ci
Copy link
Collaborator Author

Comment by Veda Murthy (JIRA)

Given that the warnings will still be there, can I get some advice on what you think I should do in my example code?

Options:

  1. Make no code change and leave the warning, but then I'll have hundreds of ignored warnings in my project and won't be able to find real warnings among them.

  2. Change views.append(shutterButton) to views.append(shutterButton, but then if someone later changes the definition of shutterButton to UIButton? instead of UIButton), a bug could get introduced here.

  3. Wrap every reference to shutterButton in an "if let" block. I guess this is the most correct but then what benefit did I get from defining it as a UIButton! instead of a UIButton?

I would really appreciate your perspective on this. Thanks!

@theblixguy
Copy link
Collaborator

I would suggest reading https://swift.org/blog/iuo/.

1) You could do that, but it depends on how many warnings there are and whether you want these warnings to persist.
2) `UIButton?` and `UIButton!` are basically the same, except that the latter is unwrapped automatically when needed. So, I don't think switching to `UIButton?` would create a bug.
3) Even if you defined it as `UIButton?`, it would trigger the same warning.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself diagnostics QoI Bug: Diagnostics Quality of Implementation good first issue Good for newcomers regression swift 5.0 type checker Area → compiler: Semantic analysis
Projects
None yet
Development

No branches or pull requests

5 participants