summaryrefslogtreecommitdiffstats
path: root/tests/ui/consts/control-flow/drop-fail.rs
blob: 41341f3121e2bbcd5676fe258bc16c0ab30c59ad (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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
// revisions: stock precise

#![cfg_attr(precise, feature(const_precise_live_drops))]

// `x` is *not* always moved into the final value and may be dropped inside the initializer.
const _: Option<Vec<i32>> = {
    let y: Option<Vec<i32>> = None;
    let x = Some(Vec::new());
    //[stock,precise]~^ ERROR destructor of

    if true {
        x
    } else {
        y
    }
};

// We only clear `NeedsDrop` if a local is moved from in entirely. This is a shortcoming of the
// existing analysis.
const _: Vec<i32> = {
    let vec_tuple = (Vec::new(),);
    //[stock]~^ ERROR destructor of

    vec_tuple.0
};

// This applies to single-field enum variants as well.
const _: Vec<i32> = {
    let x: Result<_, Vec<i32>> = Ok(Vec::new());
    //[stock]~^ ERROR destructor of

    match x {
        Ok(x) | Err(x) => x,
    }
};

const _: Option<Vec<i32>> = {
    let mut some = Some(Vec::new());
    let mut tmp = None;
    //[stock,precise]~^ ERROR destructor of

    let mut i = 0;
    while i < 10 {
        tmp = some;
        some = None;

        // We can escape the loop with `Some` still in `tmp`,
        // which would require that it be dropped at the end of the block.
        if i > 100 {
            break;
        }

        some = tmp;
        tmp = None;

        i += 1;
    }

    some
};

fn main() {}