Skip to content

[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

Merged
merged 4 commits into from
Feb 20, 2020

Conversation

LucianoPAlmeida
Copy link
Contributor

Tailored diagnostic when ConditionalCheckedCastExpr involves a literal subexpr that can be statically coerced.
cc @CodaFi

Resolves SR-11421.


// Special handle for literals conditional checked cast when they can be
// statically coerced to the cast type.
if (isa<LiteralExpr>(sub)) {
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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 :)

// statically coerced to the cast type.
if (isa<LiteralExpr>(sub)) {
if (auto protocol = TypeChecker::getLiteralProtocol(ctx, sub)) {
if (!fromType->isEqual(toType) &&
Copy link
Contributor

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?

Copy link
Contributor Author

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 :)

Copy link
Contributor Author

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 :)

@LucianoPAlmeida LucianoPAlmeida force-pushed the SR-11421-checked-cast-diag branch 2 times, most recently from 8c5faf1 to aa44334 Compare January 30, 2020 03:05
@LucianoPAlmeida LucianoPAlmeida force-pushed the SR-11421-checked-cast-diag branch 3 times, most recently from 477a748 to 348d81e Compare February 4, 2020 04:11
@LucianoPAlmeida
Copy link
Contributor Author

Just a few minor adjustments, this may be good to review.
cc @CodaFi :)

@LucianoPAlmeida LucianoPAlmeida force-pushed the SR-11421-checked-cast-diag branch 6 times, most recently from 05774cc to 47b7602 Compare February 5, 2020 17:06
@LucianoPAlmeida
Copy link
Contributor Author

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.
@CodaFi I'm not sure if we can do much more than that considering this case, so let me know what you think 👍

@xedin since you are involved with the latest diagnostics improvements, can you give some thoughts on this one, especially if the message is ideal :)

@xedin
Copy link
Contributor

xedin commented Feb 7, 2020

@LucianoPAlmeida I have this open and going to try and give it a look soon. Sorry, I have been swamped lately...

@LucianoPAlmeida
Copy link
Contributor Author

@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 :)

Copy link
Contributor

@xedin xedin left a 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...

@LucianoPAlmeida
Copy link
Contributor Author

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 :)
That makes total sense... and got me thinking that we can see a couple of diagnostics like this in CSApply, it should be possible for some of them to be moved to the new framework?

@xedin
Copy link
Contributor

xedin commented Feb 11, 2020

Yes, that's something which should happen, CSApply shouldn't diagnose anything.

@LucianoPAlmeida LucianoPAlmeida force-pushed the SR-11421-checked-cast-diag branch from 47b7602 to fd2dbe3 Compare February 13, 2020 04:04
@LucianoPAlmeida
Copy link
Contributor Author

I had forgot about this one, it should be good to go? cc @xedin

@xedin
Copy link
Contributor

xedin commented Feb 19, 2020

If @CodaFi doesn't have any other suggestions I think that this is good to go!

@xedin
Copy link
Contributor

xedin commented Feb 19, 2020

@swift-ci please smoke test

@xedin
Copy link
Contributor

xedin commented Feb 19, 2020

@LucianoPAlmeida Looks like is a single test failure on Linux.

@LucianoPAlmeida
Copy link
Contributor Author

@LucianoPAlmeida Looks like is a single test failure on Linux.

Humm... I'll take a look. Thanks for kick-in the CI :)

@theblixguy
Copy link
Collaborator

theblixguy commented Feb 19, 2020

The test can run on Linux if we split ones that use NSString/NSNumber into a separate file (so they're only run on macOS and rest runs on both Linux and macOS)

@xedin
Copy link
Contributor

xedin commented Feb 19, 2020

@LucianoPAlmeida You can use // REQUIRES: OS=macosx instead of objc_interop

@xedin
Copy link
Contributor

xedin commented Feb 19, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 47b7602d1046549928840a8d7f4dae5309061f1c

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 47b7602d1046549928840a8d7f4dae5309061f1c

@LucianoPAlmeida
Copy link
Contributor Author

@xedin Just did the adjustments real quick :)
In which cases is ideal to use // REQUIRES: OS=macosx and which is ideal REQUIRES: objc_interop?

@LucianoPAlmeida
Copy link
Contributor Author

Those new failures seems unrelated ...

@xedin
Copy link
Contributor

xedin commented Feb 19, 2020

@LucianoPAlmeida objc_interop i believe is going to run on all - iOS, tvOS and watchOS so if there are API differences between them tests would fail.

@xedin
Copy link
Contributor

xedin commented Feb 19, 2020

@swift-ci please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 705e468

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 705e468

@LucianoPAlmeida
Copy link
Contributor Author

ninja: error: 'lib/sourcekitd.framework/sourcekitd', needed by 'stdlib/tools/swift-lang/OSX/x86_64/SwiftLang.o', missing and no known rule to make it seems to be unrelated

@LucianoPAlmeida
Copy link
Contributor Author

@LucianoPAlmeida objc_interop i believe is going to run on all - iOS, tvOS and watchOS so if there are API differences between them tests would fail.

Got it 👍 Thank you :)

@owenv
Copy link
Contributor

owenv commented Feb 19, 2020

I think the revert/fix in #29934 should fix that SourceKit issue

@xedin
Copy link
Contributor

xedin commented Feb 19, 2020

Alright, we'll wait then :)

@theblixguy
Copy link
Collaborator

@swift-ci please clean test macOS

@xedin xedin merged commit 8af4f2d into swiftlang:master Feb 20, 2020
@LucianoPAlmeida LucianoPAlmeida deleted the SR-11421-checked-cast-diag branch March 25, 2021 23:45
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.

6 participants