Misleading diagnostics when owning Option is used instead of an Option with a reference #43861
Labels
A-borrow-checker
Area: The borrow checker
A-diagnostics
Area: Messages for errors, warnings, and lints
C-enhancement
Category: An issue proposing an enhancement or a PR with one.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
There's an unfortunate effect of type-checking done before borrow checking, when the root of the type error is due to use of owned value instead of a reference.
Specifically, common uses of
.map()
/.and_then()
/if let Some()
on&Option<T: !Copy>
are likely to fail with a misleading error.When the inner value is used as if it was a reference it causes an error that is technically correct, but ultimately unhelpful and misleading:
This hint is wrong, because actually using
&arg
gives the dreaded:and this error doesn't have any suggestion how to proceed, so it's a scary dead-end for someone struggling with the borrow checker.
The correct solution is to suggest calling
.as_ref()
onOption
. Here's an example:I find it to be a big problem, because in Rust it's easy to end up with
&Option<T>
. To a novice it's not obvious that it can be converted toOption<&T>
that works, and Rust doesn't suggest that solution.Due to borrow checker depending on type-checking it's probably impossible to improve this diagnostic in general case, but I suggest at least special-casing it for
Option
(suggesting.as_ref()
), since it's such a common pitfall.The text was updated successfully, but these errors were encountered: