Skip to content

Commit 8084d72

Browse files
committed
Remove DiagInner::sort_span
It's a field that can be used for sorting diagnostics. By default it is set to the primary span. It's only used for `BorrowckDiagnosticsBuffer`. This commit removes it and adds a `sort_span` to each diagnostic recorded in `BorrowckDiagnosticsBuffer`. This avoids various other pieces of code having to deal with it.
1 parent 6bb1652 commit 8084d72

5 files changed

Lines changed: 17 additions & 23 deletions

File tree

‎compiler/rustc_borrowck/src/diagnostics/mod.rs‎

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,19 @@ pub(crate) struct BorrowckDiagnosticsBuffer<'diag, 'tcx> {
9494

9595
buffered_mut_errors: FxIndexMap<Span, (Diag<'diag>, usize)>,
9696

97-
/// Buffer of diagnostics to be reported.
98-
buffered_diags: Vec<Diag<'diag>>,
97+
/// Buffer of diagnostics to be reported. Each one is paired with a span for sorting purposes;
98+
/// by default it's the primary span.
99+
buffered_diags: Vec<(Span, Diag<'diag>)>,
99100
}
100101

101102
impl<'diag, 'tcx> BorrowckDiagnosticsBuffer<'diag, 'tcx> {
102103
pub(crate) fn buffer_error(&mut self, diag: Diag<'diag>) {
103-
self.buffered_diags.push(diag);
104+
let sort_span = diag.span.primary_span().unwrap_or(DUMMY_SP);
105+
self.buffered_diags.push((sort_span, diag));
106+
}
107+
108+
pub(crate) fn buffer_error_with_sort_span(&mut self, diag: Diag<'diag>, sort_span: Span) {
109+
self.buffered_diags.push((sort_span, diag));
104110
}
105111

106112
pub(crate) fn emit_errors(&mut self) {
@@ -117,8 +123,8 @@ impl<'diag, 'tcx> BorrowckDiagnosticsBuffer<'diag, 'tcx> {
117123
}
118124

119125
if !self.buffered_diags.is_empty() {
120-
self.buffered_diags.sort_by_key(|diag| diag.sort_span);
121-
for diag in self.buffered_diags.drain(..) {
126+
self.buffered_diags.sort_by_key(|(sort_span, _)| *sort_span);
127+
for (_, diag) in self.buffered_diags.drain(..) {
122128
diag.emit();
123129
}
124130
}
@@ -130,6 +136,10 @@ impl<'diag, 'tcx> MirBorrowckCtxt<'_, 'diag, 'tcx> {
130136
self.diags_buffer.buffer_error(diag.with_dcx(self.dcx()));
131137
}
132138

139+
pub(crate) fn buffer_error_with_sort_span(&mut self, diag: Diag<'_>, sort_span: Span) {
140+
self.diags_buffer.buffer_error_with_sort_span(diag.with_dcx(self.dcx()), sort_span);
141+
}
142+
133143
pub(crate) fn buffer_move_error(
134144
&mut self,
135145
move_out_indices: Vec<MoveOutIndex>,

‎compiler/rustc_borrowck/src/diagnostics/outlives_suggestion.rs‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,8 @@ impl OutlivesSuggestionBuilder {
220220

221221
// We want this message to appear after other messages on the mir def.
222222
let mir_span = mbcx.body.span;
223-
diag.sort_span = mir_span.shrink_to_hi();
224223

225224
// Buffer the diagnostic
226-
mbcx.buffer_error(diag);
225+
mbcx.buffer_error_with_sort_span(diag, mir_span.shrink_to_hi());
227226
}
228227
}

‎compiler/rustc_codegen_ssa/src/back/write.rs‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,7 +1199,6 @@ pub struct CguMessage;
11991199
// - `span`: it doesn't impl `Send`.
12001200
// - `suggestions`: it doesn't impl `Send`, and isn't used for codegen
12011201
// diagnostics.
1202-
// - `sort_span`: it doesn't impl `Send`.
12031202
// - `is_lint`: lints aren't relevant during codegen.
12041203
// - `emitted_at`: not used for codegen diagnostics.
12051204
struct Diagnostic {
@@ -1993,7 +1992,6 @@ impl Emitter for SharedEmitter {
19931992
// the cut-down local `DiagInner`.
19941993
assert!(!diag.span.has_span_labels());
19951994
assert_eq!(diag.suggestions, Suggestions::Enabled(vec![]));
1996-
assert_eq!(diag.sort_span, rustc_span::DUMMY_SP);
19971995
assert_eq!(diag.is_lint, None);
19981996
// No sensible check for `diag.emitted_at`.
19991997

‎compiler/rustc_errors/src/diagnostic.rs‎

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use rustc_error_messages::{DiagArgMap, DiagArgName, IntoDiagArg};
1212
use rustc_hashes::Hash128;
1313
use rustc_lint_defs::{Applicability, LintExpectationId};
1414
use rustc_macros::{Decodable, Encodable};
15-
use rustc_span::{DUMMY_SP, Span, Spanned, Symbol};
15+
use rustc_span::{Span, Spanned, Symbol};
1616
use tracing::debug;
1717

1818
use crate::{
@@ -195,14 +195,7 @@ pub struct DiagInner {
195195
pub children: Vec<Subdiag>,
196196
pub suggestions: Suggestions,
197197
pub args: DiagArgMap,
198-
199-
/// This is not used for highlighting or rendering any error message. Rather, it can be used
200-
/// as a sort key to sort a buffer of diagnostics. By default, it is the primary span of
201-
/// `span` if there is one. Otherwise, it is `DUMMY_SP`.
202-
pub sort_span: Span,
203-
204198
pub is_lint: Option<IsLint>,
205-
206199
pub long_ty_path: Option<PathBuf>,
207200
/// With `-Ztrack_diagnostics` enabled,
208201
/// we print where in rustc this error was emitted.
@@ -226,7 +219,6 @@ impl DiagInner {
226219
children: vec![],
227220
suggestions: Suggestions::Enabled(vec![]),
228221
args: Default::default(),
229-
sort_span: DUMMY_SP,
230222
is_lint: None,
231223
long_ty_path: None,
232224
emitted_at: DiagLocation::caller(),
@@ -320,7 +312,6 @@ impl DiagInner {
320312
children,
321313
suggestions,
322314
args,
323-
sort_span: _, // ignore
324315
is_lint,
325316
long_ty_path: _, // ignore
326317
emitted_at: _, // ignore
@@ -1088,9 +1079,6 @@ impl<'a> Diag<'a> {
10881079
/// Add a span.
10891080
pub fn span(&mut self, sp: impl Into<MultiSpan>) -> &mut Self {
10901081
self.span = sp.into();
1091-
if let Some(span) = self.span.primary_span() {
1092-
self.sort_span = span;
1093-
}
10941082
self
10951083
} }
10961084

‎compiler/rustc_resolve/src/late.rs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4751,7 +4751,6 @@ impl<'a, 'ast, 'ra, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
47514751
} else {
47524752
err.children.append(&mut parent_err.children);
47534753
}
4754-
err.sort_span = parent_err.sort_span;
47554754
err.is_lint = parent_err.is_lint.clone();
47564755

47574756
// merge the parent_err's suggestions with the typo (err's) suggestions

0 commit comments

Comments
 (0)