Skip to content

Commit a2d9d73

Browse files
committed
Auto merge of #119751 - nnethercote:error-api-fixes, r=oli-obk
Diagnostic API fixes Some improvements to diagnostic APIs: improve some naming, use shortcuts in more places, and add a couple of missing methods. r? `@compiler-errors`
2 parents e927184 + 700a396 commit a2d9d73

File tree

135 files changed

+766
-756
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

135 files changed

+766
-756
lines changed

compiler/rustc_ast_passes/src/feature_gate.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ macro_rules! gate {
2323
($visitor:expr, $feature:ident, $span:expr, $explain:expr, $help:expr) => {{
2424
if !$visitor.features.$feature && !$span.allows_unstable(sym::$feature) {
2525
feature_err(&$visitor.sess.parse_sess, sym::$feature, $span, $explain)
26-
.help_mv($help)
26+
.with_help($help)
2727
.emit();
2828
}
2929
}};

compiler/rustc_ast_passes/src/show_span.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,21 @@ struct ShowSpanVisitor<'a> {
3838
impl<'a> Visitor<'a> for ShowSpanVisitor<'a> {
3939
fn visit_expr(&mut self, e: &'a ast::Expr) {
4040
if let Mode::Expression = self.mode {
41-
self.dcx.emit_warning(errors::ShowSpan { span: e.span, msg: "expression" });
41+
self.dcx.emit_warn(errors::ShowSpan { span: e.span, msg: "expression" });
4242
}
4343
visit::walk_expr(self, e);
4444
}
4545

4646
fn visit_pat(&mut self, p: &'a ast::Pat) {
4747
if let Mode::Pattern = self.mode {
48-
self.dcx.emit_warning(errors::ShowSpan { span: p.span, msg: "pattern" });
48+
self.dcx.emit_warn(errors::ShowSpan { span: p.span, msg: "pattern" });
4949
}
5050
visit::walk_pat(self, p);
5151
}
5252

5353
fn visit_ty(&mut self, t: &'a ast::Ty) {
5454
if let Mode::Type = self.mode {
55-
self.dcx.emit_warning(errors::ShowSpan { span: t.span, msg: "type" });
55+
self.dcx.emit_warn(errors::ShowSpan { span: t.span, msg: "type" });
5656
}
5757
visit::walk_ty(self, t);
5858
}

compiler/rustc_attr/src/builtin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,7 @@ pub fn eval_condition(
621621
}
622622
};
623623
let Some(min_version) = parse_version(*min_version) else {
624-
dcx.emit_warning(session_diagnostics::UnknownVersionLiteral { span: *span });
624+
dcx.emit_warn(session_diagnostics::UnknownVersionLiteral { span: *span });
625625
return false;
626626
};
627627

compiler/rustc_attr/src/session_diagnostics.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ impl<'a, G: EmissionGuarantee> IntoDiagnostic<'a, G> for UnknownMetaItem<'_> {
5555
fn into_diagnostic(self, dcx: &'a DiagCtxt, level: Level) -> DiagnosticBuilder<'a, G> {
5656
let expected = self.expected.iter().map(|name| format!("`{name}`")).collect::<Vec<_>>();
5757
DiagnosticBuilder::new(dcx, level, fluent::attr_unknown_meta_item)
58-
.span_mv(self.span)
59-
.code_mv(error_code!(E0541))
60-
.arg_mv("item", self.item)
61-
.arg_mv("expected", expected.join(", "))
62-
.span_label_mv(self.span, fluent::attr_label)
58+
.with_span(self.span)
59+
.with_code(error_code!(E0541))
60+
.with_arg("item", self.item)
61+
.with_arg("expected", expected.join(", "))
62+
.with_span_label(self.span, fluent::attr_label)
6363
}
6464
}
6565

compiler/rustc_borrowck/src/borrowck_errors.rs

+49-36
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_errors::{struct_span_err, DiagCtxt, DiagnosticBuilder};
1+
use rustc_errors::{struct_span_code_err, DiagCtxt, DiagnosticBuilder};
22
use rustc_middle::ty::{self, Ty, TyCtxt};
33
use rustc_span::Span;
44

@@ -31,15 +31,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
3131
borrow_span: Span,
3232
borrow_desc: &str,
3333
) -> DiagnosticBuilder<'tcx> {
34-
struct_span_err!(
34+
struct_span_code_err!(
3535
self.dcx(),
3636
span,
3737
E0503,
3838
"cannot use {} because it was mutably borrowed",
3939
desc,
4040
)
41-
.span_label_mv(borrow_span, format!("{borrow_desc} is borrowed here"))
42-
.span_label_mv(span, format!("use of borrowed {borrow_desc}"))
41+
.with_span_label(borrow_span, format!("{borrow_desc} is borrowed here"))
42+
.with_span_label(span, format!("use of borrowed {borrow_desc}"))
4343
}
4444

4545
pub(crate) fn cannot_mutably_borrow_multiply(
@@ -52,7 +52,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
5252
old_load_end_span: Option<Span>,
5353
) -> DiagnosticBuilder<'tcx> {
5454
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
55-
let mut err = struct_span_err!(
55+
let mut err = struct_span_code_err!(
5656
self.dcx(),
5757
new_loan_span,
5858
E0499,
@@ -98,7 +98,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
9898
old_loan_span: Span,
9999
old_load_end_span: Option<Span>,
100100
) -> DiagnosticBuilder<'tcx> {
101-
let mut err = struct_span_err!(
101+
let mut err = struct_span_code_err!(
102102
self.dcx(),
103103
new_loan_span,
104104
E0524,
@@ -131,7 +131,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
131131
old_opt_via: &str,
132132
previous_end_span: Option<Span>,
133133
) -> DiagnosticBuilder<'cx> {
134-
let mut err = struct_span_err!(
134+
let mut err = struct_span_code_err!(
135135
self.dcx(),
136136
new_loan_span,
137137
E0500,
@@ -163,7 +163,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
163163
previous_end_span: Option<Span>,
164164
second_borrow_desc: &str,
165165
) -> DiagnosticBuilder<'cx> {
166-
let mut err = struct_span_err!(
166+
let mut err = struct_span_code_err!(
167167
self.dcx(),
168168
new_loan_span,
169169
E0501,
@@ -196,7 +196,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
196196
old_load_end_span: Option<Span>,
197197
) -> DiagnosticBuilder<'cx> {
198198
let via = |msg: &str| if msg.is_empty() { "".to_string() } else { format!(" (via {msg})") };
199-
let mut err = struct_span_err!(
199+
let mut err = struct_span_code_err!(
200200
self.dcx(),
201201
span,
202202
E0502,
@@ -236,15 +236,15 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
236236
borrow_span: Span,
237237
desc: &str,
238238
) -> DiagnosticBuilder<'cx> {
239-
struct_span_err!(
239+
struct_span_code_err!(
240240
self.dcx(),
241241
span,
242242
E0506,
243243
"cannot assign to {} because it is borrowed",
244244
desc,
245245
)
246-
.span_label_mv(borrow_span, format!("{desc} is borrowed here"))
247-
.span_label_mv(span, format!("{desc} is assigned to here but it was already borrowed"))
246+
.with_span_label(borrow_span, format!("{desc} is borrowed here"))
247+
.with_span_label(span, format!("{desc} is assigned to here but it was already borrowed"))
248248
}
249249

250250
pub(crate) fn cannot_reassign_immutable(
@@ -254,19 +254,25 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
254254
is_arg: bool,
255255
) -> DiagnosticBuilder<'cx> {
256256
let msg = if is_arg { "to immutable argument" } else { "twice to immutable variable" };
257-
struct_span_err!(self.dcx(), span, E0384, "cannot assign {} {}", msg, desc)
257+
struct_span_code_err!(self.dcx(), span, E0384, "cannot assign {} {}", msg, desc)
258258
}
259259

260260
pub(crate) fn cannot_assign(&self, span: Span, desc: &str) -> DiagnosticBuilder<'tcx> {
261-
struct_span_err!(self.dcx(), span, E0594, "cannot assign to {}", desc)
261+
struct_span_code_err!(self.dcx(), span, E0594, "cannot assign to {}", desc)
262262
}
263263

264264
pub(crate) fn cannot_move_out_of(
265265
&self,
266266
move_from_span: Span,
267267
move_from_desc: &str,
268268
) -> DiagnosticBuilder<'cx> {
269-
struct_span_err!(self.dcx(), move_from_span, E0507, "cannot move out of {}", move_from_desc)
269+
struct_span_code_err!(
270+
self.dcx(),
271+
move_from_span,
272+
E0507,
273+
"cannot move out of {}",
274+
move_from_desc
275+
)
270276
}
271277

272278
/// Signal an error due to an attempt to move out of the interior
@@ -283,30 +289,30 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
283289
(&ty::Slice(_), _) => "slice",
284290
_ => span_bug!(move_from_span, "this path should not cause illegal move"),
285291
};
286-
struct_span_err!(
292+
struct_span_code_err!(
287293
self.dcx(),
288294
move_from_span,
289295
E0508,
290296
"cannot move out of type `{}`, a non-copy {}",
291297
ty,
292298
type_name,
293299
)
294-
.span_label_mv(move_from_span, "cannot move out of here")
300+
.with_span_label(move_from_span, "cannot move out of here")
295301
}
296302

297303
pub(crate) fn cannot_move_out_of_interior_of_drop(
298304
&self,
299305
move_from_span: Span,
300306
container_ty: Ty<'_>,
301307
) -> DiagnosticBuilder<'cx> {
302-
struct_span_err!(
308+
struct_span_code_err!(
303309
self.dcx(),
304310
move_from_span,
305311
E0509,
306312
"cannot move out of type `{}`, which implements the `Drop` trait",
307313
container_ty,
308314
)
309-
.span_label_mv(move_from_span, "cannot move out of here")
315+
.with_span_label(move_from_span, "cannot move out of here")
310316
}
311317

312318
pub(crate) fn cannot_act_on_moved_value(
@@ -318,7 +324,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
318324
) -> DiagnosticBuilder<'tcx> {
319325
let moved_path = moved_path.map(|mp| format!(": `{mp}`")).unwrap_or_default();
320326

321-
struct_span_err!(
327+
struct_span_code_err!(
322328
self.dcx(),
323329
use_span,
324330
E0382,
@@ -335,7 +341,14 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
335341
path: &str,
336342
reason: &str,
337343
) -> DiagnosticBuilder<'tcx> {
338-
struct_span_err!(self.dcx(), span, E0596, "cannot borrow {} as mutable{}", path, reason)
344+
struct_span_code_err!(
345+
self.dcx(),
346+
span,
347+
E0596,
348+
"cannot borrow {} as mutable{}",
349+
path,
350+
reason
351+
)
339352
}
340353

341354
pub(crate) fn cannot_mutate_in_immutable_section(
@@ -346,7 +359,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
346359
immutable_section: &str,
347360
action: &str,
348361
) -> DiagnosticBuilder<'tcx> {
349-
struct_span_err!(
362+
struct_span_code_err!(
350363
self.dcx(),
351364
mutate_span,
352365
E0510,
@@ -355,8 +368,8 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
355368
immutable_place,
356369
immutable_section,
357370
)
358-
.span_label_mv(mutate_span, format!("cannot {action}"))
359-
.span_label_mv(immutable_span, format!("value is immutable in {immutable_section}"))
371+
.with_span_label(mutate_span, format!("cannot {action}"))
372+
.with_span_label(immutable_span, format!("value is immutable in {immutable_section}"))
360373
}
361374

362375
pub(crate) fn cannot_borrow_across_coroutine_yield(
@@ -365,20 +378,20 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
365378
yield_span: Span,
366379
) -> DiagnosticBuilder<'tcx> {
367380
let coroutine_kind = self.body.coroutine.as_ref().unwrap().coroutine_kind;
368-
struct_span_err!(
381+
struct_span_code_err!(
369382
self.dcx(),
370383
span,
371384
E0626,
372385
"borrow may still be in use when {coroutine_kind:#} yields",
373386
)
374-
.span_label_mv(yield_span, "possible yield occurs here")
387+
.with_span_label(yield_span, "possible yield occurs here")
375388
}
376389

377390
pub(crate) fn cannot_borrow_across_destructor(
378391
&self,
379392
borrow_span: Span,
380393
) -> DiagnosticBuilder<'tcx> {
381-
struct_span_err!(
394+
struct_span_code_err!(
382395
self.dcx(),
383396
borrow_span,
384397
E0713,
@@ -391,7 +404,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
391404
span: Span,
392405
path: &str,
393406
) -> DiagnosticBuilder<'tcx> {
394-
struct_span_err!(self.dcx(), span, E0597, "{} does not live long enough", path,)
407+
struct_span_code_err!(self.dcx(), span, E0597, "{} does not live long enough", path,)
395408
}
396409

397410
pub(crate) fn cannot_return_reference_to_local(
@@ -401,7 +414,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
401414
reference_desc: &str,
402415
path_desc: &str,
403416
) -> DiagnosticBuilder<'tcx> {
404-
struct_span_err!(
417+
struct_span_code_err!(
405418
self.dcx(),
406419
span,
407420
E0515,
@@ -410,7 +423,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
410423
REFERENCE = reference_desc,
411424
LOCAL = path_desc,
412425
)
413-
.span_label_mv(
426+
.with_span_label(
414427
span,
415428
format!("{return_kind}s a {reference_desc} data owned by the current function"),
416429
)
@@ -424,22 +437,22 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
424437
capture_span: Span,
425438
scope: &str,
426439
) -> DiagnosticBuilder<'tcx> {
427-
struct_span_err!(
440+
struct_span_code_err!(
428441
self.dcx(),
429442
closure_span,
430443
E0373,
431444
"{closure_kind} may outlive the current {scope}, but it borrows {borrowed_path}, \
432445
which is owned by the current {scope}",
433446
)
434-
.span_label_mv(capture_span, format!("{borrowed_path} is borrowed here"))
435-
.span_label_mv(closure_span, format!("may outlive borrowed value {borrowed_path}"))
447+
.with_span_label(capture_span, format!("{borrowed_path} is borrowed here"))
448+
.with_span_label(closure_span, format!("may outlive borrowed value {borrowed_path}"))
436449
}
437450

438451
pub(crate) fn thread_local_value_does_not_live_long_enough(
439452
&self,
440453
span: Span,
441454
) -> DiagnosticBuilder<'tcx> {
442-
struct_span_err!(
455+
struct_span_code_err!(
443456
self.dcx(),
444457
span,
445458
E0712,
@@ -451,7 +464,7 @@ impl<'cx, 'tcx> crate::MirBorrowckCtxt<'cx, 'tcx> {
451464
&self,
452465
span: Span,
453466
) -> DiagnosticBuilder<'tcx> {
454-
struct_span_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed",)
467+
struct_span_code_err!(self.dcx(), span, E0716, "temporary value dropped while borrowed",)
455468
}
456469
}
457470

@@ -460,7 +473,7 @@ pub(crate) fn borrowed_data_escapes_closure<'tcx>(
460473
escape_span: Span,
461474
escapes_from: &str,
462475
) -> DiagnosticBuilder<'tcx> {
463-
struct_span_err!(
476+
struct_span_code_err!(
464477
tcx.dcx(),
465478
escape_span,
466479
E0521,

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+11-5
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
// ignore-tidy-filelength
2+
13
use either::Either;
24
use rustc_data_structures::captures::Captures;
35
use rustc_data_structures::fx::FxIndexSet;
4-
use rustc_errors::{struct_span_err, Applicability, Diagnostic, DiagnosticBuilder, MultiSpan};
6+
use rustc_errors::{struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder, MultiSpan};
57
use rustc_hir as hir;
68
use rustc_hir::def::{DefKind, Res};
79
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
@@ -550,8 +552,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
550552
};
551553

552554
let used = desired_action.as_general_verb_in_past_tense();
553-
let mut err =
554-
struct_span_err!(self.dcx(), span, E0381, "{used} binding {desc}{isnt_initialized}");
555+
let mut err = struct_span_code_err!(
556+
self.dcx(),
557+
span,
558+
E0381,
559+
"{used} binding {desc}{isnt_initialized}"
560+
);
555561
use_spans.var_path_only_subdiag(&mut err, desired_action);
556562

557563
if let InitializationRequiringAction::PartialAssignment
@@ -2219,11 +2225,11 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
22192225
);
22202226

22212227
self.thread_local_value_does_not_live_long_enough(borrow_span)
2222-
.span_label_mv(
2228+
.with_span_label(
22232229
borrow_span,
22242230
"thread-local variables cannot be borrowed beyond the end of the function",
22252231
)
2226-
.span_label_mv(drop_span, "end of enclosing function is here")
2232+
.with_span_label(drop_span, "end of enclosing function is here")
22272233
}
22282234

22292235
#[instrument(level = "debug", skip(self))]

0 commit comments

Comments
 (0)