-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Unboxed closure metabug #18101
Comments
Re: #14798 -- Remove old (boxed) closures from the language Are we going to replace all the uses of boxed closures in the standard library with unboxed closures? I think that in some cases it may not be doable (*) without abstract return types (ART). Let me explain why with a (rather long) story: (*) Ok, it may be doable, but tedious. See alternatives section at the bottom: I was navigating ancient issues today, and found #12677: " The #[deriving(Clone)]
struct Map<'a, A, B, I> {
iter: I,
f: |A|:'a -> B, //~ error: type `|A| -> B` does not implement any method in scope named `clone`
} So, my first attempt at a solution was: "Let's change #[deriving(Clone)]
struct Map<A, B, I: Iterator<A>, F: FnMut(A) -> B> {
iter: I,
f: F,
}
impl<A, B, I: Iterator<A>, F: FnMut(A) -> B> Iterator<B> for Map<A, B, I, F> {
fn next(&mut self) -> Option<B> {
self.iter.next().map(|elt| self.f.call_mut((elt,)))
}
} Which compiles fine, it's cloneable and should work in principle, but when I tried to replicate the fn bytes<'a>(slice: &'a str) -> () {
Map {
iter: slice.as_bytes().iter(),
f: |&mut: &b| b,
}
} I wrote error: mismatched types: expected `()`, found `Map<_, _, core::slice::Items<'_, u8>, closure>` (expected (), found struct Map) Note the 4th specialization argument of If we had ART, then the return type could be AlternativesSince we don't have ART, here are a few alternatives for the particular case of the
@aturon This is relevant to the library stabilization process:
|
@japaric Note that you will be able to use "unboxed closures" in boxed form, something like: This is just the usual tradeoff between generics (with specialization) and trait objects (with dynamic dispatch). Right now, since we don't have abstract "unboxed" return types, anything returning a closure will have to use the trait object (boxed) form. Put another way, "unboxed closures" is really an unfortunate misnomer. What's really changing is that closures are going away as a "special" thing, and instead being replaced by use of the existing trait system. That means you can choose to use them in both boxed and unboxed forms. As we've been stabilizing APIs, we've been leaving anything that uses closures as |
I'm aware that we can still use boxed closures, but it seemed a rather bad idea to use a boxed closure for the
Since we have to stabilize (as in mark as
right? (FWIW, I prefer the latter option wherever possible) |
In this case |
Doesn't each call still has to go through the trait object tough? Or is LLVM able to optimize it to a function call (or maybe even better: inline it away)? |
Yes, but that's precisely what happens today (relying on LLVM to inline), as in all closures are boxed today. |
At worst, if you want to return an unboxed closure, you can write the struct by hand and not rely on the syntaxic sugar. It correspond to your |
Infer |
Can check off #17661. HRTB landed today. |
Just opened #19135, related to HRTBs and lifetimes on unboxed closures. |
An ICE related to rust-call: #16039 |
Given that only two issues remain, I don't think this metabug is particularly valuable anymore, so I'm gonna close it. |
fix: Fix inference of literals when the expectation is Castable I followed the compiler: https://github.com/rust-lang/rust/blob/5bce6d48ff09dcb2613278ec93013795718478ef/compiler/rustc_hir_typeck/src/fn_ctxt/checks.rs#L1560-L1579. Fixes rust-lang#18095.
This is a metabug for tracking work remaining to be done on unboxed closures. It contains only the high-level remaining issues. For a comprehensive list of closure bugs, search using the A-closures label.
1.0 Issues
Fn
traits be treated like everything in trait selectionFnOnce
FnMut
for&mut F
whereF: FnMut
Completed issues
Fn
traits an associated type #20871 -- Make return type of theFn
traits an associated typeFn
,FnMut
, etc #16640 -- Implement unboxed closure upvar inferencemove
when passing an unboxed closure to Fn* + Send #18799 -- Infermove
usingSend
trait|&mut:|
wants (Copy
) upvar to be mutable when it shouldn't #16749 --|&mut:|
wants (Copy
) upvar to be mutable when it shouldn'tFn
traits #19730 -- "perfect forwarding" traits are not accepted by HRTBSend
, but sugary version|&: ...|
does not #16560 -- Struct + Fn fulfillsSend
, but sugary version|&: ...|
does notref
-prefixed closures still capture by value #16814 -- Unboxed closures:ref
-prefixed closures still capture by valueBox<Fn(int) -> int + 'static>
is not accepted: "explicit lifetime bound required" #18772 --Box<Fn() + 'static>
not acceptedThe text was updated successfully, but these errors were encountered: