Not sufficiently useful error message when FnMut is needed instead of Fn #31701
Labels
A-closures
Area: Closures (`|…| { … }`)
A-diagnostics
Area: Messages for errors, warnings, and lints
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
D-confusing
Diagnostics: Confusing error or lint that should be reworked.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
This code:
results in the following error messages in stable:
The problem here is that we're using
Fn()
instead ofFnMut()
, but the phrase "consider changing this closure to take self by mutable reference" is not particularly useful in telling us that that specifically is the problem. That is, the fact that we cannot assign to the captured outer variable is because we are trying to returnBox<Fn() -> usize>
instead ofBox<FnMut() -> usize>
, but that's not immediately clear from the original error message.I'm not entirely sure how this would be rectified, but at the very least mentioning
FnMut
somewhere would be useful. @eddyb mentioned that this issue was "because a bound/trait object ofFn()
was used but onlyFnMut()
allows mutable borrows", so I think some variation on that would be reasonably sufficient to clarify the error.The text was updated successfully, but these errors were encountered: