-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Cannot coerce closures to fn(...) -> ...
in a slice if the slice contains only one function
#48109
Comments
This also depends on the order of items within the array. You must start with two different functions for it to work. IIRC the fn item to fn pointer coercion is a bit more "loose" than other coercions (it triggers a bit more often), causing this. I wasn't aware closures also coerce. |
You don’t have to start with two different functions, but you must have (at least) 2 different ones before the closure is seen. That is, you can have |
This is because two functions will coerce to a function pointer, but a function and a closure won't. The only reason it works for functions, without a coercion target being inferred from anywhere, is for backwards-compatibility, since some items were incorrectly typed as cc @rust-lang/lang We could allow |
@eddyb a good question. It feels like it would be useful, but might be quite confusing though, and perhaps problematic in the future if we ever get some form of union type thing going on, no? |
@nikomatsakis Well, right now, the lack of it breaks the associativity of the "LUB coercion": We originally special-cased Introducing more coercions (i.e. from closures) to a given special-cased And non-associative LUB-coerce is observable as order dependence of arrays, if-else/match arms, etc. In other words, for an order-independent LUB-coerce, the special-cases aside from |
Triage: no changes |
Support coercion between (FnDef | Closure) and (FnDef | Closure) Fixes rust-lang#46742, fixes rust-lang#48109 Inject `Closure` into the `FnDef x FnDef` coercion special case, which makes coercion of `(FnDef | Closure) x (FnDef | Closure)` possible, where closures should be **non-capturing**.
compiles and runs fine. (see https://play.rust-lang.org/?gist=de9e7ce8fd0683e1dbdcf362a2e16dd4&version=stable)
but if I remove
useful2
from the slice, type inference fails:(see https://play.rust-lang.org/?gist=428522c1c8e14eefaa690d5d09e67b82&version=stable )
If I insert
useful
twice (ie&[useful, useful, |x| x]
) I still get this error.The text was updated successfully, but these errors were encountered: