-
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
Activity
MightyPork commentedon Jan 27, 2022
Similar issue in current rustc, my code involves std RwLock and match
the error makes no sense, i drop it manually right above. I can't find a way to subjugate the compiler.
cole-miller commentedon Jul 27, 2022
Probably the same underlying issue affects this example (playground):
This compiles if you remove the
Drop
impl. I believe the error is spurious because the destructor forone
never runs -- that value is consumed by theone.step()
call.Aaron1011 commentedon Sep 19, 2022
Minimized:
Drop
local has an assignment in a loop #102078Rollup merge of rust-lang#108780 - Zeegomo:close-70919, r=WaffleLapkin
Rollup merge of rust-lang#108780 - Zeegomo:close-70919, r=WaffleLapkin
Remove `ManuallyDrop` usage
ManuallyDrop
usage typst/typst#2058Remove `ManuallyDrop` usage (#2058)
Downgrade bincode