-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-11421][Diagnostics] Tailored diagnostic for checked downcast with literals #29493
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][Diagnostics] Tailored diagnostic for checked downcast with literals #29493
Conversation
lib/Sema/CSApply.cpp
Outdated
|
||
// Special handle for literals conditional checked cast when they can be | ||
// statically coerced to the cast type. | ||
if (isa<LiteralExpr>(sub)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please leave behind a FIXME about revising this behavior? This is a warning for a deeply unintuitive behavior that we need to revisit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed SR-12093 about this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please leave behind a FIXME about revising this behavior? This is a warning for a deeply unintuitive behavior that we need to revisit.
Sure :)
lib/Sema/CSApply.cpp
Outdated
// statically coerced to the cast type. | ||
if (isa<LiteralExpr>(sub)) { | ||
if (auto protocol = TypeChecker::getLiteralProtocol(ctx, sub)) { | ||
if (!fromType->isEqual(toType) && |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens if the to type is more complex like NSString or NSNumber?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still emitted, and the same is also valid for any type that conforms to the actual literal protocol can be coerced e.g.
struct S: ExpressibleByStringLiteral, CustomStringConvertible {
typealias StringLiteralType = String
var _value: String = ""
init(stringLiteral value: Self.StringLiteralType) {
self._value = value
}
var description: String { _value }
}
let a = "A" as S
print(a) // "A"
I'm just adding the test cases :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Note: When the literal inferred type can be bridged to the cast type, the conditional downcast always succeeds, also added the test cases :)
8c5faf1
to
aa44334
Compare
477a748
to
348d81e
Compare
Just a few minor adjustments, this may be good to review. |
05774cc
to
47b7602
Compare
Offering a fix-it to these situations is not the best approach because of normal cases conditional downcasts are used like if let a = "a" as? Character {
} And a fix-it to replace with coercion would not be ideal. So this is now only a better message with a hint to use coercion. @xedin since you are involved with the latest diagnostics improvements, can you give some thoughts on this one, especially if the message is ideal :) |
@LucianoPAlmeida I have this open and going to try and give it a look soon. Sorry, I have been swamped lately... |
@xedin No worries, And thank you for taking the time to look at this :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM! Looks like to make SR-12093 work we need to do a revamp of how checked casts are handled in the solver. Having it warn in CSApply for now is fine but ultimately we should be able to detect this in the solver and avoid producing a valid solution with invalid casts...
Thanks for the review @xedin :) |
Yes, that's something which should happen, CSApply shouldn't diagnose anything. |
…volving literals that can be statically coerced
47b7602
to
fd2dbe3
Compare
I had forgot about this one, it should be good to go? cc @xedin |
If @CodaFi doesn't have any other suggestions I think that this is good to go! |
@swift-ci please smoke test |
@LucianoPAlmeida Looks like is a single test failure on Linux. |
Humm... I'll take a look. Thanks for kick-in the CI :) |
The test can run on Linux if we split ones that use |
@LucianoPAlmeida You can use |
@swift-ci please test |
Build failed |
Build failed |
@xedin Just did the adjustments real quick :) |
Those new failures seems unrelated ... |
@LucianoPAlmeida |
@swift-ci please test |
Build failed |
Build failed |
|
Got it 👍 Thank you :) |
I think the revert/fix in #29934 should fix that SourceKit issue |
Alright, we'll wait then :) |
@swift-ci please clean test macOS |
Tailored diagnostic when
ConditionalCheckedCastExpr
involves a literal subexpr that can be statically coerced.cc @CodaFi
Resolves SR-11421.