-
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 call implementations of the Fn*
traits on references
#42736
Comments
cc @nikomatsakis Sounds like the overloaded call derefs too eagerly? Or is missing autoref? |
Fn*
traits for referencesFn*
traits on references
I am the one that posted the relevant StackOverflow question. I found some more example: trait T1 {
}
trait T2 {
}
impl<'a> T1 for &'a T2 {
}
struct S {
}
impl T2 for S {
}
fn main() {
let t2:&T2 = &S {};
let t1:&T1 = &t2; //This is OK
let t3:&T1 = t2; //E0308: Expecting `T1`, found `T2`
} Note the above does not require any features and behave the same in all channels. So, there has nothing to do with closures and |
In the simplified example, I tried to defense the current compiler behavior. The statement
On the other hand,
The difference here is we now have an expression, so it have to be calculated. The type checker will make sure the result is a reference to So, although counter intuitive, but the current compiler behavior is not wrong. I think to achieve the designated usage, what we want to do is not implement a trait on top of a trait object, but to implement the conversion traits instead (already in my to-study list), so the compiler can apply the conversion automatically. |
I'm not sure what's going wrong. Have to trace through it I guess. I added this bug as a blocker to stabilizing. Going to call it P-medium. triage: P-medium |
Progress on this lately? :-) |
Are we in a good place to tackle this now that #45510 has been solved? |
One more example that works: fn repro_ref(thing: impl FnOnce() -> ()) {
thing();
} |
@elycruz That's missing the reference though, this OTOH does error: fn repro_ref(thing: &impl FnOnce()) {
thing();
} Interestingly, the error is different with a generic like that (it doesn't have to be (Btw, your link is just to the playground, not to your example - you need to press the |
@eddyb You're right (gotcha 👍 (updated my link 👍)). |
Any news? If used wisely, generic callable "objects" can make for some very elegant APIs -- however here's a playground example of how this is still not possible. |
That code is simply wrong though. |
Longer repro with current Nightly:
I think the compiler isn't doing some autoref that it would for method despatch. Is this actually a blocking issue for #29625 (stabilising impl Fn) though? It can only bite you if you do something like |
i think that if people really cared about this, it would have been fixed by now. can we just close this issue and make fn_traits stable. |
Attempting to implement
FnOnce
for a reference to a struct or a trait object:Produces the error:
Interestingly, both of these alternatives work:
cc #29625.
Originally from this Stack Overflow question.
The text was updated successfully, but these errors were encountered: