Skip to content

Commit cd63b22

Browse files
committed
Add more tests for label-break-value
1 parent c110cfa commit cd63b22

4 files changed

+208
-1
lines changed

src/test/ui/for-loop-while/label_break_value.rs

+52-1
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,50 @@ fn label_break_mixed(v: u32) -> u32 {
9595
r
9696
}
9797

98+
fn label_break_match(c: u8, xe: u8, ye: i8) {
99+
let mut x = 0;
100+
let y = 'a: {
101+
match c {
102+
0 => break 'a 0,
103+
v if { if v % 2 == 0 { break 'a 1; }; v % 3 == 0 } => { x += 1; },
104+
v if { 'b: { break 'b v == 5; } } => { x = 41; },
105+
_ => 'b: { //~ WARNING `'b` shadows a label
106+
break 'b ();
107+
},
108+
}
109+
x += 1;
110+
-1
111+
};
112+
113+
assert_eq!(x, xe);
114+
assert_eq!(y, ye);
115+
}
116+
117+
#[allow(unused_labels)]
118+
fn label_break_macro() {
119+
macro_rules! mac1 {
120+
($target:lifetime, $val:expr) => {
121+
break $target $val;
122+
};
123+
}
124+
let x: u8 = 'a: {
125+
'b: {
126+
mac1!('b, 1);
127+
};
128+
0
129+
};
130+
assert_eq!(x, 0);
131+
let x: u8 = 'a: { //~ WARNING `'a` shadows a label
132+
'b: { //~ WARNING `'b` shadows a label
133+
if true {
134+
mac1!('a, 1);
135+
}
136+
};
137+
0
138+
};
139+
assert_eq!(x, 1);
140+
}
141+
98142
pub fn main() {
99143
assert_eq!(label_break(true, false), 1);
100144
assert_eq!(label_break(false, true), 2);
@@ -112,5 +156,12 @@ pub fn main() {
112156
assert_eq!(label_break_mixed(5), 5);
113157
assert_eq!(label_break_mixed(6), 6);
114158

115-
// FIXME: ensure that labeled blocks work if produced by macros and in match arms
159+
label_break_match(0, 0, 0);
160+
label_break_match(1, 1, -1);
161+
label_break_match(2, 0, 1);
162+
label_break_match(3, 2, -1);
163+
label_break_match(5, 42, -1);
164+
label_break_match(7, 1, -1);
165+
166+
label_break_macro();
116167
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
warning: label name `'b` shadows a label name that is already in scope
2+
--> $DIR/label_break_value.rs:105:18
3+
|
4+
LL | v if { 'b: { break 'b v == 5; } } => { x = 41; },
5+
| -- first declared here
6+
LL | _ => 'b: {
7+
| ^^ label `'b` already in scope
8+
9+
warning: label name `'a` shadows a label name that is already in scope
10+
--> $DIR/label_break_value.rs:131:17
11+
|
12+
LL | let x: u8 = 'a: {
13+
| -- first declared here
14+
...
15+
LL | let x: u8 = 'a: {
16+
| ^^ label `'a` already in scope
17+
18+
warning: label name `'b` shadows a label name that is already in scope
19+
--> $DIR/label_break_value.rs:132:9
20+
|
21+
LL | 'b: {
22+
| -- first declared here
23+
...
24+
LL | 'b: {
25+
| ^^ label `'b` already in scope
26+
27+
warning: 3 warnings emitted
28+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#![crate_type = "lib"]
2+
#![feature(label_break_value)]
3+
4+
fn lbv_macro_test_hygiene_respected() {
5+
macro_rules! mac2 {
6+
($val:expr) => {
7+
break 'a $val; //~ ERROR undeclared label `'a` [E0426]
8+
};
9+
}
10+
let x: u8 = 'a: {
11+
'b: {
12+
if true {
13+
mac2!(2);
14+
}
15+
};
16+
0
17+
};
18+
assert_eq!(x, 2);
19+
20+
macro_rules! mac3 {
21+
($val:expr) => {
22+
'a: {
23+
//~^ WARNING `'a` shadows a label
24+
//~| WARNING `'a` shadows a label
25+
//~| WARNING `'a` shadows a label
26+
$val
27+
}
28+
};
29+
}
30+
let x: u8 = mac3!('b: { //~ WARNING `'b` shadows a label
31+
if true {
32+
break 'a 3; //~ ERROR undeclared label `'a` [E0426]
33+
}
34+
0
35+
});
36+
assert_eq!(x, 3);
37+
let x: u8 = mac3!(break 'a 4); //~ ERROR undeclared label `'a` [E0426]
38+
assert_eq!(x, 4);
39+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
error[E0426]: use of undeclared label `'a`
2+
--> $DIR/label_break_value_invalid.rs:7:19
3+
|
4+
LL | break 'a $val;
5+
| ^^ undeclared label `'a`
6+
...
7+
LL | mac2!(2);
8+
| -------- in this macro invocation
9+
|
10+
= note: this error originates in the macro `mac2` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error[E0426]: use of undeclared label `'a`
13+
--> $DIR/label_break_value_invalid.rs:32:19
14+
|
15+
LL | let x: u8 = mac3!('b: {
16+
| -- a label with a similar name is reachable
17+
LL | if true {
18+
LL | break 'a 3;
19+
| ^^
20+
| |
21+
| undeclared label `'a`
22+
| help: try using similarly named label: `'b`
23+
24+
error[E0426]: use of undeclared label `'a`
25+
--> $DIR/label_break_value_invalid.rs:37:29
26+
|
27+
LL | let x: u8 = mac3!(break 'a 4);
28+
| ^^ undeclared label `'a`
29+
30+
warning: label name `'a` shadows a label name that is already in scope
31+
--> $DIR/label_break_value_invalid.rs:22:13
32+
|
33+
LL | let x: u8 = 'a: {
34+
| -- first declared here
35+
...
36+
LL | 'a: {
37+
| ^^ label `'a` already in scope
38+
...
39+
LL | let x: u8 = mac3!('b: {
40+
| _________________-
41+
LL | | if true {
42+
LL | | break 'a 3;
43+
LL | | }
44+
LL | | 0
45+
LL | | });
46+
| |______- in this macro invocation
47+
|
48+
= note: this warning originates in the macro `mac3` (in Nightly builds, run with -Z macro-backtrace for more info)
49+
50+
warning: label name `'b` shadows a label name that is already in scope
51+
--> $DIR/label_break_value_invalid.rs:30:23
52+
|
53+
LL | 'b: {
54+
| -- first declared here
55+
...
56+
LL | let x: u8 = mac3!('b: {
57+
| ^^ label `'b` already in scope
58+
59+
warning: label name `'a` shadows a label name that is already in scope
60+
--> $DIR/label_break_value_invalid.rs:22:13
61+
|
62+
LL | let x: u8 = 'a: {
63+
| -- first declared here
64+
...
65+
LL | 'a: {
66+
| ^^ label `'a` already in scope
67+
...
68+
LL | let x: u8 = mac3!(break 'a 4);
69+
| ----------------- in this macro invocation
70+
|
71+
= note: this warning originates in the macro `mac3` (in Nightly builds, run with -Z macro-backtrace for more info)
72+
73+
warning: label name `'a` shadows a label name that is already in scope
74+
--> $DIR/label_break_value_invalid.rs:22:13
75+
|
76+
LL | 'a: {
77+
| ^^
78+
| |
79+
| first declared here
80+
| label `'a` already in scope
81+
...
82+
LL | let x: u8 = mac3!(break 'a 4);
83+
| ----------------- in this macro invocation
84+
|
85+
= note: this warning originates in the macro `mac3` (in Nightly builds, run with -Z macro-backtrace for more info)
86+
87+
error: aborting due to 3 previous errors; 4 warnings emitted
88+
89+
For more information about this error, try `rustc --explain E0426`.

0 commit comments

Comments
 (0)