Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upClosures should always implement the all applicable Fn* traits. #26085
Comments
sfackler
added
the
A-closures
label
Jun 7, 2015
This comment has been minimized.
This comment has been minimized.
|
This bug has an observable effect with specialization too. Edited: I pasted the wrong code in my first attempt. // This prints "default apply"
// rustc 1.9.0-nightly (c66d2380a 2016-03-15)
#![feature(specialization)]
pub fn apply<F>(f: F)
where F: FnMut()
{
<()>::apply(f);
}
trait Apply<F> {
fn apply(f: F);
}
impl<F> Apply<F> for ()
where F: FnMut()
{
default fn apply(f: F) {
println!("default apply")
}
}
impl<F> Apply<F> for ()
where F: Fn()
{
fn apply(f: F) {
println!("thread safe apply")
}
}
fn main() {
apply(|| {});
}
|
This comment has been minimized.
This comment has been minimized.
|
I don't know off-hand if this is a "mere compiler bug" or a deep language specification issue. I'm going to throw it onto the compiler teams plate for the short term, trusting that the team can move it to lang if that's what makes sense. |
This comment has been minimized.
This comment has been minimized.
|
triage: I-nominated T-compiler |
rust-highfive
added
the
I-nominated
label
Mar 17, 2016
pnkfelix
added
the
T-compiler
label
Mar 17, 2016
This comment has been minimized.
This comment has been minimized.
|
So this was intentional, but not necessarily wise. It helped us to get closures up and going, and I left it in as a useful "shorcut" to the right answer for closure inference. It is also the only way to influence closure trait selection, should you want to do so (but that last part is bad; we should probably have some explicit syntax at some point). So my feeling is we should try commenting the "expected type inference" out and see what breaks. That is probably fairly easy to do. Hopefully, things will "just work" -- but there may be corner cases that will start to fail, I'm not sure. |
This comment has been minimized.
This comment has been minimized.
|
triage: P-medium |
rust-highfive
added
P-medium
and removed
I-nominated
labels
Mar 17, 2016
brson
added
the
C-enhancement
label
Oct 20, 2016
This comment has been minimized.
This comment has been minimized.
|
Could be P-low? |
This comment has been minimized.
This comment has been minimized.
|
Triage: not aware of any changes here |
timvermeulen
referenced this issue
Feb 18, 2019
Closed
Empty closure doesn't implement Fn when passed directly as FnMut argument #58424
This comment has been minimized.
This comment has been minimized.
This might make a good |
cuviper
referenced this issue
Apr 4, 2019
Closed
`-> impl Fn()` fails with closures used as `FnMut` but actually Fn #59022
This comment has been minimized.
This comment has been minimized.
|
I've done a bit of experimenting with this, and it seems like just dropping the expected kind here works without issues for
It seems like the closure kind inference does not account for mutable references being returned from the closure, so it ends up inferring a less strict kind than necessary. |
Stebalien commentedJun 7, 2015
If a closure is passed into a function call as temporary, it only implements the
Fn*traits needed to satisfy the constraints specified by the function. However, if it is declared first and then moved into the function, it correctly implements all applicableFn*traits.