-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[SR-9839] Fixes ambiguity in convention function argument inference #30022
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-9839] Fixes ambiguity in convention function argument inference #30022
Conversation
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.
I'd suggest a different approach - let's detect this situation in matchFunctionTypes
and increase score for SK_FunctionConversion
because passing () -> Void
to @convention(block) () -> Void
parameter requires an implicit conversion, so it's always better to pick a type which doesn't require one.
I think this is a better approach because instead of asking ranking algorithm to find where the difference is solver would record exact spot and type of difference.
Even better idea might be to join bindings based on representation if it's allowed, so instead of two bindings we'd just have one - |
Hey @xedin Thank you for the review :)
Awesome, I'm at work right now, so I'll take a look and change this later tonight :) |
Sounds good! |
I think it does make sense to join regular Swift convention to |
@xedin Just a quick question, as I just got back to this ... this |
Hm, I guess it's not going to be called because one of the types is subtype and another is a supertype... I guess there is no other choice but to make it work with score in |
Humm ... that makes sense, but I'm still not sure how to go about to decide when to increase the scores or not, here are the matchings when solving the system for this case when both
So right now, I can't really see how I would increase the score to prefer an type over the other ... |
Also, @xedin Thank you for the help here, really appreciate :) |
That's curious since it doesn't matter what we attempt for |
No idea, looking into it right now :) |
One thing I've noted is that I was expecting to get into the
But even for the |
|
Seems like this also fixes SR-9840 :) |
Yeah, the problem is that the two types are both viable solutions(not sure if that's what you mean by "legit ambiguity") ... e.g. func foo(_ x: @escaping @convention(block) () -> Void) {}
// If we ignore type inference, and the solver choose any of the solutions, they are both applicable
func f( _ a: @escaping () -> Void, _ b: @escaping @convention(block) () -> Void ) {
foo(a) // Ok
foo(b) // Ok
} We can note this here:
|
Sorry, I mean it's really ambiguous and not just a bug because I think we should just make it diagnose ambiguous and be done. |
Humm, I see ... I thought that since both types could be implicit converted to one another what I called |
I think it would have to be more than just test-cases because at least original example from SR is not diagnosed as ambiguity at the moment... |
It is for SR-9839, ... right now here is the diagnostic
|
Yeah, that's the problem, I think that diagnostic should point out exactly what is ambiguous here :) |
Oh right, I'm on it :) |
d19ad1e
to
481c58b
Compare
@xedin One thing that still got me thinking is the fact that although this is really ambiguous, it may not be too obvious to the user. func foo(_ x: @escaping @convention(block) () -> Void) {}
func id<T>(_ x: T) -> T {
return x
}
var qux: () -> Void = {}
foo(qux)
let p = id(qux) // This will be infered as ()->Void
foo(p) // Works fine ...
foo(id(qux)) // This will error, but for the user, this may not be too obvious and
// confusing given that for them, this is just a one-liner shorten form
// to write the same of above. So maybe they would expect the same semantics So my question is if there is a way to prioritize the first bind e.g. Anyways it is just a thought, and I want to understand better(especially the downsides) to know if we could improve the situation :) |
@xedin I was thinking in being more specific with those diagnostics and provide something like |
I promise to take a look at this on Friday. Sorry, I got distracted by other things this week :/ |
@LucianoPAlmeida We could try one more way to disambiguate this. Similar to my suggestion to use The difference between
And there is going to be a solution for each type. That's why I was suggesting either try to join the types (which is unfortunately impossible) or increase a score for one of the cases. |
No worries @xedin :) These things take time and you have other things 👍 |
a361989
to
2fd2b86
Compare
2fd2b86
to
45796be
Compare
@xedin Can you check on this one when you have some time? :) Just to know if this approach is ok and we just have to tweak details or should we go the other way and just diagnose this ambiguous ... |
45796be
to
76c163e
Compare
Sorry it took me awhile to come back to this, was trying to see whether I could come up with anything else given time... After looking at the code again I still feel like what we are trying to do is a hack and we should try to diagnose it better instead. WDYT? |
@xedin
Agree, I kinda felt the same and also that this situation is too specific to worth making those hacks to only this specific case to work. |
No worries, thank you for seeing it through! |
76c163e
to
bfda020
Compare
Right, diagnostic implemented \o/ |
34c4578
to
4c8f1c8
Compare
…neric parameter bindings with no fixes
c45009f
to
426a2f8
Compare
Alright, let's give it a try. |
@swift-ci please smoke test |
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.
Let's ! Thank you, @LucianoPAlmeida!
\o/ Thanks @xedin! |
Resolves the ambiguity issue between @\convention function types where the solver finds two possibilities in this case
() -> Void
and@convention(block) () -> Void
but couldn't disambiguate them.Resolves SR-9839 and SR-9840
cc @xedin @hamishknight