Skip to content

Bad diagnostics using ? operator on Option<Cow<'_, str>> [E507] #116335

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Closed
djc opened this issue Oct 2, 2023 · 3 comments
Closed

Bad diagnostics using ? operator on Option<Cow<'_, str>> [E507] #116335

djc opened this issue Oct 2, 2023 · 3 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@djc
Copy link
Contributor

djc commented Oct 2, 2023

Code

use std::borrow::Cow;

struct Foo<'a> {
    list: Vec<Bar<'a>>,
}

impl<'a> Foo<'a> {
    fn bar(&self) -> Option<String> {
        Some(self.list.first()?.inner?.into_owned())
    }
}

struct Bar<'a> {
    inner: Option<Cow<'a, str>>,
}

Current output

error[E0507]: cannot move out of a shared reference
 --> src/lib.rs:9:14
  |
9 |         Some(self.list.first()?.inner?.into_owned())
  |              ^^^^^^^^^^^^^^^^^^^^^^^^-
  |              |
  |              value moved due to this method call
  |              move occurs because value has type `Option<Cow<'_, str>>`, which does not implement the `Copy` trait
  |
note: `branch` takes ownership of the receiver `self`, which moves value
 --> /rustc/d5c2e9c342b358556da91d61ed4133f6f50fc0c3/library/core/src/ops/try_trait.rs:217:15
help: you can `clone` the value and consume it, but this might not be your desired behavior
  |
9 |         Some(clone().self.list.first()?.inner?.into_owned())
  |              ++++++++

For more information about this error, try `rustc --explain E0507`.

Desired output

No response

Rationale and extra context

Pointing to some anonymous branch() method from (what I presume is) an impl for Try feels very unhelpful here. Note also the strange suggestion which wants me to add a bare clone() call before the expression...

Other cases

No response

Anything else?

No response

@djc djc added 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. labels Oct 2, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 2, 2023
@fmease fmease added A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 2, 2023
@fmease
Copy link
Member

fmease commented Oct 2, 2023

Thanks for the bug report. However, it's already fixed on beta and nightly. In the future, please double-check if your issue has already been fixed on nightly before opening an issue.

Closing as duplicate of #111016 (fixed by #112945).

@fmease fmease closed this as not planned Won't fix, can't repro, duplicate, stale Oct 2, 2023
@djc
Copy link
Contributor Author

djc commented Oct 2, 2023

Oops, sorry and thanks for following up!

@fmease
Copy link
Member

fmease commented Oct 2, 2023

All good 👍

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. 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

3 participants