Skip to content

Undetected use after move #18571

Closed
Closed
@pepijndevos

Description

@pepijndevos

So I was curious how Drop handles cycles, because this could lead to dangling pointers, which is why __del__ might not be called in Python. And indeed the below example does not call Drop.

But t is moved into u, so why does this compile at all?

struct Test {
    a: int,
    b: Option<Box<Test>>,
}

impl Drop for Test {
    fn drop(&mut self) {
        println!("Dropping {}", self.a);
    }
}

fn stuff() {
    let mut t = Test { a: 1, b: None};
    let mut u = Test { a: 2, b: Some(box t)};    
    t.b = Some(box u);
    println!("done");
}

fn main() {
    stuff();
    println!("Hello, world!")
}

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions