Skip to content

[SR-16058][Sema] Consider wrapping type variable layers of optionality when warning about checked casts #42050

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 1 commit into from
Mar 31, 2022

Conversation

LucianoPAlmeida
Copy link
Contributor

In #39648 I have only considered an extra layer of optionality coming from a value-to-optional constraint restriction.
But that is not the only case where extra optionals need to be added. This can come from overload resolution of a unresolved member producing as result a generic argument that would be bound by context wrapped in layers of optionality. For example the case in the SR:

  let dict: [Int: String?] = [:]
  let foo: Int? = 1
  let _: String? = foo.flatMap { dict[$0] } as? String  // OK

If we look to the constraints debugging we can se that checked cast constraint is
$T3 checked cast to String
Where overload being attempted is
selected overload set choice Int?.flatMap: $T1 == (($T2) throws -> $T3?) throws -> $T3?
So when recording warning fixes we should consider these extra layer of optionality($T3?) in the "from" checked cast sub-expression.

This changes the logic to be based on type of checked cast "from" expression when it is a type variable wrapped in optionals to handle this correctly.

Resolves SR-16058.

@LucianoPAlmeida LucianoPAlmeida requested a review from xedin March 27, 2022 22:41
@LucianoPAlmeida
Copy link
Contributor Author

@swift-ci Please smoke test

@xedin
Copy link
Contributor

xedin commented Mar 30, 2022

Sorry it took a bit, I'm going to take a look at this today.

@LucianoPAlmeida
Copy link
Contributor Author

Sorry it took a bit, I'm going to take a look at this today.

No worries @xedin! Thank you for taking the time for those reviews and for the always insightful feedbacks, really appreciated :)

@LucianoPAlmeida LucianoPAlmeida merged commit df913c9 into swiftlang:main Mar 31, 2022
@LucianoPAlmeida LucianoPAlmeida deleted the SR-16058-casts branch March 31, 2022 02:44
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.

2 participants