-
-
Notifications
You must be signed in to change notification settings - Fork 14.2k
Closed
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerC-bugCategory: This is a bug.Category: This is a bug.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
This code won't compile
I tried this code:
#[derive(Debug)]
struct Node {
value: i32,
parent: RefCell<Weak<Node>>,
children: RefCell<Vec<Rc<Node>>>,
}
fn main() {
let leaf = Rc::new(Node {
value: 3,
parent: RefCell::new(Weak::new()),
children: RefCell::new(vec![]),
});
println!("leaf parent = {:?}", leaf.parent.borrow().upgrade());
let branch = Rc::new(Node {
value: 5,
parent: RefCell::new(Weak::new()),
children: RefCell::new(vec![Rc::clone(&leaf)]),
});
*leaf.parent.borrow_mut() = Rc::downgrade(&branch);
if let Some(x) = leaf.parent.borrow().upgrade() {
println!("leaf parent = {:?}", x);
}
}I expected to see this happen: code compiled
Instead, this happened: compiler error
a temporary with access to the borrow is created here ...rustcE0597
main.rs(35, 22): original diagnostic
`leaf` does not live long enough
borrowed value does not live long enoughrustcE0597
main.rs(38, 1): `leaf` dropped here while still borrowed
main.rs(35, 22): a temporary with access to the borrow is created here ...
main.rs(38, 1): ... and the borrow might be used here, when that temporary is dropped and runs the destructor for type `Ref<'_, std::rc::Weak<Node>>`
main.rs(37, 6): consider adding semicolon after the expression so its temporaries are dropped sooner, before the local variables declared by the block are dropped: `;`
Metadata
Metadata
Assignees
Labels
A-borrow-checkerArea: The borrow checkerArea: The borrow checkerC-bugCategory: This is a bug.Category: This is a bug.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.