migrate loops.rs to translateable diagnostics
This commit is contained in:
parent
572f3414b7
commit
69766e4f16
3 changed files with 198 additions and 118 deletions
|
@ -433,3 +433,37 @@ passes_expr_not_allowed_in_context =
|
||||||
passes_const_impl_const_trait =
|
passes_const_impl_const_trait =
|
||||||
const `impl`s must be for traits marked with `#[const_trait]`
|
const `impl`s must be for traits marked with `#[const_trait]`
|
||||||
.note = this trait must be annotated with `#[const_trait]`
|
.note = this trait must be annotated with `#[const_trait]`
|
||||||
|
|
||||||
|
passes_break_non_loop =
|
||||||
|
`break` with value from a `{$kind}` loop
|
||||||
|
.label = can only break with a value inside `loop` or breakable block
|
||||||
|
.label2 = you can't `break` with a value in a `{$kind}` loop
|
||||||
|
.suggestion = use `break` on its own without a value inside this `{$kind}` loop
|
||||||
|
.break_expr_suggestion = alternatively, you might have meant to use the available loop label
|
||||||
|
|
||||||
|
passes_continue_labeled_block =
|
||||||
|
`continue` pointing to a labeled block
|
||||||
|
.label = labeled blocks cannot be `continue`'d
|
||||||
|
.block_label = labeled block the `continue` points to
|
||||||
|
|
||||||
|
passes_break_inside_closure =
|
||||||
|
`{$name}` inside of a closure
|
||||||
|
.label = cannot `{$name}` inside of a closure
|
||||||
|
.closure_label = enclosing closure
|
||||||
|
|
||||||
|
passes_break_inside_async_block =
|
||||||
|
`{$name}` inside of an `async` block
|
||||||
|
.label = cannot `{$name}` inside of an `async` block
|
||||||
|
.async_block_label = enclosing `async` block
|
||||||
|
|
||||||
|
passes_outside_loop =
|
||||||
|
`{$name}` outside of a loop
|
||||||
|
.label = cannot `{$name}` outside of a loop
|
||||||
|
|
||||||
|
passes_unlabeled_in_labeled_block =
|
||||||
|
unlabeled `{$cf_type}` inside of a labeled block
|
||||||
|
.label = `{$cf_type}` statements that would diverge to or through a labeled block need to bear a label
|
||||||
|
|
||||||
|
passes_unlabeled_cf_in_while_condition =
|
||||||
|
`break` or `continue` with no label in the condition of a `while` loop
|
||||||
|
.label = unlabeled `{$cf_type}` in the condition of a `while` loop
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
use std::{io::Error, path::Path};
|
use std::{io::Error, path::Path};
|
||||||
|
|
||||||
use rustc_errors::{Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
|
use rustc_ast::Label;
|
||||||
use rustc_hir::Target;
|
use rustc_errors::{error_code, Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
|
||||||
|
use rustc_hir::{self as hir, ExprKind, Target};
|
||||||
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
|
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
|
||||||
use rustc_span::{Span, Symbol};
|
use rustc_span::{Span, Symbol};
|
||||||
|
|
||||||
|
@ -870,3 +871,119 @@ pub struct ConstImplConstTrait {
|
||||||
#[note]
|
#[note]
|
||||||
pub def_span: Span,
|
pub def_span: Span,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct BreakNonLoop<'a> {
|
||||||
|
pub span: Span,
|
||||||
|
pub head: Option<Span>,
|
||||||
|
pub kind: &'a str,
|
||||||
|
pub suggestion: String,
|
||||||
|
pub loop_label: Option<Label>,
|
||||||
|
pub break_label: Option<Label>,
|
||||||
|
pub break_expr_kind: &'a ExprKind<'a>,
|
||||||
|
pub break_expr_span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<'a> IntoDiagnostic<'_> for BreakNonLoop<'a> {
|
||||||
|
fn into_diagnostic(
|
||||||
|
self,
|
||||||
|
handler: &rustc_errors::Handler,
|
||||||
|
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
|
||||||
|
let mut diag = handler.struct_span_err_with_code(
|
||||||
|
self.span,
|
||||||
|
rustc_errors::fluent::passes::break_non_loop,
|
||||||
|
error_code!(E0571),
|
||||||
|
);
|
||||||
|
diag.set_arg("kind", self.kind);
|
||||||
|
diag.span_label(self.span, rustc_errors::fluent::passes::label);
|
||||||
|
if let Some(head) = self.head {
|
||||||
|
diag.span_label(head, rustc_errors::fluent::passes::label2);
|
||||||
|
}
|
||||||
|
diag.span_suggestion(
|
||||||
|
self.span,
|
||||||
|
rustc_errors::fluent::passes::suggestion,
|
||||||
|
self.suggestion,
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
if let (Some(label), None) = (self.loop_label, self.break_label) {
|
||||||
|
match self.break_expr_kind {
|
||||||
|
ExprKind::Path(hir::QPath::Resolved(
|
||||||
|
None,
|
||||||
|
hir::Path { segments: [segment], res: hir::def::Res::Err, .. },
|
||||||
|
)) if label.ident.to_string() == format!("'{}", segment.ident) => {
|
||||||
|
// This error is redundant, we will have already emitted a
|
||||||
|
// suggestion to use the label when `segment` wasn't found
|
||||||
|
// (hence the `Res::Err` check).
|
||||||
|
diag.delay_as_bug();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
diag.span_suggestion(
|
||||||
|
self.break_expr_span,
|
||||||
|
rustc_errors::fluent::passes::break_expr_suggestion,
|
||||||
|
label.ident,
|
||||||
|
Applicability::MaybeIncorrect,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
diag
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(passes::continue_labeled_block, code = "E0696")]
|
||||||
|
pub struct ContinueLabeledBlock {
|
||||||
|
#[primary_span]
|
||||||
|
#[label]
|
||||||
|
pub span: Span,
|
||||||
|
#[label(passes::block_label)]
|
||||||
|
pub block_span: Span,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(passes::break_inside_closure, code = "E0267")]
|
||||||
|
pub struct BreakInsideClosure<'a> {
|
||||||
|
#[primary_span]
|
||||||
|
#[label]
|
||||||
|
pub span: Span,
|
||||||
|
#[label(passes::closure_label)]
|
||||||
|
pub closure_span: Span,
|
||||||
|
pub name: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(passes::break_inside_async_block, code = "E0267")]
|
||||||
|
pub struct BreakInsideAsyncBlock<'a> {
|
||||||
|
#[primary_span]
|
||||||
|
#[label]
|
||||||
|
pub span: Span,
|
||||||
|
#[label(passes::async_block_label)]
|
||||||
|
pub closure_span: Span,
|
||||||
|
pub name: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(passes::outside_loop, code = "E0268")]
|
||||||
|
pub struct OutsideLoop<'a> {
|
||||||
|
#[primary_span]
|
||||||
|
#[label]
|
||||||
|
pub span: Span,
|
||||||
|
pub name: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(passes::unlabeled_in_labeled_block, code = "E0695")]
|
||||||
|
pub struct UnlabeledInLabeledBlock<'a> {
|
||||||
|
#[primary_span]
|
||||||
|
#[label]
|
||||||
|
pub span: Span,
|
||||||
|
pub cf_type: &'a str,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Diagnostic)]
|
||||||
|
#[diag(passes::unlabeled_cf_in_while_condition, code = "E0590")]
|
||||||
|
pub struct UnlabeledCfInWhileCondition<'a> {
|
||||||
|
#[primary_span]
|
||||||
|
#[label]
|
||||||
|
pub span: Span,
|
||||||
|
pub cf_type: &'a str,
|
||||||
|
}
|
||||||
|
|
|
@ -1,6 +1,5 @@
|
||||||
use Context::*;
|
use Context::*;
|
||||||
|
|
||||||
use rustc_errors::{struct_span_err, Applicability};
|
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::def_id::LocalDefId;
|
use rustc_hir::def_id::LocalDefId;
|
||||||
use rustc_hir::intravisit::{self, Visitor};
|
use rustc_hir::intravisit::{self, Visitor};
|
||||||
|
@ -13,6 +12,11 @@ use rustc_session::Session;
|
||||||
use rustc_span::hygiene::DesugaringKind;
|
use rustc_span::hygiene::DesugaringKind;
|
||||||
use rustc_span::Span;
|
use rustc_span::Span;
|
||||||
|
|
||||||
|
use crate::errors::{
|
||||||
|
BreakInsideAsyncBlock, BreakInsideClosure, BreakNonLoop, ContinueLabeledBlock, OutsideLoop,
|
||||||
|
UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock,
|
||||||
|
};
|
||||||
|
|
||||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||||
enum Context {
|
enum Context {
|
||||||
Normal,
|
Normal,
|
||||||
|
@ -90,7 +94,10 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
|
||||||
Ok(loop_id) => Some(loop_id),
|
Ok(loop_id) => Some(loop_id),
|
||||||
Err(hir::LoopIdError::OutsideLoopScope) => None,
|
Err(hir::LoopIdError::OutsideLoopScope) => None,
|
||||||
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
|
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
|
||||||
self.emit_unlabled_cf_in_while_condition(e.span, "break");
|
self.sess.emit_err(UnlabeledCfInWhileCondition {
|
||||||
|
span: e.span,
|
||||||
|
cf_type: "break",
|
||||||
|
});
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
Err(hir::LoopIdError::UnresolvedLabel) => None,
|
Err(hir::LoopIdError::UnresolvedLabel) => None,
|
||||||
|
@ -116,69 +123,22 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
|
||||||
match loop_kind {
|
match loop_kind {
|
||||||
None | Some(hir::LoopSource::Loop) => (),
|
None | Some(hir::LoopSource::Loop) => (),
|
||||||
Some(kind) => {
|
Some(kind) => {
|
||||||
let mut err = struct_span_err!(
|
let suggestion = format!(
|
||||||
self.sess,
|
"break{}",
|
||||||
e.span,
|
break_label
|
||||||
E0571,
|
.label
|
||||||
"`break` with value from a `{}` loop",
|
.map_or_else(String::new, |l| format!(" {}", l.ident))
|
||||||
kind.name()
|
|
||||||
);
|
);
|
||||||
err.span_label(
|
self.sess.emit_err(BreakNonLoop {
|
||||||
e.span,
|
span: e.span,
|
||||||
"can only break with a value inside `loop` or breakable block",
|
head,
|
||||||
);
|
kind: kind.name(),
|
||||||
if let Some(head) = head {
|
suggestion,
|
||||||
err.span_label(
|
loop_label,
|
||||||
head,
|
break_label: break_label.label,
|
||||||
&format!(
|
break_expr_kind: &break_expr.kind,
|
||||||
"you can't `break` with a value in a `{}` loop",
|
break_expr_span: break_expr.span,
|
||||||
kind.name()
|
});
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
err.span_suggestion(
|
|
||||||
e.span,
|
|
||||||
&format!(
|
|
||||||
"use `break` on its own without a value inside this `{}` loop",
|
|
||||||
kind.name(),
|
|
||||||
),
|
|
||||||
format!(
|
|
||||||
"break{}",
|
|
||||||
break_label
|
|
||||||
.label
|
|
||||||
.map_or_else(String::new, |l| format!(" {}", l.ident))
|
|
||||||
),
|
|
||||||
Applicability::MaybeIncorrect,
|
|
||||||
);
|
|
||||||
if let (Some(label), None) = (loop_label, break_label.label) {
|
|
||||||
match break_expr.kind {
|
|
||||||
hir::ExprKind::Path(hir::QPath::Resolved(
|
|
||||||
None,
|
|
||||||
hir::Path {
|
|
||||||
segments: [segment],
|
|
||||||
res: hir::def::Res::Err,
|
|
||||||
..
|
|
||||||
},
|
|
||||||
)) if label.ident.to_string()
|
|
||||||
== format!("'{}", segment.ident) =>
|
|
||||||
{
|
|
||||||
// This error is redundant, we will have already emitted a
|
|
||||||
// suggestion to use the label when `segment` wasn't found
|
|
||||||
// (hence the `Res::Err` check).
|
|
||||||
err.delay_as_bug();
|
|
||||||
}
|
|
||||||
_ => {
|
|
||||||
err.span_suggestion(
|
|
||||||
break_expr.span,
|
|
||||||
"alternatively, you might have meant to use the \
|
|
||||||
available loop label",
|
|
||||||
label.ident,
|
|
||||||
Applicability::MaybeIncorrect,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err.emit();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -191,19 +151,17 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
|
||||||
match destination.target_id {
|
match destination.target_id {
|
||||||
Ok(loop_id) => {
|
Ok(loop_id) => {
|
||||||
if let Node::Block(block) = self.hir_map.find(loop_id).unwrap() {
|
if let Node::Block(block) = self.hir_map.find(loop_id).unwrap() {
|
||||||
struct_span_err!(
|
self.sess.emit_err(ContinueLabeledBlock {
|
||||||
self.sess,
|
span: e.span,
|
||||||
e.span,
|
block_span: block.span,
|
||||||
E0696,
|
});
|
||||||
"`continue` pointing to a labeled block"
|
|
||||||
)
|
|
||||||
.span_label(e.span, "labeled blocks cannot be `continue`'d")
|
|
||||||
.span_label(block.span, "labeled block the `continue` points to")
|
|
||||||
.emit();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
|
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
|
||||||
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
|
self.sess.emit_err(UnlabeledCfInWhileCondition {
|
||||||
|
span: e.span,
|
||||||
|
cf_type: "continue",
|
||||||
|
});
|
||||||
}
|
}
|
||||||
Err(_) => {}
|
Err(_) => {}
|
||||||
}
|
}
|
||||||
|
@ -226,21 +184,16 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn require_break_cx(&self, name: &str, span: Span) {
|
fn require_break_cx(&self, name: &str, span: Span) {
|
||||||
let err_inside_of = |article, ty, closure_span| {
|
|
||||||
struct_span_err!(self.sess, span, E0267, "`{}` inside of {} {}", name, article, ty)
|
|
||||||
.span_label(span, format!("cannot `{}` inside of {} {}", name, article, ty))
|
|
||||||
.span_label(closure_span, &format!("enclosing {}", ty))
|
|
||||||
.emit();
|
|
||||||
};
|
|
||||||
|
|
||||||
match self.cx {
|
match self.cx {
|
||||||
LabeledBlock | Loop(_) => {}
|
LabeledBlock | Loop(_) => {}
|
||||||
Closure(closure_span) => err_inside_of("a", "closure", closure_span),
|
Closure(closure_span) => {
|
||||||
AsyncClosure(closure_span) => err_inside_of("an", "`async` block", closure_span),
|
self.sess.emit_err(BreakInsideClosure { span, closure_span, name });
|
||||||
|
}
|
||||||
|
AsyncClosure(closure_span) => {
|
||||||
|
self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name });
|
||||||
|
}
|
||||||
Normal | AnonConst => {
|
Normal | AnonConst => {
|
||||||
struct_span_err!(self.sess, span, E0268, "`{}` outside of a loop", name)
|
self.sess.emit_err(OutsideLoop { span, name });
|
||||||
.span_label(span, format!("cannot `{}` outside of a loop", name))
|
|
||||||
.emit();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -251,37 +204,13 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
|
||||||
label: &Destination,
|
label: &Destination,
|
||||||
cf_type: &str,
|
cf_type: &str,
|
||||||
) -> bool {
|
) -> bool {
|
||||||
if !span.is_desugaring(DesugaringKind::QuestionMark) && self.cx == LabeledBlock {
|
if !span.is_desugaring(DesugaringKind::QuestionMark)
|
||||||
if label.label.is_none() {
|
&& self.cx == LabeledBlock
|
||||||
struct_span_err!(
|
&& label.label.is_none()
|
||||||
self.sess,
|
{
|
||||||
span,
|
self.sess.emit_err(UnlabeledInLabeledBlock { span, cf_type });
|
||||||
E0695,
|
return true;
|
||||||
"unlabeled `{}` inside of a labeled block",
|
|
||||||
cf_type
|
|
||||||
)
|
|
||||||
.span_label(
|
|
||||||
span,
|
|
||||||
format!(
|
|
||||||
"`{}` statements that would diverge to or through \
|
|
||||||
a labeled block need to bear a label",
|
|
||||||
cf_type
|
|
||||||
),
|
|
||||||
)
|
|
||||||
.emit();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
false
|
false
|
||||||
}
|
}
|
||||||
fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
|
|
||||||
struct_span_err!(
|
|
||||||
self.sess,
|
|
||||||
span,
|
|
||||||
E0590,
|
|
||||||
"`break` or `continue` with no label in the condition of a `while` loop"
|
|
||||||
)
|
|
||||||
.span_label(span, format!("unlabeled `{}` in the condition of a `while` loop", cf_type))
|
|
||||||
.emit();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue