Misleading error message - Returning Option<T> when Clone not implemented #136151
Labels
A-diagnostics
Area: Messages for errors, warnings, and lints
D-terse
Diagnostics: An error or lint that doesn't give enough information about the problem at hand.
T-compiler
Relevant to the compiler team, which will review and decide on the PR/issue.
When a function returns
Option<T>
and the function is basically a wrapper on anArc<RwLock<Option<T>>>
butT
does not implementClone
then the codeself.value.read().unwrap().as_ref().map(|value| value.clone())
will show a compile error"mismatched types"
as the return type isOption<_> and found Option<&_>
. This suggests that I need to clone the value, but I am already doing that. Trying to dereference the value also fails with the error that theT
does not implementCopy
(nor do I want it to).Example:
https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=9ab38c00a0a33a5c07b2aa42fa510762
I expected to see this happen:
The underlying issue of
"no method named clone found for type"
is the true underlying problem that is hidden from the developer.Instead, this happened:
I was misdirected to the return type being invalid.
I was under the impression that bad or misleading error messages in Rust are considered bugs, but if that is not the case please let me know what I need to do to help resolve this issue. Thank you.
Edit:
While constructing the more complete example, I noticed that omitting the
.as_ref()
then caused the compiler to properly call out the lack ofClone
. When I addedT: Clone
then an error would appear since I was missing.as_ref()
. This bug demonstrates that if you already start with.as_ref()
included, that the compiler fails to point you to the lack ofClone
.The text was updated successfully, but these errors were encountered: