Skip to content

Commit 69766e4

Browse files
committed
migrate loops.rs to translateable diagnostics
1 parent 572f341 commit 69766e4

File tree

3 files changed

+198
-118
lines changed

3 files changed

+198
-118
lines changed

compiler/rustc_error_messages/locales/en-US/passes.ftl

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,3 +433,37 @@ passes_expr_not_allowed_in_context =
433433
passes_const_impl_const_trait =
434434
const `impl`s must be for traits marked with `#[const_trait]`
435435
.note = this trait must be annotated with `#[const_trait]`
436+
437+
passes_break_non_loop =
438+
`break` with value from a `{$kind}` loop
439+
.label = can only break with a value inside `loop` or breakable block
440+
.label2 = you can't `break` with a value in a `{$kind}` loop
441+
.suggestion = use `break` on its own without a value inside this `{$kind}` loop
442+
.break_expr_suggestion = alternatively, you might have meant to use the available loop label
443+
444+
passes_continue_labeled_block =
445+
`continue` pointing to a labeled block
446+
.label = labeled blocks cannot be `continue`'d
447+
.block_label = labeled block the `continue` points to
448+
449+
passes_break_inside_closure =
450+
`{$name}` inside of a closure
451+
.label = cannot `{$name}` inside of a closure
452+
.closure_label = enclosing closure
453+
454+
passes_break_inside_async_block =
455+
`{$name}` inside of an `async` block
456+
.label = cannot `{$name}` inside of an `async` block
457+
.async_block_label = enclosing `async` block
458+
459+
passes_outside_loop =
460+
`{$name}` outside of a loop
461+
.label = cannot `{$name}` outside of a loop
462+
463+
passes_unlabeled_in_labeled_block =
464+
unlabeled `{$cf_type}` inside of a labeled block
465+
.label = `{$cf_type}` statements that would diverge to or through a labeled block need to bear a label
466+
467+
passes_unlabeled_cf_in_while_condition =
468+
`break` or `continue` with no label in the condition of a `while` loop
469+
.label = unlabeled `{$cf_type}` in the condition of a `while` loop

compiler/rustc_passes/src/errors.rs

Lines changed: 119 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use std::{io::Error, path::Path};
22

3-
use rustc_errors::{Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
4-
use rustc_hir::Target;
3+
use rustc_ast::Label;
4+
use rustc_errors::{error_code, Applicability, ErrorGuaranteed, IntoDiagnostic, MultiSpan};
5+
use rustc_hir::{self as hir, ExprKind, Target};
56
use rustc_macros::{Diagnostic, LintDiagnostic, Subdiagnostic};
67
use rustc_span::{Span, Symbol};
78

@@ -870,3 +871,119 @@ pub struct ConstImplConstTrait {
870871
#[note]
871872
pub def_span: Span,
872873
}
874+
875+
pub struct BreakNonLoop<'a> {
876+
pub span: Span,
877+
pub head: Option<Span>,
878+
pub kind: &'a str,
879+
pub suggestion: String,
880+
pub loop_label: Option<Label>,
881+
pub break_label: Option<Label>,
882+
pub break_expr_kind: &'a ExprKind<'a>,
883+
pub break_expr_span: Span,
884+
}
885+
886+
impl<'a> IntoDiagnostic<'_> for BreakNonLoop<'a> {
887+
fn into_diagnostic(
888+
self,
889+
handler: &rustc_errors::Handler,
890+
) -> rustc_errors::DiagnosticBuilder<'_, ErrorGuaranteed> {
891+
let mut diag = handler.struct_span_err_with_code(
892+
self.span,
893+
rustc_errors::fluent::passes::break_non_loop,
894+
error_code!(E0571),
895+
);
896+
diag.set_arg("kind", self.kind);
897+
diag.span_label(self.span, rustc_errors::fluent::passes::label);
898+
if let Some(head) = self.head {
899+
diag.span_label(head, rustc_errors::fluent::passes::label2);
900+
}
901+
diag.span_suggestion(
902+
self.span,
903+
rustc_errors::fluent::passes::suggestion,
904+
self.suggestion,
905+
Applicability::MaybeIncorrect,
906+
);
907+
if let (Some(label), None) = (self.loop_label, self.break_label) {
908+
match self.break_expr_kind {
909+
ExprKind::Path(hir::QPath::Resolved(
910+
None,
911+
hir::Path { segments: [segment], res: hir::def::Res::Err, .. },
912+
)) if label.ident.to_string() == format!("'{}", segment.ident) => {
913+
// This error is redundant, we will have already emitted a
914+
// suggestion to use the label when `segment` wasn't found
915+
// (hence the `Res::Err` check).
916+
diag.delay_as_bug();
917+
}
918+
_ => {
919+
diag.span_suggestion(
920+
self.break_expr_span,
921+
rustc_errors::fluent::passes::break_expr_suggestion,
922+
label.ident,
923+
Applicability::MaybeIncorrect,
924+
);
925+
}
926+
}
927+
}
928+
diag
929+
}
930+
}
931+
932+
#[derive(Diagnostic)]
933+
#[diag(passes::continue_labeled_block, code = "E0696")]
934+
pub struct ContinueLabeledBlock {
935+
#[primary_span]
936+
#[label]
937+
pub span: Span,
938+
#[label(passes::block_label)]
939+
pub block_span: Span,
940+
}
941+
942+
#[derive(Diagnostic)]
943+
#[diag(passes::break_inside_closure, code = "E0267")]
944+
pub struct BreakInsideClosure<'a> {
945+
#[primary_span]
946+
#[label]
947+
pub span: Span,
948+
#[label(passes::closure_label)]
949+
pub closure_span: Span,
950+
pub name: &'a str,
951+
}
952+
953+
#[derive(Diagnostic)]
954+
#[diag(passes::break_inside_async_block, code = "E0267")]
955+
pub struct BreakInsideAsyncBlock<'a> {
956+
#[primary_span]
957+
#[label]
958+
pub span: Span,
959+
#[label(passes::async_block_label)]
960+
pub closure_span: Span,
961+
pub name: &'a str,
962+
}
963+
964+
#[derive(Diagnostic)]
965+
#[diag(passes::outside_loop, code = "E0268")]
966+
pub struct OutsideLoop<'a> {
967+
#[primary_span]
968+
#[label]
969+
pub span: Span,
970+
pub name: &'a str,
971+
}
972+
973+
#[derive(Diagnostic)]
974+
#[diag(passes::unlabeled_in_labeled_block, code = "E0695")]
975+
pub struct UnlabeledInLabeledBlock<'a> {
976+
#[primary_span]
977+
#[label]
978+
pub span: Span,
979+
pub cf_type: &'a str,
980+
}
981+
982+
#[derive(Diagnostic)]
983+
#[diag(passes::unlabeled_cf_in_while_condition, code = "E0590")]
984+
pub struct UnlabeledCfInWhileCondition<'a> {
985+
#[primary_span]
986+
#[label]
987+
pub span: Span,
988+
pub cf_type: &'a str,
989+
}

compiler/rustc_passes/src/loops.rs

Lines changed: 45 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use Context::*;
22

3-
use rustc_errors::{struct_span_err, Applicability};
43
use rustc_hir as hir;
54
use rustc_hir::def_id::LocalDefId;
65
use rustc_hir::intravisit::{self, Visitor};
@@ -13,6 +12,11 @@ use rustc_session::Session;
1312
use rustc_span::hygiene::DesugaringKind;
1413
use rustc_span::Span;
1514

15+
use crate::errors::{
16+
BreakInsideAsyncBlock, BreakInsideClosure, BreakNonLoop, ContinueLabeledBlock, OutsideLoop,
17+
UnlabeledCfInWhileCondition, UnlabeledInLabeledBlock,
18+
};
19+
1620
#[derive(Clone, Copy, Debug, PartialEq)]
1721
enum Context {
1822
Normal,
@@ -90,7 +94,10 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
9094
Ok(loop_id) => Some(loop_id),
9195
Err(hir::LoopIdError::OutsideLoopScope) => None,
9296
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
93-
self.emit_unlabled_cf_in_while_condition(e.span, "break");
97+
self.sess.emit_err(UnlabeledCfInWhileCondition {
98+
span: e.span,
99+
cf_type: "break",
100+
});
94101
None
95102
}
96103
Err(hir::LoopIdError::UnresolvedLabel) => None,
@@ -116,69 +123,22 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
116123
match loop_kind {
117124
None | Some(hir::LoopSource::Loop) => (),
118125
Some(kind) => {
119-
let mut err = struct_span_err!(
120-
self.sess,
121-
e.span,
122-
E0571,
123-
"`break` with value from a `{}` loop",
124-
kind.name()
125-
);
126-
err.span_label(
127-
e.span,
128-
"can only break with a value inside `loop` or breakable block",
126+
let suggestion = format!(
127+
"break{}",
128+
break_label
129+
.label
130+
.map_or_else(String::new, |l| format!(" {}", l.ident))
129131
);
130-
if let Some(head) = head {
131-
err.span_label(
132-
head,
133-
&format!(
134-
"you can't `break` with a value in a `{}` loop",
135-
kind.name()
136-
),
137-
);
138-
}
139-
err.span_suggestion(
140-
e.span,
141-
&format!(
142-
"use `break` on its own without a value inside this `{}` loop",
143-
kind.name(),
144-
),
145-
format!(
146-
"break{}",
147-
break_label
148-
.label
149-
.map_or_else(String::new, |l| format!(" {}", l.ident))
150-
),
151-
Applicability::MaybeIncorrect,
152-
);
153-
if let (Some(label), None) = (loop_label, break_label.label) {
154-
match break_expr.kind {
155-
hir::ExprKind::Path(hir::QPath::Resolved(
156-
None,
157-
hir::Path {
158-
segments: [segment],
159-
res: hir::def::Res::Err,
160-
..
161-
},
162-
)) if label.ident.to_string()
163-
== format!("'{}", segment.ident) =>
164-
{
165-
// This error is redundant, we will have already emitted a
166-
// suggestion to use the label when `segment` wasn't found
167-
// (hence the `Res::Err` check).
168-
err.delay_as_bug();
169-
}
170-
_ => {
171-
err.span_suggestion(
172-
break_expr.span,
173-
"alternatively, you might have meant to use the \
174-
available loop label",
175-
label.ident,
176-
Applicability::MaybeIncorrect,
177-
);
178-
}
179-
}
180-
}
181-
err.emit();
132+
self.sess.emit_err(BreakNonLoop {
133+
span: e.span,
134+
head,
135+
kind: kind.name(),
136+
suggestion,
137+
loop_label,
138+
break_label: break_label.label,
139+
break_expr_kind: &break_expr.kind,
140+
break_expr_span: break_expr.span,
141+
});
182142
}
183143
}
184144
}
@@ -191,19 +151,17 @@ impl<'a, 'hir> Visitor<'hir> for CheckLoopVisitor<'a, 'hir> {
191151
match destination.target_id {
192152
Ok(loop_id) => {
193153
if let Node::Block(block) = self.hir_map.find(loop_id).unwrap() {
194-
struct_span_err!(
195-
self.sess,
196-
e.span,
197-
E0696,
198-
"`continue` pointing to a labeled block"
199-
)
200-
.span_label(e.span, "labeled blocks cannot be `continue`'d")
201-
.span_label(block.span, "labeled block the `continue` points to")
202-
.emit();
154+
self.sess.emit_err(ContinueLabeledBlock {
155+
span: e.span,
156+
block_span: block.span,
157+
});
203158
}
204159
}
205160
Err(hir::LoopIdError::UnlabeledCfInWhileCondition) => {
206-
self.emit_unlabled_cf_in_while_condition(e.span, "continue");
161+
self.sess.emit_err(UnlabeledCfInWhileCondition {
162+
span: e.span,
163+
cf_type: "continue",
164+
});
207165
}
208166
Err(_) => {}
209167
}
@@ -226,21 +184,16 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
226184
}
227185

228186
fn require_break_cx(&self, name: &str, span: Span) {
229-
let err_inside_of = |article, ty, closure_span| {
230-
struct_span_err!(self.sess, span, E0267, "`{}` inside of {} {}", name, article, ty)
231-
.span_label(span, format!("cannot `{}` inside of {} {}", name, article, ty))
232-
.span_label(closure_span, &format!("enclosing {}", ty))
233-
.emit();
234-
};
235-
236187
match self.cx {
237188
LabeledBlock | Loop(_) => {}
238-
Closure(closure_span) => err_inside_of("a", "closure", closure_span),
239-
AsyncClosure(closure_span) => err_inside_of("an", "`async` block", closure_span),
189+
Closure(closure_span) => {
190+
self.sess.emit_err(BreakInsideClosure { span, closure_span, name });
191+
}
192+
AsyncClosure(closure_span) => {
193+
self.sess.emit_err(BreakInsideAsyncBlock { span, closure_span, name });
194+
}
240195
Normal | AnonConst => {
241-
struct_span_err!(self.sess, span, E0268, "`{}` outside of a loop", name)
242-
.span_label(span, format!("cannot `{}` outside of a loop", name))
243-
.emit();
196+
self.sess.emit_err(OutsideLoop { span, name });
244197
}
245198
}
246199
}
@@ -251,37 +204,13 @@ impl<'a, 'hir> CheckLoopVisitor<'a, 'hir> {
251204
label: &Destination,
252205
cf_type: &str,
253206
) -> bool {
254-
if !span.is_desugaring(DesugaringKind::QuestionMark) && self.cx == LabeledBlock {
255-
if label.label.is_none() {
256-
struct_span_err!(
257-
self.sess,
258-
span,
259-
E0695,
260-
"unlabeled `{}` inside of a labeled block",
261-
cf_type
262-
)
263-
.span_label(
264-
span,
265-
format!(
266-
"`{}` statements that would diverge to or through \
267-
a labeled block need to bear a label",
268-
cf_type
269-
),
270-
)
271-
.emit();
272-
return true;
273-
}
207+
if !span.is_desugaring(DesugaringKind::QuestionMark)
208+
&& self.cx == LabeledBlock
209+
&& label.label.is_none()
210+
{
211+
self.sess.emit_err(UnlabeledInLabeledBlock { span, cf_type });
212+
return true;
274213
}
275214
false
276215
}
277-
fn emit_unlabled_cf_in_while_condition(&mut self, span: Span, cf_type: &str) {
278-
struct_span_err!(
279-
self.sess,
280-
span,
281-
E0590,
282-
"`break` or `continue` with no label in the condition of a `while` loop"
283-
)
284-
.span_label(span, format!("unlabeled `{}` in the condition of a `while` loop", cf_type))
285-
.emit();
286-
}
287216
}

0 commit comments

Comments
 (0)