Skip to content

Commit

Permalink
Auto merge of rust-lang#116366 - estebank:issue-103982, r=oli-obk
Browse files Browse the repository at this point in the history
Suggest labeling block if `break` is in bare block

Fix rust-lang#103982.
  • Loading branch information
bors committed Oct 10, 2023
2 parents fa6d1e7 + d23dc20 commit 84d44dd
Show file tree
Hide file tree
Showing 30 changed files with 220 additions and 8 deletions.
2 changes: 2 additions & 0 deletions compiler/rustc_passes/messages.ftl
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,8 @@ passes_outside_loop =
*[false] {""}
}
passes_outside_loop_suggestion = consider labeling this block to be able to break within it
passes_params_not_allowed =
referencing function parameters is not allowed in naked functions
.help = follow the calling convention in asm block to use parameters
Expand Down
10 changes: 10 additions & 0 deletions compiler/rustc_passes/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,6 +1099,16 @@ pub struct OutsideLoop<'a> {
pub span: Span,
pub name: &'a str,
pub is_break: bool,
#[subdiagnostic]
pub suggestion: Option<OutsideLoopSuggestion>,
}
#[derive(Subdiagnostic)]
#[multipart_suggestion(passes_outside_loop_suggestion, applicability = "maybe-incorrect")]
pub struct OutsideLoopSuggestion {
#[suggestion_part(code = "'block: ")]
pub block_span: Span,
#[suggestion_part(code = " 'block")]
pub break_span: Span,
}

#[derive(Diagnostic)]
Expand Down
55 changes: 47 additions & 8 deletions compiler/rustc_passes/src/loops.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use Context::*;

use rustc_hir as hir;
use rustc_hir::def_id::LocalModDefId;
use rustc_hir::def_id::{LocalDefId, LocalModDefId};
use rustc_hir::intravisit::{self, Visitor};
use rustc_hir::{Destination, Movability, Node};
use rustc_middle::hir::map::Map;
Expand All @@ -10,19 +10,21 @@ use rustc_middle::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_session::Session;
use rustc_span::hygiene::DesugaringKind;
use rustc_span::Span;
use rustc_span::{BytePos, Span};

use crate::errors::{
BreakInsideAsyncBlock, BreakInsideClosure, BreakNonLoop, ContinueLabeledBlock, OutsideLoop,
UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock,
OutsideLoopSuggestion, UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock,
};

#[derive(Clone, Copy, Debug, PartialEq)]
enum Context {
Normal,
Fn,
Loop(hir::LoopSource),
Closure(Span),
AsyncClosure(Span),
UnlabeledBlock(Span),
LabeledBlock,
Constant,
}
Expand Down Expand Up @@ -60,6 +62,25 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
self.with_context(Constant, |v| intravisit::walk_inline_const(v, c));
}

fn visit_fn(
&mut self,
fk: hir::intravisit::FnKind<'hir>,
fd: &'hir hir::FnDecl<'hir>,
b: hir::BodyId,
_: Span,
id: LocalDefId,
) {
self.with_context(Fn, |v| intravisit::walk_fn(v, fk, fd, b, id));
}

fn visit_trait_item(&mut self, trait_item: &'hir hir::TraitItem<'hir>) {
self.with_context(Fn, |v| intravisit::walk_trait_item(v, trait_item));
}

fn visit_impl_item(&mut self, impl_item: &'hir hir::ImplItem<'hir>) {
self.with_context(Fn, |v| intravisit::walk_impl_item(v, impl_item));
}

fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
match e.kind {
hir::ExprKind::Loop(ref b, _, source, _) => {
Expand All @@ -83,6 +104,14 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
hir::ExprKind::Block(ref b, Some(_label)) => {
self.with_context(LabeledBlock, |v| v.visit_block(&b));
}
hir::ExprKind::Block(ref b, None) if matches!(self.cx, Fn) => {
self.with_context(Normal, |v| v.visit_block(&b));
}
hir::ExprKind::Block(ref b, None)
if matches!(self.cx, Normal | Constant | UnlabeledBlock(_)) =>
{
self.with_context(UnlabeledBlock(b.span.shrink_to_lo()), |v| v.visit_block(&b));
}
hir::ExprKind::Break(break_label, ref opt_expr) => {
if let Some(e) = opt_expr {
self.visit_expr(e);
Expand Down Expand Up @@ -147,7 +176,12 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
}
}

self.require_break_cx("break", e.span);
let sp_lo = e.span.with_lo(e.span.lo() + BytePos("break".len() as u32));
let label_sp = match break_label.label {
Some(label) => sp_lo.with_hi(label.ident.span.hi()),
None => sp_lo.shrink_to_lo(),
};
self.require_break_cx("break", e.span, label_sp);
}
hir::ExprKind::Continue(destination) => {
self.require_label_in_labeled_block(e.span, &destination, "continue");
Expand All @@ -169,7 +203,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
}
Err(_) => {}
}
self.require_break_cx("continue", e.span)
self.require_break_cx("continue", e.span, e.span)
}
_ => intravisit::walk_expr(self, e),
}
Expand All @@ -187,7 +221,8 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
self.cx = old_cx;
}

fn require_break_cx(&self, name: &str, span: Span) {
fn require_break_cx(&self, name: &str, span: Span, break_span: Span) {
let is_break = name == "break";
match self.cx {
LabeledBlock | Loop(_) => {}
Closure(closure_span) => {
Expand All @@ -196,8 +231,12 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
AsyncClosure(closure_span) => {
self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name });
}
Normal | Constant => {
self.sess.emit_err(OutsideLoop { span, name, is_break: name == "break" });
UnlabeledBlock(block_span) if is_break && block_span.ctxt() == break_span.ctxt() => {
let suggestion = Some(OutsideLoopSuggestion { block_span, break_span });
self.sess.emit_err(OutsideLoop { span, name, is_break, suggestion });
}
Normal | Constant | Fn | UnlabeledBlock(_) => {
self.sess.emit_err(OutsideLoop { span, name, is_break, suggestion: None });
}
}
}
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
43 changes: 43 additions & 0 deletions tests/ui/parser/break-in-unlabeled-block-in-macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
macro_rules! foo {
() => {
break (); //~ ERROR `break` outside of a loop or labeled block
};
($e: expr) => {
break $e; //~ ERROR `break` outside of a loop or labeled block
};
(stmt $s: stmt) => {
$s
};
(@ $e: expr) => {
{ break $e; } //~ ERROR `break` outside of a loop or labeled block
};
(=> $s: stmt) => {
{ $s }
};
}

fn main() {
{
foo!();
}
{
foo!(());
}
{
foo!(stmt break ()); //~ ERROR `break` outside of a loop or labeled block
}
{
foo!(@ ());
}
{
foo!(=> break ()); //~ ERROR `break` outside of a loop or labeled block
}
{
macro_rules! bar {
() => {
break () //~ ERROR `break` outside of a loop or labeled block
};
}
bar!()
}
}
69 changes: 69 additions & 0 deletions tests/ui/parser/break-in-unlabeled-block-in-macro.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
error[E0268]: `break` outside of a loop or labeled block
--> $DIR/break-in-unlabeled-block-in-macro.rs:3:9
|
LL | break ();
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
...
LL | foo!();
| ------ in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0268]: `break` outside of a loop or labeled block
--> $DIR/break-in-unlabeled-block-in-macro.rs:6:9
|
LL | break $e;
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
...
LL | foo!(());
| -------- in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)

error[E0268]: `break` outside of a loop or labeled block
--> $DIR/break-in-unlabeled-block-in-macro.rs:27:19
|
LL | foo!(stmt 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 ~ foo!(stmt break 'block ());
|

error[E0268]: `break` outside of a loop or labeled block
--> $DIR/break-in-unlabeled-block-in-macro.rs:12:11
|
LL | { break $e; }
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
...
LL | foo!(@ ());
| ---------- in this macro invocation
|
= note: this error originates in the macro `foo` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider labeling this block to be able to break within it
|
LL | 'block: { break 'block $e; }
| +++++++ ++++++

error[E0268]: `break` outside of a loop or labeled block
--> $DIR/break-in-unlabeled-block-in-macro.rs:33:17
|
LL | foo!(=> break ());
| ^^^^^^^^ cannot `break` outside of a loop or labeled block

error[E0268]: `break` outside of a loop or labeled block
--> $DIR/break-in-unlabeled-block-in-macro.rs:38:17
|
LL | break ()
| ^^^^^^^^ cannot `break` outside of a loop or labeled block
...
LL | bar!()
| ------ in this macro invocation
|
= note: this error originates in the macro `bar` (in Nightly builds, run with -Z macro-backtrace for more info)

error: aborting due to 6 previous errors

For more information about this error, try `rustc --explain E0268`.
11 changes: 11 additions & 0 deletions tests/ui/parser/break-in-unlabeled-block.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-rustfix
fn main() {
'block: {
break 'block (); //~ ERROR `break` outside of a loop or labeled block
}
{
'block: {
break 'block (); //~ ERROR `break` outside of a loop or labeled block
}
}
}
11 changes: 11 additions & 0 deletions tests/ui/parser/break-in-unlabeled-block.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// run-rustfix
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.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.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.rs:8:13
|
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`.

0 comments on commit 84d44dd

Please sign in to comment.