`Drop` checking seems not working inside loop

Following code won't compile

struct BorrowWithDrop<'a> {
    s: &'a mut str,
}

impl<'a> BorrowWithDrop<'a> {
    fn new(s: &'a mut str) -> Self {
        Self { s }
    }
}

impl<'a> Drop for BorrowWithDrop<'a> {
    fn drop(&mut self) {}
}

fn main() {
    let mut s = String::new();
    let mut b = Some(BorrowWithDrop::new(&mut s));
    loop {
        drop(b);
        b = Some(BorrowWithDrop::new(&mut s));
    }
}

If there's no loop or no impl Drop, the code compiles. How should I work around this problem?

1 Like

I believe this is a known issue

I don't have any tips off the top of my head for workarounds, other than the drastic option of using ManuallyDrop and managing the drops yourself.

2 Likes

Thanks! Got my code to compile with ManuallyDrop. Actually didn't take much modification.