summaryrefslogtreecommitdiffstats
path: root/tests/ui/for-loop-while/loop-break-value.rs
blob: d7209fc4de867f168c5db50c403efc4ba33abf49 (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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// run-pass

#![allow(unreachable_code)]
#![feature(never_type)]

#[allow(unused)]
fn never_returns() {
    loop {
        break loop {};
    }
}

pub fn main() {
    let value = 'outer: loop {
        if 1 == 1 {
            break 13;
        } else {
            let _never: ! = loop {
                break loop {
                    break 'outer panic!();
                }
            };
        }
    };
    assert_eq!(value, 13);

    let x = [1, 3u32, 5];
    let y = [17];
    let z = [];
    let coerced: &[_] = loop {
        match 2 {
            1 => break &x,
            2 => break &y,
            3 => break &z,
            _ => (),
        }
    };
    assert_eq!(coerced, &[17u32]);

    let trait_unified = loop {
        break if true {
            break Default::default()
        } else {
            break [13, 14]
        };
    };
    assert_eq!(trait_unified, [0, 0]);

    let trait_unified_2 = loop {
        if false {
            break [String::from("Hello")]
        } else {
            break Default::default()
        };
    };
    assert_eq!(trait_unified_2, [""]);

    let trait_unified_3 = loop {
        break if false {
            break [String::from("Hello")]
        } else {
            ["Yes".into()]
        };
    };
    assert_eq!(trait_unified_3, ["Yes"]);

    let regular_break = loop {
        if true {
            break;
        } else {
            break break Default::default();
        }
    };
    assert_eq!(regular_break, ());

    let regular_break_2 = loop {
        if true {
            break Default::default();
        } else {
            break;
        }
    };
    assert_eq!(regular_break_2, ());

    let regular_break_3 = loop {
        break if true {
            Default::default()
        } else {
            break;
        }
    };
    assert_eq!(regular_break_3, ());

    let regular_break_4 = loop {
        break ();
        break;
    };
    assert_eq!(regular_break_4, ());

    let regular_break_5 = loop {
        break;
        break ();
    };
    assert_eq!(regular_break_5, ());

    let nested_break_value = 'outer2: loop {
        let _a: u32 = 'inner: loop {
            if true {
                break 'outer2 "hello";
            } else {
                break 'inner 17;
            }
        };
        panic!();
    };
    assert_eq!(nested_break_value, "hello");

    let break_from_while_cond = loop {
        'inner_loop: while break 'inner_loop {
            panic!();
        }
        break 123;
    };
    assert_eq!(break_from_while_cond, 123);

    let break_from_while_to_outer = 'outer_loop: loop {
        while break 'outer_loop 567 {
            panic!("from_inner");
        }
        panic!("from outer");
    };
    assert_eq!(break_from_while_to_outer, 567);

    let rust = true;
    let value = loop {
        break rust;
    };
    assert!(value);
}