Skip to content
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

Misleading error message - Returning Option<T> when Clone not implemented #136151

Open
AustinHellerRepo opened this issue Jan 27, 2025 · 4 comments
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.

Comments

@AustinHellerRepo
Copy link

AustinHellerRepo commented Jan 27, 2025

When a function returns Option<T> and the function is basically a wrapper on an Arc<RwLock<Option<T>>> but T does not implement Clone then the code self.value.read().unwrap().as_ref().map(|value| value.clone()) will show a compile error "mismatched types" as the return type is Option<_> 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 the T does not implement Copy (nor do I want it to).

Example:

struct MyStruct<T>
{
    value: Arc<RwLock<Option<T>>>,
}

impl<T> MyStruct<T>
{
    fn get_value(&self) -> Option<T> {
        let locked_value = self.value.read().unwrap();
        locked_value.as_ref().map(|value| value.clone())
    }
}

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 of Clone. When I added T: 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 of Clone.

@AustinHellerRepo AustinHellerRepo added the C-bug Category: This is a bug. label Jan 27, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Jan 27, 2025
@compiler-errors
Copy link
Member

You should probably provide a more complete code sample which demonstrates the bug.

@fmease fmease added S-needs-info Status: The issue lacks details necessary to triage or act on it. A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Jan 27, 2025
@AustinHellerRepo
Copy link
Author

AustinHellerRepo commented Jan 27, 2025

You should probably provide a more complete code sample which demonstrates the bug.

Sure thing. I have updated the original example accordingly. Please let me know if this is still unclear or incomplete.

@fmease fmease added D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. and removed S-needs-info Status: The issue lacks details necessary to triage or act on it. labels Jan 27, 2025
@fmease
Copy link
Member

fmease commented Jan 27, 2025

This looks like a more complicated variant of the closed issue #95535 (fixed by #95585).
See also closed issue #91532 (fixed by #118076) and closed issue #121524 (fixed by #122174).

Lastly, CC the open issue #91536 and the open tracking issue #83099.

@AustinHellerRepo
Copy link
Author

So long as there is at least one open issue for this problem, it makes sense to close this one. Thanks for reviewing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
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.
Projects
None yet
Development

No branches or pull requests

4 participants