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-11421] Checked Cast Diagnostics Should Be More Specific When Literals Are Involved #53822
Comments
Comment by Mohit Athwani (JIRA) @CodaFi I'd like to contribute to this with your assistance. |
@CodaFi Let me try this! |
Hey @CodaFi I have ran the exact same code, I have got this output:
What exact type of phrasing are you looking for, because the above seems very clear and is also giving a suggestion to use the |
@Rajveer100 This is definitely an improvement, but I think we can take another step forward and mention the inferred type of the literal as well. I can imagine people wondering why their supposed |
Yeah I understand that, could you exactly specify to what it could be potentially changed to, what would be your suggestion? |
Do you have any ideas in mind? Of course, I could just throw in a suggestion without asking you first, but to me that defeats the purpose of a good first issue. |
So overall, the fix is to change the diagnosed message to a more better phrasing and provide a fix it to it if the casting is possible. While I am solving another issue, I will get back to this. |
@AnthonyLatsis Back into this issue! |
Hey Rajveer, got any ideas on how to improve the message yet? Suppose you compile |
In my understanding, considering the three cases of
Considering the |
|
Okay, so the reason the cast fails is that |
In which perspective are they 'unrelated', since a |
They are "unrelated" in the type system, not logically. "Unrelated" in this sense means that neither is a descendent (subtype) of the other. With the exception of bridging conversions and some other edge cases, Swift’s type safety implies that a type cast/coercion fails if the types are "unrelated". |
That makes sense. |
So we can probably update the diagnostic warning to, |
I suggest we use the same phrasing as in |
Right... |
Additional Detail from JIRA
md5: 54d2d8b997d41666f9024642eb576483
cloned to:
Issue Description:
Checked casts on literals can have surprising effects at face value
The former is a character literal and is statically coerced. The latter is a
String
literal that is being dynamically cast to aCharacter
. This cast will always fail, and we note as much - this also works withas!
force casts.We could definitely do a better job phrasing this warning to let the user know what went wrong and can even offer a fixit that changes the dynamic cast to a static cast if it's going to succeed.
The text was updated successfully, but these errors were encountered: