-
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
Tracking issue for RFC 1558: Allow coercing non-capturing closures to function pointers #39817
Comments
I'd like to implement this. Which steps are required? |
@est31 Awesome, thank you! @eddyb or @nikomatsakis, can one of you give some pointers? |
I'd reuse the existing "reify to Then HIR+Ty->HAIR lowering of the reify coercion has to generate something like... wait, no 😢 |
Hmm seems its more than I thought. As I'm rather tight with time this week, is it a problem if I implement it only next week? |
@est31 Absolutely! I don't know of anyone chomping at the bit to do this implementation work. |
@eddyb I am not so sure how I feel about using this coercion. In particular, as we discussed in the RFC thread itself, it would imply that the closure from which we coerce is a "zero-capture" closure -- well, I guess we can actually check that eagerly easily enough, since the list of upvars that a closure uses is something we analyze very early. (i.e., the freevars list is available quite early.) |
@nikomatsakis I've assumed we use that early check all along ;). |
@eddyb well as usual you're two steps ahead of me. =) Have pity on an old man. That said, do we want to allow said coercion is you only capture zero-sized types? I guess I'm happy not to permit that for now. =) |
Implement non-capturing closure to fn coercion Implements non capturing closure coercion ([RFC 1558](https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md)). cc tracking issue rust-lang#39817
Implement non-capturing closure to fn coercion Implements non capturing closure coercion ([RFC 1558](https://github.com/rust-lang/rfcs/blob/master/text/1558-closure-to-fn-coercion.md)). cc tracking issue rust-lang#39817
cc @rust-lang/lang Is this ready for FCP? Can it get in for 1.19? |
It seems to be fully implemented, but I don't know if anyone has actually had any experience using it. Any users who have, it would be great to hear from you. I don't have any objection to stabilizing in principle, don't know about anyone else. I'd like to know that someone has used it and not had any ICEs or anything though. |
Use case I found for this: |
this gives a list of people using the feature on github. Maybe ask them? I personally was (positively) surprised that the Rust ABI for having |
@withoutboats #40204 (which affects this feature) is yet unresolved. |
One problem I had trying to use this feature was the type inference for the expected type of arguments doesn't work. For example, this code does not compile https://is.gd/FNoLWg: #![feature(closure_to_fn_coercion)]
fn foo(f: fn(Vec<u32>) -> usize) { }
fn main() {
foo(|x| x.len())
} UPDATE: Opened an issue (#41755) with some mentoring instructions. |
I tried to use thing in ring here: briansmith/ring@44170d4. However, I got the error:
The type of The problem is that the implementation doesn't allow closures where |
Yeah, converting to extern "C" is not really possible right now. Implementing that to a satisfying degree is a bit bigger than the initial implementation. First the ABI of the closure needs to be inferred, and then the code that creates the actual closure function needs to be changed, to use the inferred type. Also, one needs to create an error when you put the closure into a local variable and use it in both extern "C" and normal extern "rust" contexts. |
I would think we would generate a wrapper for the closure's (internal) function. (Though I guess ideally we'd do this more uniformly or not at all, probably.) |
Now that the outstanding bugs have been resolved, this feature is ready at least for consideration for stabilization. @rfcbot fcp merge |
Team member @aturon has proposed to merge this. The next step is review by the rest of the tagged teams: No concerns currently listed. Once these reviewers reach consensus, this will enter its final comment period. If you spot a major issue that hasn't been raised at any point in this process, please speak up! See this document for info about what commands tagged team members can give me. |
2 weeks to get this in for 1.19 |
If we want it in until then, this can become tight. In the optimal case, people can already prepare PRs to the respective repositories for the neccessary steps required for stabilisation, like documentation. Also, as the time is pressing, it would be nice to have contributors for this who can devote enough time to get the PRs through review in a timely manner. All of this of course only if we actually want this feature in 1.19. |
So I've opened PRs to document and stabilize the feature, to hopefully speed up the process around those. @steveklabnik do you think the book requires documentation of this feature before stabilization? |
🔔 This is now entering its final comment period, as per the review above. 🔔 |
@est31 I think this is a bit in-the-weeds for the book, so I'm leaning towards no. @carols10cents ? |
Agreed, I think the reference and the API docs (if those are relevant?) is more appropriate. |
I'm not aware of any examples in the documentation that could be written more concisely with this feature present, nor do I know of an easy way to spot them if there are any. When replacing the There is also (to my knowledge) no api page for the So I'd say API docs would require no updating. |
IIUC, that would not "zero cost" and so it would break the language design principles. It would be zero-cost if the closure's internal function were always inlined into the wrapper, I guess. But then that's the same as not having a wrapper, right? |
@briansmith The ABI could be part of monomorphization such that the closure isn't translated until it's either used with a Fn trait or coerced to an fn pointer and the latter would choose the ABI of the closure body itself. @michaelwoerister would be able to say if the trans item collector could reasonably do this. |
Stabilize non capturing closure to fn coercion Stabilisation PR for non capturing closure to fn coercion. closes rust-lang#39817
Stabilize non capturing closure to fn coercion Stabilisation PR for non capturing closure to fn coercion. closes rust-lang#39817
RFC
Summary:
Outstanding issues:
fn
#41755 -- inference shortcomingsThe text was updated successfully, but these errors were encountered: