-
Notifications
You must be signed in to change notification settings - Fork 13.6k
Closed
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-destructorsArea: Destructors (`Drop`, …)Area: Destructors (`Drop`, …)C-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.T-langRelevant to the language teamRelevant to the language team
Description
use std::rc::Rc;
use std::cell::{RefCell, RefMut};
use std::mem::drop;
fn huh(x: Rc<RefCell<i32>>) {
let mut rc: Rc<RefCell<i32>> = x.clone();
let mut inner: RefMut<'_, i32> = rc.borrow_mut();
loop {
drop(inner);
drop(rc);
rc = x.clone();
inner = rc.borrow_mut();
}
}
produces:
error[E0505]: cannot move out of `rc` because it is borrowed
--> src/lib.rs:9:14
|
6 | let mut inner: RefMut<'_, i32> = rc.borrow_mut();
| -- borrow of `rc` occurs here
...
9 | drop(rc);
| ^^ move out of `rc` occurs here
...
12 | inner = rc.borrow_mut();
| ----- borrow might be used here, when `inner` is dropped and runs the destructor for type `std::cell::RefMut<'_, i32>`
error[E0506]: cannot assign to `rc` because it is borrowed
--> src/lib.rs:11:9
|
6 | let mut inner: RefMut<'_, i32> = rc.borrow_mut();
| -- borrow of `rc` occurs here
...
11 | rc = x.clone();
| ^^ assignment to borrowed `rc` occurs here
12 | inner = rc.borrow_mut();
| ----- borrow might be used here, when `inner` is dropped and runs the destructor for type `std::cell::RefMut<'_, i32>`
(Same error with -Zpolonius
.)
The error messages are wrong: the assignment to inner
can never run a destructor, since the previous value of inner
was dropped earlier on.
And AFAICT this code is sound and ought to be accepted.
shepmaster, laurmaedje, maxbrunsfeld, GrizzlT, nshp and 4 more
Metadata
Metadata
Assignees
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerA-destructorsArea: Destructors (`Drop`, …)Area: Destructors (`Drop`, …)C-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.T-langRelevant to the language teamRelevant to the language team