-
Notifications
You must be signed in to change notification settings - Fork 10.6k
[ConstraintSystem] Make it possible to infer subtype bindings through argument conversions #30006
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
…ve" generic parameters
Consider following example:
```swift
struct MyType<TyA, TyB> {
var a : TyA, b : TyB
}
typealias B<T1> = MyType<T1, T1>
_ = B(a: "foo", b: 42)
```
Here `T1` is equal to `TyA` and `TyB` so diagnostic about
conflicting arguments ('String' vs. 'Int') should only be
produced for "representative" `T1`.
… is used as a mutable one
… argument conversions
Enable solver to transitively infer bindings through argument conversion
constraints. That helps to infer bindings for (generic) parameters
from their arguments e.g.
```swift
func foo<T: ExpressibleByStringLiteral>(_: String, _: T) -> T {
fatalError()
}
func bar(_: Any?) {}
func test() {
bar(foo("", ""))
}
```
In this case `T` can currently only be inferred as `Any?`
(based on parameter type of `bar`) although a complete
set of bindings for that type variable includes `String`
as well, which comes from use of `T` in argument position.
Resolves: rdar://problem/56212087
In case of contextual failures such bindings could produce better solutions with fewer fixes.
|
@swift-ci please test |
|
@swift-ci please test source compatibility |
|
@xedin Just looking at this I was wondering if this in some way also fixes SR-9839 func foo(_ x: @escaping @convention(block) () -> Void) {}
func id<T>(_ x: T) -> T {
return x
}
var qux: () -> Void = {}
foo(qux)
foo(id(qux)) // error: Cannot convert value of type '() -> Void' to expected argument type '@convention(block) () -> Void'I'm not exactly sure, because still didn't work too much on it to know what's the problem, |
|
Build failed |
|
@LucianoPAlmeida The way to solve problem described in SR-9839 is to make sure that |
|
@swift-ci please test Linux platform |
@xedin Humm.. got it, Thanks. I’ll resume work there soon, I just thought that since that one involves generic arguments inference too, it could be related to this one in some way :) |
… through argument conversions" Reverts swiftlang#30006. It caused a regression that we'd like to address before re-landing: ```swift struct X { var cgf: CGFloat } func test(x: X?) { let _ = (x?.cgf ?? 0) <= 0.5 } ``` This reverts commit 0a6b444. This reverts commit ed25559. This reverts commit 3e01160. This reverts commit 96297b7. Resolves: rdar://problem/60185506
Enable solver to transitively infer bindings through argument conversion
constraints. That helps to infer bindings for (generic) parameters
from their arguments e.g.
In this case
Tcan currently only be inferred asAny?(based on parameter type of
bar) although a completeset of bindings for that type variable includes
Stringas well, which comes from use of
Tin argument position.Inferring types transitively helps diagnostics as well because
it makes it possible to produce a better solution by attempting
e.g. default literal types inferred for arguments when the
problem is contextual e.g.
In cases like this both
IntandVoidcould be attempted forresult type of the function which makes it much easier to diagnose.
Resolves: rdar://problem/56212087