Skip to content

Surprising internals in TryTrait revealed in diagnostic #112939

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

Open
jbr opened this issue Jun 22, 2023 · 2 comments
Open

Surprising internals in TryTrait revealed in diagnostic #112939

jbr opened this issue Jun 22, 2023 · 2 comments
Labels
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.

Comments

@jbr
Copy link
Contributor

jbr commented Jun 22, 2023

Code

pub fn for_aggregator(client: Client, aggregator: Aggregator) -> Option<Self> {
        Some(Self {
            client,
            base_url: aggregator.api_url?.clone().into(),
            auth_header: HeaderValue::from(format!("Bearer {}", aggregator.bearer_token?.clone())),
            aggregator,
        })
    }

Current output

error[E0382]: use of partially moved value: `aggregator`
   --> src/clients/aggregator_client.rs:33:13
    |
32  | ...   auth_header: HeaderValue::from(format!("Bearer {}", aggregator.bearer_token?.cl...
    |                                                           ------------------------ `aggregator.bearer_token` partially moved due to this method call
33  | ...   aggregator,
    |       ^^^^^^^^^^ value used here after partial move
    |
note: `branch` takes ownership of the receiver `self`, which moves `aggregator.bearer_token`
   --> /Users/jbr/.rustup/toolchains/nightly-x86_64-apple-darwin/lib/rustlib/src/rust/library/core/src/ops/try_trait.rs:217:15
    |
217 |     fn branch(self) -> ControlFlow<Self::Residual, Self::Output>;
    |               ^^^^
    = note: partial move occurs because `aggregator.bearer_token` has type `std::option::Option<std::string::String>`, which does not implement the `Copy` trait
help: you can `clone` the value and consume it, but this might not be your desired behavior
    |
32  |             auth_header: HeaderValue::from(format!("Bearer {}", clone().aggregator.bearer_token?.clone())),
    |                                                                 ++++++++

Some errors have detailed explanations: E0382, E0507.
For more information about an error, try `rustc --explain E0382`.
error: could not compile `divviup-api` (lib) due to 2 previous errors

Desired output

error[E0382]: use of partially moved value: `aggregator`
   --> src/clients/aggregator_client.rs:33:13
    |
32  | ...   auth_header: HeaderValue::from(format!("Bearer {}", aggregator.bearer_token?.cl...
    |                                                           ------------------------ `aggregator.bearer_token` partially moved due to this method call
33  | ...   aggregator,
    |       ^^^^^^^^^^ value used here after partial move
    |
    = note: partial move occurs because `aggregator.bearer_token` has type `std::option::Option<std::string::String>`, which does not implement the `Copy` trait
help: you can `clone` the value and consume it, but this might not be your desired behavior
    |
32  |             auth_header: HeaderValue::from(format!("Bearer {}", aggregator.bearer_token?.clone())),
    |                                                                                         ++++++++

Some errors have detailed explanations: E0382, E0507.
For more information about an error, try `rustc --explain E0382`.
error: could not compile `divviup-api` (lib) due to 2 previous errors

Rationale and extra context

The fn branch(self) -> ControlFlow<Self::Residual, Self::Output> content is unhelpful as it's internal to ?, as is the confusing and invalid suggestion of clone().aggregator.bearer_token?.clone().

Other cases

No response

Anything else?

In this case, as_deref()/as_ref() was actually the preferable change to clone, but that's probably a distinct diagnostic concern

@jbr jbr 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 Jun 22, 2023
@compiler-errors
Copy link
Member

Do you mind minimizing this example to something that actually demonstrates the issue on https://play.rust-lang.org ?

@compiler-errors
Copy link
Member

Also, the bad clone suggestion is #111016, claimed it a while back and it fell off my radar -- I'll work on that.

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

No branches or pull requests

2 participants