]> git.lizzy.rs Git - rust.git/blob - src/test/run-pass/hygienic-labels-in-let.rs
Auto merge of #28816 - petrochenkov:unistruct, r=nrc
[rust.git] / src / test / run-pass / hygienic-labels-in-let.rs
1 // Copyright 2012-2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 // ignore-pretty: pprust doesn't print hygiene output
12
13 // Test that labels injected by macros do not break hygiene.  This
14 // checks cases where the macros invocations are under the rhs of a
15 // let statement.
16
17 // Issue #24278: The label/lifetime shadowing checker from #24162
18 // conservatively ignores hygiene, and thus issues warnings that are
19 // both true- and false-positives for this test.
20
21 macro_rules! loop_x {
22     ($e: expr) => {
23         // $e shouldn't be able to interact with this 'x
24         'x: loop { $e }
25     }
26 }
27
28 macro_rules! while_true {
29     ($e: expr) => {
30         // $e shouldn't be able to interact with this 'x
31         'x: while 1 + 1 == 2 { $e }
32     }
33 }
34
35 macro_rules! run_once {
36     ($e: expr) => {
37         // ditto
38         'x: for _ in 0..1 { $e }
39     }
40 }
41
42 pub fn main() {
43     let mut i = 0;
44
45     let j: isize = {
46         'x: loop {
47             // this 'x should refer to the outer loop, lexically
48             loop_x!(break 'x);
49             i += 1;
50         }
51         i + 1
52     };
53     assert_eq!(j, 1);
54
55     let k: isize = {
56         'x: for _ in 0..1 {
57             // ditto
58             loop_x!(break 'x);
59             i += 1;
60         }
61         i + 1
62     };
63     assert_eq!(k, 1);
64
65     let l: isize = {
66         'x: for _ in 0..1 {
67             // ditto
68             while_true!(break 'x);
69             i += 1;
70         }
71         i + 1
72     };
73     assert_eq!(l, 1);
74
75     let n: isize = {
76         'x: for _ in 0..1 {
77             // ditto
78             run_once!(continue 'x);
79             i += 1;
80         }
81         i + 1
82     };
83     assert_eq!(n, 1);
84 }