-
Notifications
You must be signed in to change notification settings - Fork 13.3k
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
Unspecific error on Fn trait bounds #115741
Comments
I believe the pattern that should be suggested there is something like this, using function pointers instead of a reference to a closure: pub type MyFn = fn() -> ();
fn do_f(f: MyFn) {}
fn main() {
let f = || {};
do_f(f);
} |
The issue here is that there is no automatic implementation for
I guess we could suggest a blanket implementation, but I'm somewhat skeptical of the value of that suggestion, since it heavily depends on what the user is trying to do with the trait (and the existence of other impls that the blanket impl would conflict with). |
This would only work if the closure doesn't capture anything from its environment. If it does, then the closure is a distinct type that cannot be coerced to a function pointer, and you'll get errors saying something like:
|
Hmm, good point. I might be wrong here as well, but I don't think there's a way to implement a trait on a specific closure, so the blanket impl would be the only way to make the exact usage from the original example work... but this could definitely subvert the intent of the trait itself. I know diagnostics don't usually suggest multiple options and this would be pretty verbose, but maybe the suggestion could present both options (blanket impl or change trait to function pointer type alias) with some discussion of the tradeoff? |
I don't think it makes sense in general to turn a I've added a note in #115741 that points out if a trait has no implementations, but it doesn't go the full way to suggesting how such a blanket impl would be written -- I fear that would be too "specific" for other cases. |
Point out if a local trait has no implementations Slightly helps with rust-lang#115741
Point out if a local trait has no implementations Slightly helps with rust-lang#115741
Point out if a local trait has no implementations Slightly helps with rust-lang#115741
Rollup merge of rust-lang#115743 - compiler-errors:no-impls, r=davidtwco Point out if a local trait has no implementations Slightly helps with rust-lang#115741
Copy, makes sense. Thanks! |
Code
Current output
Desired output
Some explanation of what part doesn't match or how a person might implement their intent would be helpful
Rationale and extra context
No response
Other cases
Anything else?
No response
The text was updated successfully, but these errors were encountered: