about summary refs log tree commit diff
path: root/tests/mir-opt/box_conditional_drop_allocator.rs
blob: 9471be14c8770b0f437beba9b09c7b5b9a1aa404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// skip-filecheck
//@ test-mir-pass: ElaborateDrops
//@ needs-unwind
#![feature(allocator_api)]

// Regression test for #131082.
// Testing that the allocator of a Box is dropped in conditional drops

use std::alloc::{AllocError, Allocator, Global, Layout};
use std::ptr::NonNull;

struct DropAllocator;

unsafe impl Allocator for DropAllocator {
    fn allocate(&self, layout: Layout) -> Result<NonNull<[u8]>, AllocError> {
        Global.allocate(layout)
    }
    unsafe fn deallocate(&self, ptr: NonNull<u8>, layout: Layout) {
        Global.deallocate(ptr, layout);
    }
}
impl Drop for DropAllocator {
    fn drop(&mut self) {}
}

struct HasDrop;
impl Drop for HasDrop {
    fn drop(&mut self) {}
}

// EMIT_MIR box_conditional_drop_allocator.main.ElaborateDrops.diff
fn main() {
    let b = Box::new_in(HasDrop, DropAllocator);
    if true {
        drop(*b);
    } else {
        drop(b);
    }
}