-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[MiscDiagnostics] Update diagnostics for IUO to Any coercion #23617
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
Conversation
|
cc @xedin |
|
Could you take another look @xedin? I ran the tests locally and have got a few failing ones, all for implicit coercions from IUO to Any. I'll fix them now |
xedin
left a comment
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.
Looks much better now, few more comments inline and test-case is missing.
|
Thanks @xedin, I have made the changes and updated the existing tests. Not sure if I should add another test case because the existing ones are good enough? I think we lost some warnings for cases where we should be emitting a warning. |
xedin
left a comment
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.
Still needs some work but looks much better now!
|
Thank you @xedin, I have made the changes you requested. I can't use |
|
@theblixguy Looks like for the |
|
@xedin Sounds good! However, there's one issue left now, which is that func warnCollectionOfOptionalToAnyCoercion(_ a: [Int?], _ d: [String : Int?]) {
takesCollectionOfAny(a, d)
}
func takesCollectionOfAny(_ a: [Any], _ d: [String : Any]) {}Here, our |
|
@theblixguy That's because |
|
@xedin We end up saying |
|
Here's an idea from the top of my head: we can create a vector to hold a struct containing a locator and the two types. If we don't already have a struct in there with the same locator, we push one. Otherwise, we skip. Later when creating the fix, we can retrieve the struct (using the locator as "key") and extract the original types and pass it to the fix. Something like struct MatchTypesCacheItem {
ConstraintLocator *locator;
Type *type1;
Type *type2;
...
}
std::vector< MatchTypesCacheItem > matchTypesCache; |
|
@theblixguy I'm to a fan for trying to keep a stack of types like that in matchTypes because that's on the hot path... Tell me one thing - in that example from your previous comment - what locator does warning have? |
|
@xedin The locator is |
|
@theblixguy Ok, that's good! This means that if you strip away |
|
@xedin Okay! This is only for a But this may not work if the scenario is different i.e. when we're returning |
|
@theblixguy You can't actually use My suggestion would be to try multiple different examples and see what locators are going to be and if it would be possible to deduce types from locators (e.g. you can find type variables by locator as well by iterating over |
|
Thanks @xedin for the suggestion! We don't always have all the type variables we need in CS. For example: func foo(value x: Int?) -> Any {
let y: Any = x
}in this case, we'll only have one type variable in I am not sure if there is one solution for this - we probably need different approaches depending on what the locator is or how many type variables are there. Maybe storing the original types somewhere might be simpler. |
|
Sorry I'm not say that we should away use type variables... I think that it might be okay to pinpoint types like new diagnostic would do e.g. instead of |
|
Oh sorry, I misunderstood! We can probably use a combination of type variables, locators and perhaps anchor to determine the two types (I gave it a shot and it seems to work sometimes), but at that point, I would say it starts to feel like a total hack and we might as well just store the original types in a vector in the constraint system as I originally suggested. I am not sure if there's going to be any noticeable impact on performance because we will only store it once (per locator) and it might be useful in future fixes. Also, while we have regressed slightly in terms of showing the correct type in the diagnostic, the other problem is we actually don't emit any diagnostic in some cases because the types we store in the fix aren't correct (ex: it lost one level of optionality, or we get the element type rather than array type, etc - due to the recursion in Example: Here we lost the optionality so we get |
|
Let’s just fix this in MiscDiagnostics and take time to experiment with warning fix. |
|
I don't like losing the Array part, because there's no expression in that statement that has the type |
|
I have updated the tests with the new diagnostic - ran it locally and all of them pass. @xedin could you run the smoke tests if everything looks good to you? |
|
@swift-ci please test |
|
Build failed |
|
Build failed |
|
Unrelated failure on Linux:
|
|
@swift-ci please test Linux platform |
|
Build failed |
|
@swift-ci please test Linux platform |
|
Build failed |
|
Same unrelated failure with TestFoundation - the Swift tests passed though. |
|
@swift-ci please test Linux platform |
|
It was a tough day for Linux CI yesterday, should be resolved now :) |
|
Thank you @xedin! The tests have passed now 🎉 I will create a PR now to cherry-pick this to the 5.1 branch. |
|
No problem! Please cherry-pick a squashed commit I have merged. |
This PR updates the diagnostics for implicit coercion from
T!toAny.Resolves SR-10199.