Skip to content

Commit dbcc5b1

Browse files
committed
Prefer DiagnosticBuilder over Diagnostic in diagnostic modifiers.
There are lots of functions that modify a diagnostic. This can be via a `&mut Diagnostic` or a `&mut DiagnosticBuilder`, because the latter type wraps the former and impls `DerefMut`. This commit converts all the `&mut Diagnostic` occurrences to `&mut DiagnosticBuilder`. This is a step towards greatly simplifying `Diagnostic`. Some of the relevant function are made generic, because they deal with both errors and warnings. No function bodies are changed, because all the modifier methods are available on both `Diagnostic` and `DiagnosticBuilder`.
1 parent b381d3a commit dbcc5b1

Some content is hidden

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

66 files changed

+597
-505
lines changed

compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@
66
use either::Either;
77
use rustc_data_structures::captures::Captures;
88
use rustc_data_structures::fx::FxIndexSet;
9-
use rustc_errors::{
10-
codes::*, struct_span_code_err, Applicability, Diagnostic, DiagnosticBuilder, MultiSpan,
11-
};
9+
use rustc_errors::{codes::*, struct_span_code_err, Applicability, DiagnosticBuilder, MultiSpan};
1210
use rustc_hir as hir;
1311
use rustc_hir::def::{DefKind, Res};
1412
use rustc_hir::intravisit::{walk_block, walk_expr, Visitor};
@@ -629,7 +627,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
629627

630628
fn suggest_assign_value(
631629
&self,
632-
err: &mut Diagnostic,
630+
err: &mut DiagnosticBuilder<'_>,
633631
moved_place: PlaceRef<'tcx>,
634632
sugg_span: Span,
635633
) {
@@ -668,7 +666,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
668666

669667
fn suggest_borrow_fn_like(
670668
&self,
671-
err: &mut Diagnostic,
669+
err: &mut DiagnosticBuilder<'_>,
672670
ty: Ty<'tcx>,
673671
move_sites: &[MoveSite],
674672
value_name: &str,
@@ -736,7 +734,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
736734

737735
fn suggest_cloning(
738736
&self,
739-
err: &mut Diagnostic,
737+
err: &mut DiagnosticBuilder<'_>,
740738
ty: Ty<'tcx>,
741739
expr: &hir::Expr<'_>,
742740
span: Span,
@@ -772,7 +770,12 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
772770
}
773771
}
774772

775-
fn suggest_adding_copy_bounds(&self, err: &mut Diagnostic, ty: Ty<'tcx>, span: Span) {
773+
fn suggest_adding_copy_bounds(
774+
&self,
775+
err: &mut DiagnosticBuilder<'_>,
776+
ty: Ty<'tcx>,
777+
span: Span,
778+
) {
776779
let tcx = self.infcx.tcx;
777780
let generics = tcx.generics_of(self.mir_def_id());
778781

@@ -1206,7 +1209,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
12061209
#[instrument(level = "debug", skip(self, err))]
12071210
fn suggest_using_local_if_applicable(
12081211
&self,
1209-
err: &mut Diagnostic,
1212+
err: &mut DiagnosticBuilder<'_>,
12101213
location: Location,
12111214
issued_borrow: &BorrowData<'tcx>,
12121215
explanation: BorrowExplanation<'tcx>,
@@ -1302,7 +1305,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
13021305

13031306
fn suggest_slice_method_if_applicable(
13041307
&self,
1305-
err: &mut Diagnostic,
1308+
err: &mut DiagnosticBuilder<'_>,
13061309
place: Place<'tcx>,
13071310
borrowed_place: Place<'tcx>,
13081311
) {
@@ -1411,7 +1414,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
14111414
/// ```
14121415
pub(crate) fn explain_iterator_advancement_in_for_loop_if_applicable(
14131416
&self,
1414-
err: &mut Diagnostic,
1417+
err: &mut DiagnosticBuilder<'_>,
14151418
span: Span,
14161419
issued_spans: &UseSpans<'tcx>,
14171420
) {
@@ -1598,7 +1601,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
15981601
/// ```
15991602
fn suggest_using_closure_argument_instead_of_capture(
16001603
&self,
1601-
err: &mut Diagnostic,
1604+
err: &mut DiagnosticBuilder<'_>,
16021605
borrowed_place: Place<'tcx>,
16031606
issued_spans: &UseSpans<'tcx>,
16041607
) {
@@ -1732,7 +1735,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
17321735

17331736
fn suggest_binding_for_closure_capture_self(
17341737
&self,
1735-
err: &mut Diagnostic,
1738+
err: &mut DiagnosticBuilder<'_>,
17361739
issued_spans: &UseSpans<'tcx>,
17371740
) {
17381741
let UseSpans::ClosureUse { capture_kind_span, .. } = issued_spans else { return };
@@ -2978,7 +2981,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
29782981
self.buffer_error(err);
29792982
}
29802983

2981-
fn explain_deref_coercion(&mut self, loan: &BorrowData<'tcx>, err: &mut Diagnostic) {
2984+
fn explain_deref_coercion(&mut self, loan: &BorrowData<'tcx>, err: &mut DiagnosticBuilder<'_>) {
29822985
let tcx = self.infcx.tcx;
29832986
if let (
29842987
Some(Terminator {
@@ -3513,7 +3516,11 @@ enum AnnotatedBorrowFnSignature<'tcx> {
35133516
impl<'tcx> AnnotatedBorrowFnSignature<'tcx> {
35143517
/// Annotate the provided diagnostic with information about borrow from the fn signature that
35153518
/// helps explain.
3516-
pub(crate) fn emit(&self, cx: &mut MirBorrowckCtxt<'_, 'tcx>, diag: &mut Diagnostic) -> String {
3519+
pub(crate) fn emit(
3520+
&self,
3521+
cx: &mut MirBorrowckCtxt<'_, 'tcx>,
3522+
diag: &mut DiagnosticBuilder<'_>,
3523+
) -> String {
35173524
match self {
35183525
&AnnotatedBorrowFnSignature::Closure { argument_ty, argument_span } => {
35193526
diag.span_label(

compiler/rustc_borrowck/src/diagnostics/explain_borrow.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
#![allow(rustc::diagnostic_outside_of_impl)]
44
#![allow(rustc::untranslatable_diagnostic)]
55

6-
use rustc_errors::{Applicability, Diagnostic};
6+
use rustc_errors::{Applicability, DiagnosticBuilder};
77
use rustc_hir as hir;
88
use rustc_hir::intravisit::Visitor;
99
use rustc_index::IndexSlice;
@@ -65,7 +65,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
6565
tcx: TyCtxt<'tcx>,
6666
body: &Body<'tcx>,
6767
local_names: &IndexSlice<Local, Option<Symbol>>,
68-
err: &mut Diagnostic,
68+
err: &mut DiagnosticBuilder<'_>,
6969
borrow_desc: &str,
7070
borrow_span: Option<Span>,
7171
multiple_borrow_span: Option<(Span, Span)>,
@@ -306,7 +306,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
306306
fn add_object_lifetime_default_note(
307307
&self,
308308
tcx: TyCtxt<'tcx>,
309-
err: &mut Diagnostic,
309+
err: &mut DiagnosticBuilder<'_>,
310310
unsize_ty: Ty<'tcx>,
311311
) {
312312
if let ty::Adt(def, args) = unsize_ty.kind() {
@@ -359,7 +359,7 @@ impl<'tcx> BorrowExplanation<'tcx> {
359359

360360
fn add_lifetime_bound_suggestion_to_diagnostic(
361361
&self,
362-
err: &mut Diagnostic,
362+
err: &mut DiagnosticBuilder<'_>,
363363
category: &ConstraintCategory<'tcx>,
364364
span: Span,
365365
region_name: &RegionName,

compiler/rustc_borrowck/src/diagnostics/mod.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::session_diagnostics::{
55
CaptureVarKind, CaptureVarPathUseCause, OnClosureNote,
66
};
77
use itertools::Itertools;
8-
use rustc_errors::{Applicability, Diagnostic};
8+
use rustc_errors::{Applicability, DiagnosticBuilder};
99
use rustc_hir as hir;
1010
use rustc_hir::def::{CtorKind, Namespace};
1111
use rustc_hir::CoroutineKind;
@@ -80,7 +80,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
8080
&self,
8181
location: Location,
8282
place: PlaceRef<'tcx>,
83-
diag: &mut Diagnostic,
83+
diag: &mut DiagnosticBuilder<'_>,
8484
) -> bool {
8585
debug!("add_moved_or_invoked_closure_note: location={:?} place={:?}", location, place);
8686
let mut target = place.local_or_deref_local();
@@ -587,7 +587,7 @@ impl UseSpans<'_> {
587587
/// Add a span label to the arguments of the closure, if it exists.
588588
pub(super) fn args_subdiag(
589589
self,
590-
err: &mut Diagnostic,
590+
err: &mut DiagnosticBuilder<'_>,
591591
f: impl FnOnce(Span) -> CaptureArgLabel,
592592
) {
593593
if let UseSpans::ClosureUse { args_span, .. } = self {
@@ -599,7 +599,7 @@ impl UseSpans<'_> {
599599
/// only adds label to the `path_span`
600600
pub(super) fn var_path_only_subdiag(
601601
self,
602-
err: &mut Diagnostic,
602+
err: &mut DiagnosticBuilder<'_>,
603603
action: crate::InitializationRequiringAction,
604604
) {
605605
use crate::InitializationRequiringAction::*;
@@ -630,7 +630,7 @@ impl UseSpans<'_> {
630630
pub(super) fn var_subdiag(
631631
self,
632632
dcx: Option<&rustc_errors::DiagCtxt>,
633-
err: &mut Diagnostic,
633+
err: &mut DiagnosticBuilder<'_>,
634634
kind: Option<rustc_middle::mir::BorrowKind>,
635635
f: impl FnOnce(hir::ClosureKind, Span) -> CaptureVarCause,
636636
) {
@@ -1002,7 +1002,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10021002

10031003
fn explain_captures(
10041004
&mut self,
1005-
err: &mut Diagnostic,
1005+
err: &mut DiagnosticBuilder<'_>,
10061006
span: Span,
10071007
move_span: Span,
10081008
move_spans: UseSpans<'tcx>,

compiler/rustc_borrowck/src/diagnostics/move_errors.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#![allow(rustc::diagnostic_outside_of_impl)]
22
#![allow(rustc::untranslatable_diagnostic)]
33

4-
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder};
4+
use rustc_errors::{Applicability, DiagnosticBuilder};
55
use rustc_middle::mir::*;
66
use rustc_middle::ty::{self, Ty};
77
use rustc_mir_dataflow::move_paths::{LookupResult, MovePathIndex};
@@ -437,7 +437,12 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
437437
err
438438
}
439439

440-
fn add_move_hints(&self, error: GroupedMoveError<'tcx>, err: &mut Diagnostic, span: Span) {
440+
fn add_move_hints(
441+
&self,
442+
error: GroupedMoveError<'tcx>,
443+
err: &mut DiagnosticBuilder<'_>,
444+
span: Span,
445+
) {
441446
match error {
442447
GroupedMoveError::MovesFromPlace { mut binds_to, move_from, .. } => {
443448
self.add_borrow_suggestions(err, span);
@@ -494,7 +499,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
494499
}
495500
}
496501

497-
fn add_borrow_suggestions(&self, err: &mut Diagnostic, span: Span) {
502+
fn add_borrow_suggestions(&self, err: &mut DiagnosticBuilder<'_>, span: Span) {
498503
match self.infcx.tcx.sess.source_map().span_to_snippet(span) {
499504
Ok(snippet) if snippet.starts_with('*') => {
500505
err.span_suggestion_verbose(
@@ -515,7 +520,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
515520
}
516521
}
517522

518-
fn add_move_error_suggestions(&self, err: &mut Diagnostic, binds_to: &[Local]) {
523+
fn add_move_error_suggestions(&self, err: &mut DiagnosticBuilder<'_>, binds_to: &[Local]) {
519524
let mut suggestions: Vec<(Span, String, String)> = Vec::new();
520525
for local in binds_to {
521526
let bind_to = &self.body.local_decls[*local];
@@ -567,7 +572,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
567572
}
568573
}
569574

570-
fn add_move_error_details(&self, err: &mut Diagnostic, binds_to: &[Local]) {
575+
fn add_move_error_details(&self, err: &mut DiagnosticBuilder<'_>, binds_to: &[Local]) {
571576
for (j, local) in binds_to.iter().enumerate() {
572577
let bind_to = &self.body.local_decls[*local];
573578
let binding_span = bind_to.source_info.span;
@@ -601,7 +606,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
601606
/// expansion of a packed struct.
602607
/// Such errors happen because derive macro expansions shy away from taking
603608
/// references to the struct's fields since doing so would be undefined behaviour
604-
fn add_note_for_packed_struct_derive(&self, err: &mut Diagnostic, local: Local) {
609+
fn add_note_for_packed_struct_derive(&self, err: &mut DiagnosticBuilder<'_>, local: Local) {
605610
let local_place: PlaceRef<'tcx> = local.into();
606611
let local_ty = local_place.ty(self.body.local_decls(), self.infcx.tcx).ty.peel_refs();
607612

compiler/rustc_borrowck/src/diagnostics/mutability_errors.rs

+11-6
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#![allow(rustc::untranslatable_diagnostic)]
33

44
use hir::ExprKind;
5-
use rustc_errors::{Applicability, Diagnostic, DiagnosticBuilder};
5+
use rustc_errors::{Applicability, DiagnosticBuilder};
66
use rustc_hir as hir;
77
use rustc_hir::intravisit::Visitor;
88
use rustc_hir::Node;
@@ -542,15 +542,20 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
542542
}
543543
}
544544

545-
fn suggest_map_index_mut_alternatives(&self, ty: Ty<'tcx>, err: &mut Diagnostic, span: Span) {
545+
fn suggest_map_index_mut_alternatives(
546+
&self,
547+
ty: Ty<'tcx>,
548+
err: &mut DiagnosticBuilder<'tcx>,
549+
span: Span,
550+
) {
546551
let Some(adt) = ty.ty_adt_def() else { return };
547552
let did = adt.did();
548553
if self.infcx.tcx.is_diagnostic_item(sym::HashMap, did)
549554
|| self.infcx.tcx.is_diagnostic_item(sym::BTreeMap, did)
550555
{
551556
struct V<'a, 'tcx> {
552557
assign_span: Span,
553-
err: &'a mut Diagnostic,
558+
err: &'a mut DiagnosticBuilder<'tcx>,
554559
ty: Ty<'tcx>,
555560
suggested: bool,
556561
}
@@ -792,7 +797,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
792797
tcx: TyCtxt<'_>,
793798
closure_local_def_id: hir::def_id::LocalDefId,
794799
the_place_err: PlaceRef<'tcx>,
795-
err: &mut Diagnostic,
800+
err: &mut DiagnosticBuilder<'_>,
796801
) {
797802
let tables = tcx.typeck(closure_local_def_id);
798803
if let Some((span, closure_kind_origin)) = tcx.closure_kind_origin(closure_local_def_id) {
@@ -854,7 +859,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
854859

855860
// Attempt to search similar mutable associated items for suggestion.
856861
// In the future, attempt in all path but initially for RHS of for_loop
857-
fn suggest_similar_mut_method_for_for_loop(&self, err: &mut Diagnostic, span: Span) {
862+
fn suggest_similar_mut_method_for_for_loop(&self, err: &mut DiagnosticBuilder<'_>, span: Span) {
858863
use hir::{
859864
BorrowKind, Expr,
860865
ExprKind::{AddrOf, Block, Call, MethodCall},
@@ -938,7 +943,7 @@ impl<'a, 'tcx> MirBorrowckCtxt<'a, 'tcx> {
938943
}
939944

940945
/// Targeted error when encountering an `FnMut` closure where an `Fn` closure was expected.
941-
fn expected_fn_found_fn_mut_call(&self, err: &mut Diagnostic, sp: Span, act: &str) {
946+
fn expected_fn_found_fn_mut_call(&self, err: &mut DiagnosticBuilder<'_>, sp: Span, act: &str) {
942947
err.span_label(sp, format!("cannot {act}"));
943948

944949
let hir = self.infcx.tcx.hir();

compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
#![allow(rustc::untranslatable_diagnostic)]
66

77
use rustc_data_structures::fx::FxIndexSet;
8-
use rustc_errors::Diagnostic;
8+
use rustc_errors::DiagnosticBuilder;
99
use rustc_middle::ty::RegionVid;
1010
use smallvec::SmallVec;
1111
use std::collections::BTreeMap;
@@ -158,13 +158,13 @@ impl OutlivesSuggestionBuilder {
158158
self.constraints_to_add.entry(fr).or_default().push(outlived_fr);
159159
}
160160

161-
/// Emit an intermediate note on the given `Diagnostic` if the involved regions are
161+
/// Emit an intermediate note on the given `DiagnosticBuilder` if the involved regions are
162162
/// suggestable.
163163
pub(crate) fn intermediate_suggestion(
164164
&mut self,
165165
mbcx: &MirBorrowckCtxt<'_, '_>,
166166
errci: &ErrorConstraintInfo<'_>,
167-
diag: &mut Diagnostic,
167+
diag: &mut DiagnosticBuilder<'_>,
168168
) {
169169
// Emit an intermediate note.
170170
let fr_name = self.region_vid_to_name(mbcx, errci.fr);

0 commit comments

Comments
 (0)