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
Comments
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 cc @xedin |
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... |
Do you want to take a look at this one, @theblixguy? |
Sure. Do we not want to emit this diagnostic if we're implicitly unwrapping to Any? |
@theblixguy No, we should keep that, but before emitting such diagnostic we should check if that optional type is actually optional and not IUO. |
Hmm, seems like it's not possible to use `OptionalTypeKind` for this. Probably need to check the type repr. |
@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 |
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. |
Maybe what we need to do is if (srcType->getOptionalObjectType() && hasImplicitlyUnwrappedResult(subExpr)) {
return;
} |
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. |
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(). |
Nevermind, I fixed it. I'll create a PR now |
PR: #23617 |
Can you cherry-pick this to 5.1 when you and Pavel are done with it? |
Sure! |
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. |
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. |
Additional Detail from JIRA
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:
And then later I reference it as an 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.
The text was updated successfully, but these errors were encountered: