1
Fork 0

Suggest labeling block if break is in bare block

Fix #103982.
This commit is contained in:
Esteban Küber 2023-10-03 00:20:59 +00:00
parent cdddcd3bea
commit daac011459
6 changed files with 109 additions and 8 deletions

View file

@ -580,6 +580,8 @@ passes_outside_loop =
*[false] {""} *[false] {""}
} }
passes_outside_loop_suggestion = consider labeling this block to be able to break within it
passes_params_not_allowed = passes_params_not_allowed =
referencing function parameters is not allowed in naked functions referencing function parameters is not allowed in naked functions
.help = follow the calling convention in asm block to use parameters .help = follow the calling convention in asm block to use parameters

View file

@ -1099,6 +1099,16 @@ pub struct OutsideLoop<'a> {
pub span: Span, pub span: Span,
pub name: &'a str, pub name: &'a str,
pub is_break: bool, 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)] #[derive(Diagnostic)]

View file

@ -1,7 +1,7 @@
use Context::*; use Context::*;
use rustc_hir as hir; 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::intravisit::{self, Visitor};
use rustc_hir::{Destination, Movability, Node}; use rustc_hir::{Destination, Movability, Node};
use rustc_middle::hir::map::Map; use rustc_middle::hir::map::Map;
@ -10,19 +10,21 @@ use rustc_middle::query::Providers;
use rustc_middle::ty::TyCtxt; use rustc_middle::ty::TyCtxt;
use rustc_session::Session; use rustc_session::Session;
use rustc_span::hygiene::DesugaringKind; use rustc_span::hygiene::DesugaringKind;
use rustc_span::Span; use rustc_span::{BytePos, Span};
use crate::errors::{ use crate::errors::{
BreakInsideAsyncBlock, BreakInsideClosure, BreakNonLoop, ContinueLabeledBlock, OutsideLoop, BreakInsideAsyncBlock, BreakInsideClosure, BreakNonLoop, ContinueLabeledBlock, OutsideLoop,
UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock, OutsideLoopSuggestion, UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock,
}; };
#[derive(Clone, Copy, Debug, PartialEq)] #[derive(Clone, Copy, Debug, PartialEq)]
enum Context { enum Context {
Normal, Normal,
Fn,
Loop(hir::LoopSource), Loop(hir::LoopSource),
Closure(Span), Closure(Span),
AsyncClosure(Span), AsyncClosure(Span),
UnlabeledBlock(Span),
LabeledBlock, LabeledBlock,
Constant, Constant,
} }
@ -60,6 +62,25 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
self.with_context(Constant, |v| intravisit::walk_inline_const(v, c)); 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>) { fn visit_expr(&mut self, e: &'hir hir::Expr<'hir>) {
match e.kind { match e.kind {
hir::ExprKind::Loop(ref b, _, source, _) => { hir::ExprKind::Loop(ref b, _, source, _) => {
@ -83,6 +104,14 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
hir::ExprKind::Block(ref b, Some(_label)) => { hir::ExprKind::Block(ref b, Some(_label)) => {
self.with_context(LabeledBlock, |v| v.visit_block(&b)); 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) => { hir::ExprKind::Break(break_label, ref opt_expr) => {
if let Some(e) = opt_expr { if let Some(e) = opt_expr {
self.visit_expr(e); self.visit_expr(e);
@ -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) => { hir::ExprKind::Continue(destination) => {
self.require_label_in_labeled_block(e.span, &destination, "continue"); self.require_label_in_labeled_block(e.span, &destination, "continue");
@ -169,7 +203,7 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
} }
Err(_) => {} Err(_) => {}
} }
self.require_break_cx("continue", e.span) self.require_break_cx("continue", e.span, e.span)
} }
_ => intravisit::walk_expr(self, e), _ => intravisit::walk_expr(self, e),
} }
@ -187,7 +221,8 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
self.cx = old_cx; 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 { match self.cx {
LabeledBlock | Loop(_) => {} LabeledBlock | Loop(_) => {}
Closure(closure_span) => { Closure(closure_span) => {
@ -196,8 +231,12 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
AsyncClosure(closure_span) => { AsyncClosure(closure_span) => {
self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name }); self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name });
} }
Normal | Constant => { UnlabeledBlock(block_span) if is_break => {
self.sess.emit_err(OutsideLoop { span, name, is_break: name == "break" }); 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 });
} }
} }
} }

View file

@ -0,0 +1,12 @@
// 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
}
}
}

View file

@ -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
}
}
}

View file

@ -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`.