-
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
Opaque error message when typechecking closure #26046
Comments
Here's a user on Reddit who was confused by these error messages. This is a real problem. |
Replace consider_unification_despite_ambiguity with new obligation variant Is work towards #32730. Addresses part one of #32286. Addresses #24210 and #26046 to some degree. r? @nikomatsakis
This error message has gotten better:
Is it good enough? I'm not sure. |
I think it would be nice if the "derives from here" message pointed at the type or printed the type of the expression. This is certainly not necessary, though. |
I believe this will be fixed by #42196. |
Hm, looks like it wasn't -- @nikomatsakis you provided mentoring instructions for #42065; I expected that in the case of the code in the description of this issue it'd point at the
|
Yeah. Perhaps @tommyip would like to extend their previous work to cover this case! The short version is that, in #42196, we added the infrastructure to track why which variable we need to print, but we only print that information in one particular case (the case where the closure is being called twice). There are more errors where it'd be useful context (like this one). The relevant code for printing the error is here. We would need to add similar calls to the error-reporting code here. The tricky bit is that we have to find the right "tables" to get the data from. When this code runs, the |
I will have a look at this, are there any other places that would benefit from the tracked data? |
@nikomatsakis
If there is no valid table associated with the inference context it would just fallback to the existing message, ie:
|
Speaking for myself, I think that error message is great |
Use tracked data introduced in rust-lang#42196 to provide a better closure error message by showing why a closure implements `FnOnce`. ``` error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce` --> $DIR/issue_26046.rs:4:19 | 4 | let closure = move || { | ___________________^ 5 | | vec 6 | | }; | |_____^ | note: closure is `FnOnce` because it moves the variable `vec` out of its environment --> $DIR/issue_26046.rs:5:9 | 5 | vec | ^^^ error: aborting due to previous error(s) ``` Fixes rust-lang#26046
Better closure error message Use tracked data introduced in #42196 to provide a better closure error message by showing why a closure implements `FnOnce`. ``` error[E0525]: expected a closure that implements the `Fn` trait, but this closure only implements `FnOnce` --> $DIR/issue_26046.rs:4:19 | 4 | let closure = move || { | ___________________^ 5 | | vec 6 | | }; | |_____^ | note: closure is `FnOnce` because it moves the variable `vec` out of its environment --> $DIR/issue_26046.rs:5:9 | 5 | vec | ^^^ error: aborting due to previous error(s) ``` Fixes #26046 r? @nikomatsakis cc @doomrobo
UPDATE: Mentoring instructions found in this comment below.
The following code fails because the closure moves out of
vec
, and anything implementingFn
only gets a reference to its environment (i.e can't moveself
)A simple fix that would allow the closure to properly implement
Fn
is to replacevec
withvec.clone()
inside the closure. The error message, however, is of no help:In cases where the closure is more complex than this trivial example, the error message is no more descriptive than above, making it much more difficult to debug.
I would like to see something more along the lines of:
The text was updated successfully, but these errors were encountered: