Skip to content

Commit d23dc20

Browse files
committed
Account for macros
1 parent c30d57b commit d23dc20

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed

compiler/rustc_passes/src/loops.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
231231
AsyncClosure(closure_span) => {
232232
self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name });
233233
}
234-
UnlabeledBlock(block_span) if is_break => {
234+
UnlabeledBlock(block_span) if is_break && block_span.ctxt() == break_span.ctxt() => {
235235
let suggestion = Some(OutsideLoopSuggestion { block_span, break_span });
236236
self.sess.emit_err(OutsideLoop { span, name, is_break, suggestion });
237237
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
macro_rules! foo {
2+
() => {
3+
break (); //~ ERROR `break` outside of a loop or labeled block
4+
};
5+
($e: expr) => {
6+
break $e; //~ ERROR `break` outside of a loop or labeled block
7+
};
8+
(stmt $s: stmt) => {
9+
$s
10+
};
11+
(@ $e: expr) => {
12+
{ break $e; } //~ ERROR `break` outside of a loop or labeled block
13+
};
14+
(=> $s: stmt) => {
15+
{ $s }
16+
};
17+
}
18+
19+
fn main() {
20+
{
21+
foo!();
22+
}
23+
{
24+
foo!(());
25+
}
26+
{
27+
foo!(stmt break ()); //~ ERROR `break` outside of a loop or labeled block
28+
}
29+
{
30+
foo!(@ ());
31+
}
32+
{
33+
foo!(=> break ()); //~ ERROR `break` outside of a loop or labeled block
34+
}
35+
{
36+
macro_rules! bar {
37+
() => {
38+
break () //~ ERROR `break` outside of a loop or labeled block
39+
};
40+
}
41+
bar!()
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
error[E0268]: `break` outside of a loop or labeled block
2+
--> $DIR/break-in-unlabeled-block-in-macro.rs:3:9
3+
|
4+
LL | break ();
5+
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
6+
...
7+
LL | foo!();
8+
| ------ in this macro invocation
9+
|
10+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
11+
12+
error[E0268]: `break` outside of a loop or labeled block
13+
--> $DIR/break-in-unlabeled-block-in-macro.rs:6:9
14+
|
15+
LL | break $e;
16+
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
17+
...
18+
LL | foo!(());
19+
| -------- in this macro invocation
20+
|
21+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
22+
23+
error[E0268]: `break` outside of a loop or labeled block
24+
--> $DIR/break-in-unlabeled-block-in-macro.rs:27:19
25+
|
26+
LL | foo!(stmt break ());
27+
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
28+
|
29+
help: consider labeling this block to be able to break within it
30+
|
31+
LL ~ 'block: {
32+
LL ~ foo!(stmt break 'block ());
33+
|
34+
35+
error[E0268]: `break` outside of a loop or labeled block
36+
--> $DIR/break-in-unlabeled-block-in-macro.rs:12:11
37+
|
38+
LL | { break $e; }
39+
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
40+
...
41+
LL | foo!(@ ());
42+
| ---------- in this macro invocation
43+
|
44+
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
45+
help: consider labeling this block to be able to break within it
46+
|
47+
LL | 'block: { break 'block $e; }
48+
| +++++++ ++++++
49+
50+
error[E0268]: `break` outside of a loop or labeled block
51+
--> $DIR/break-in-unlabeled-block-in-macro.rs:33:17
52+
|
53+
LL | foo!(=> break ());
54+
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
55+
56+
error[E0268]: `break` outside of a loop or labeled block
57+
--> $DIR/break-in-unlabeled-block-in-macro.rs:38:17
58+
|
59+
LL | break ()
60+
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
61+
...
62+
LL | bar!()
63+
| ------ in this macro invocation
64+
|
65+
= note: this error originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info)
66+
67+
error: aborting due to 6 previous errors
68+
69+
For more information about this error, try `rustc --explain E0268`.

0 commit comments

Comments
 (0)