]> git.lizzy.rs Git - rust.git/blob - src/test/ui/hygiene/hygienic-labels-in-let.rs
Rollup merge of #90202 - matthewjasper:xcrate-hygiene, r=petrochenkov
[rust.git] / src / test / ui / hygiene / hygienic-labels-in-let.rs
1 // run-pass
2 #![allow(unreachable_code)]
3 #![allow(unused_labels)]
4
5 // Test that labels injected by macros do not break hygiene.  This
6 // checks cases where the macros invocations are under the rhs of a
7 // let statement.
8
9 // Issue #24278: The label/lifetime shadowing checker from #24162
10 // conservatively ignores hygiene, and thus issues warnings that are
11 // both true- and false-positives for this test.
12
13 macro_rules! loop_x {
14     ($e: expr) => {
15         // $e shouldn't be able to interact with this 'x
16         'x: loop { $e }
17         //~^ WARNING shadows a label name that is already in scope
18         //~| WARNING shadows a label name that is already in scope
19         //~| WARNING shadows a label name that is already in scope
20         //~| WARNING shadows a label name that is already in scope
21     }
22 }
23
24 macro_rules! while_true {
25     ($e: expr) => {
26         // $e shouldn't be able to interact with this 'x
27         'x: while 1 + 1 == 2 { $e }
28         //~^ WARNING shadows a label name that is already in scope
29         //~| WARNING shadows a label name that is already in scope
30         //~| WARNING shadows a label name that is already in scope
31         //~| WARNING shadows a label name that is already in scope
32         //~| WARNING shadows a label name that is already in scope
33     }
34 }
35
36 macro_rules! run_once {
37     ($e: expr) => {
38         // ditto
39         'x: for _ in 0..1 { $e }
40         //~^ WARNING shadows a label name that is already in scope
41         //~| WARNING shadows a label name that is already in scope
42         //~| WARNING shadows a label name that is already in scope
43         //~| WARNING shadows a label name that is already in scope
44         //~| WARNING shadows a label name that is already in scope
45         //~| WARNING shadows a label name that is already in scope
46         //~| WARNING shadows a label name that is already in scope
47     }
48 }
49
50 pub fn main() {
51     let mut i = 0;
52
53     let j: isize = {
54         'x: loop {
55             // this 'x should refer to the outer loop, lexically
56             loop_x!(break 'x);
57             i += 1;
58         }
59         i + 1
60     };
61     assert_eq!(j, 1);
62
63     let k: isize = {
64         'x: for _ in 0..1 {
65             //~^ WARNING shadows a label name that is already in scope
66             //~| WARNING shadows a label name that is already in scope
67             // ditto
68             loop_x!(break 'x);
69             i += 1;
70         }
71         i + 1
72     };
73     assert_eq!(k, 1);
74
75     let l: isize = {
76         'x: for _ in 0..1 {
77             //~^ WARNING shadows a label name that is already in scope
78             //~| WARNING shadows a label name that is already in scope
79             //~| WARNING shadows a label name that is already in scope
80             //~| WARNING shadows a label name that is already in scope
81             // ditto
82             while_true!(break 'x);
83             i += 1;
84         }
85         i + 1
86     };
87     assert_eq!(l, 1);
88
89     let n: isize = {
90         'x: for _ in 0..1 {
91             //~^ WARNING shadows a label name that is already in scope
92             //~| WARNING shadows a label name that is already in scope
93             //~| WARNING shadows a label name that is already in scope
94             //~| WARNING shadows a label name that is already in scope
95             //~| WARNING shadows a label name that is already in scope
96             //~| WARNING shadows a label name that is already in scope
97             // ditto
98             run_once!(continue 'x);
99             i += 1;
100         }
101         i + 1
102     };
103     assert_eq!(n, 1);
104 }