Skip to content

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

Open

Description

@AustinHellerRepo

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.

Activity

  1. added
    C-bug
    on Jan 27, 2025
  2. added
    needs-triage
    on Jan 27, 2025
  3. compiler-errors commented on Jan 27, 2025

    @compiler-errors
    Contributor

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

  4. added
    S-needs-info
    A-diagnostics
    T-compiler
    and removed
    C-bug
    needs-triage
    on Jan 27, 2025
  5. AustinHellerRepo commented on Jan 27, 2025

    @AustinHellerRepo
    Author

    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.

  6. added
    D-terse
    and removed
    S-needs-info
    on Jan 27, 2025
  7. fmease commented on Jan 27, 2025

    @fmease
    Member

    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.

  8. AustinHellerRepo commented on Jan 27, 2025

    @AustinHellerRepo
    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.

  9. estebank commented on Aug 12, 2025

    @estebank
    Contributor

    Current output, no change:

    error[E0308]: mismatched types
      --> src/main.rs:12:9
       |
     8 | impl<T> MyStruct<T>
       |      - expected this type parameter
     9 | {
    10 |     fn get_value(&self) -> Option<T> {
       |                            --------- expected `Option<T>` because of return type
    11 |         let locked_value = self.value.read().unwrap();
    12 |         locked_value.as_ref().map(|value| value.clone())
       |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `Option<T>`, found `Option<&T>`
       |
       = note: expected enum `Option<_>`
                  found enum `Option<&_>`
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsD-terseT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions