Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion compiler/rustc_hir_typeck/src/loops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,13 @@ impl<'hir> Visitor<'hir> for CheckLoopVisitor<'hir> {
}
}

let sp_lo = e.span.with_lo(e.span.lo() + BytePos("break".len() as u32));
let sp_lo = if let Ok(snippet) = self.tcx.sess.source_map().span_to_snippet(e.span)
&& let Some(break_pos) = snippet.find("break")
{
e.span.with_lo(e.span.lo() + BytePos((break_pos + "break".len()) as u32))
} else {
e.span.with_lo(e.span.lo() + BytePos("break".len() as u32))
};
let label_sp = match break_destination.label {
Some(label) => sp_lo.with_hi(label.ident.span.hi()),
None => sp_lo.shrink_to_lo(),
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/parser/break-in-unlabeled-block-parenthesized.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![allow(unused_parens)]
fn main() {
{
(break); //~ ERROR `break` outside of a loop or labeled block
};
{
((break)); //~ ERROR `break` outside of a loop or labeled block
};
}
27 changes: 27 additions & 0 deletions tests/ui/parser/break-in-unlabeled-block-parenthesized.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
error[E0268]: `break` outside of a loop or labeled block
--> $DIR/break-in-unlabeled-block-parenthesized.rs:4:9
|
LL | (break);
| ^^^^^^^ cannot `break` outside of a loop or labeled block
|
help: consider labeling this block to be able to break within it
|
LL ~ 'block: {
LL ~ (break 'block);
|

error[E0268]: `break` outside of a loop or labeled block
--> $DIR/break-in-unlabeled-block-parenthesized.rs:7:9
|
LL | ((break));
| ^^^^^^^^^ cannot `break` outside of a loop or labeled block
|
help: consider labeling this block to be able to break within it
|
LL ~ 'block: {
LL ~ ((break 'block));
|

error: aborting due to 2 previous errors

For more information about this error, try `rustc --explain E0268`.
Loading