When a variable is incorrectly used in a closure, error points to the source of the borrow, not the variable #81877
Labels
A-borrow-checker
Area: The borrow checker
A-closures
Area: Closures (`|…| { … }`)
A-diagnostics
Area: Messages for errors, warnings, and lints
A-lifetimes
Area: Lifetimes / regions
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
D-papercut
Diagnostics: An error or lint that needs small tweaks.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
Uh oh!
There was an error while loading. Please reload this page.
Unhelpful diagnostic. It's technically correct, but points to a variable that isn't the direct cause of the problem, obscuring the actual issue.
playground
The problem here (apart from #69350) is that the
source
argument is not used directly in the closure. Searching closure's code for thesource
variable won't find it.let source = source.to_owned()
will not help. In non-trivial functions this diagnostic is very puzzling, because it appears to complain about an unused/unrelated variable.Fixing this problem requires user to manually find and trace all places where the
source
arg has been borrowed, which could end up being in the closure. This may be difficult when borrowing is obscured by function calls or match ergonomics.The error should point to
actual_variable
. Instead of tracing the lifetime all the way to its origin, it would be more helpful to trace it to the binding that ended up captured by the closure:Another case:
complains about the
source
and the call toobscure
, which is wrong on both fronts, because the problem is aboutactual_variable
and the call tospawn
.The text was updated successfully, but these errors were encountered: