-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Open
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.
Description
Given the following code:
trait Handler {}
struct Easy2<H: Handler>(H);
struct Context<'r>(Option<&'r [u8]>);
impl<'r> Handler for &'r mut Context<'r> {}
fn perform<'a>(ctx: &'a mut Context<'a>) {
Easy2(ctx);
}
fn build_response(_ctx: Context) {}
fn call() {
let mut ctx = Context(None);
perform(&mut ctx);
build_response(ctx);
}
Rust reports:
error[E0505]: cannot move out of `ctx` because it is borrowed
--> src/lib.rs:15:20
|
14 | perform(&mut ctx);
| -------- borrow of `ctx` occurs here
15 | build_response(ctx);
| ^^^
| |
| move out of `ctx` occurs here
| borrow later used here
This seems to be a confusing error, and doesn't help diagnosing where the problem is. The real fix here is to remove the lifetime annotations on perform
and have impl Handler
use different lifetimes for the two places, but this is totally unobvious from the error message.
It might be more helpful if it can point out that this is still borrowed because ctx
forms a self-reference due to <'a>(ctx: &'a mut Context<'a>)
and the field.
And it might also be useful to warn impl<'r> Handler for &'r mut Context<'r>
. I'm not sure whether a declaration like this where self-reference can arise has any legit use cases. It might be helpful to suggest people giving them two different lifetimes.
Metadata
Metadata
Assignees
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-diagnosticsArea: Messages for errors, warnings, and lintsArea: Messages for errors, warnings, and lintsA-lifetimesArea: Lifetimes / regionsArea: Lifetimes / regionsC-enhancementCategory: An issue proposing an enhancement or a PR with one.Category: An issue proposing an enhancement or a PR with one.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.Relevant to the compiler team, which will review and decide on the PR/issue.