Skip to content

Commit f5387a1

Browse files
authored
Rollup merge of #119841 - nnethercote:rm-DiagnosticBuilder-buffer, r=oli-obk
Remove `DiagnosticBuilder::buffer` `DiagnosticBuilder::buffer` doesn't do much, and part of what it does (for `-Ztreat-err-as-bug`) it shouldn't. This PR strips it back, replaces its uses, and finally removes it, making a few cleanups in the vicinity along the way. r? ``@oli-obk``
2 parents 6beb676 + 4fd1db1 commit f5387a1

File tree

20 files changed

+118
-158
lines changed

20 files changed

+118
-158
lines changed

compiler/rustc_borrowck/src/lib.rs

+10-9
Original file line numberDiff line numberDiff line change
@@ -2399,18 +2399,19 @@ mod error {
23992399
/// and we want only the best of those errors.
24002400
///
24012401
/// The `report_use_of_moved_or_uninitialized` function checks this map and replaces the
2402-
/// diagnostic (if there is one) if the `Place` of the error being reported is a prefix of the
2403-
/// `Place` of the previous most diagnostic. This happens instead of buffering the error. Once
2404-
/// all move errors have been reported, any diagnostics in this map are added to the buffer
2405-
/// to be emitted.
2402+
/// diagnostic (if there is one) if the `Place` of the error being reported is a prefix of
2403+
/// the `Place` of the previous most diagnostic. This happens instead of buffering the
2404+
/// error. Once all move errors have been reported, any diagnostics in this map are added
2405+
/// to the buffer to be emitted.
24062406
///
24072407
/// `BTreeMap` is used to preserve the order of insertions when iterating. This is necessary
24082408
/// when errors in the map are being re-added to the error buffer so that errors with the
24092409
/// same primary span come out in a consistent order.
24102410
buffered_move_errors:
24112411
BTreeMap<Vec<MoveOutIndex>, (PlaceRef<'tcx>, DiagnosticBuilder<'tcx>)>,
24122412
buffered_mut_errors: FxIndexMap<Span, (DiagnosticBuilder<'tcx>, usize)>,
2413-
/// Diagnostics to be reported buffer.
2413+
/// Buffer of diagnostics to be reported. Uses `Diagnostic` rather than `DiagnosticBuilder`
2414+
/// because it has a mixture of error diagnostics and non-error diagnostics.
24142415
buffered: Vec<Diagnostic>,
24152416
/// Set to Some if we emit an error during borrowck
24162417
tainted_by_errors: Option<ErrorGuaranteed>,
@@ -2434,11 +2435,11 @@ mod error {
24342435
"diagnostic buffered but not emitted",
24352436
))
24362437
}
2437-
t.buffer(&mut self.buffered);
2438+
self.buffered.push(t.into_diagnostic());
24382439
}
24392440

24402441
pub fn buffer_non_error_diag(&mut self, t: DiagnosticBuilder<'_, ()>) {
2441-
t.buffer(&mut self.buffered);
2442+
self.buffered.push(t.into_diagnostic());
24422443
}
24432444

24442445
pub fn set_tainted_by_errors(&mut self, e: ErrorGuaranteed) {
@@ -2486,13 +2487,13 @@ mod error {
24862487
// Buffer any move errors that we collected and de-duplicated.
24872488
for (_, (_, diag)) in std::mem::take(&mut self.errors.buffered_move_errors) {
24882489
// We have already set tainted for this error, so just buffer it.
2489-
diag.buffer(&mut self.errors.buffered);
2490+
self.errors.buffered.push(diag.into_diagnostic());
24902491
}
24912492
for (_, (mut diag, count)) in std::mem::take(&mut self.errors.buffered_mut_errors) {
24922493
if count > 10 {
24932494
diag.note(format!("...and {} other attempted mutable borrows", count - 10));
24942495
}
2495-
diag.buffer(&mut self.errors.buffered);
2496+
self.errors.buffered.push(diag.into_diagnostic());
24962497
}
24972498

24982499
if !self.errors.buffered.is_empty() {

compiler/rustc_const_eval/src/transform/check_consts/check.rs

+8-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! The `Visitor` responsible for actually checking a `mir::Body` for invalid operations.
22
3-
use rustc_errors::{Diagnostic, ErrorGuaranteed};
3+
use rustc_errors::{DiagnosticBuilder, ErrorGuaranteed};
44
use rustc_hir as hir;
55
use rustc_hir::def_id::DefId;
66
use rustc_index::bit_set::BitSet;
@@ -214,7 +214,7 @@ pub struct Checker<'mir, 'tcx> {
214214
local_has_storage_dead: Option<BitSet<Local>>,
215215

216216
error_emitted: Option<ErrorGuaranteed>,
217-
secondary_errors: Vec<Diagnostic>,
217+
secondary_errors: Vec<DiagnosticBuilder<'tcx>>,
218218
}
219219

220220
impl<'mir, 'tcx> Deref for Checker<'mir, 'tcx> {
@@ -272,14 +272,17 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
272272
}
273273

274274
// If we got through const-checking without emitting any "primary" errors, emit any
275-
// "secondary" errors if they occurred.
275+
// "secondary" errors if they occurred. Otherwise, cancel the "secondary" errors.
276276
let secondary_errors = mem::take(&mut self.secondary_errors);
277277
if self.error_emitted.is_none() {
278278
for error in secondary_errors {
279-
self.tcx.dcx().emit_diagnostic(error);
279+
error.emit();
280280
}
281281
} else {
282282
assert!(self.tcx.dcx().has_errors().is_some());
283+
for error in secondary_errors {
284+
error.cancel();
285+
}
283286
}
284287
}
285288

@@ -347,7 +350,7 @@ impl<'mir, 'tcx> Checker<'mir, 'tcx> {
347350
self.error_emitted = Some(reported);
348351
}
349352

350-
ops::DiagnosticImportance::Secondary => err.buffer(&mut self.secondary_errors),
353+
ops::DiagnosticImportance::Secondary => self.secondary_errors.push(err),
351354
}
352355
}
353356

compiler/rustc_errors/src/diagnostic_builder.rs

+4-26
Original file line numberDiff line numberDiff line change
@@ -255,35 +255,13 @@ impl<'a, G: EmissionGuarantee> DiagnosticBuilder<'a, G> {
255255
/// Stashes diagnostic for possible later improvement in a different,
256256
/// later stage of the compiler. The diagnostic can be accessed with
257257
/// the provided `span` and `key` through [`DiagCtxt::steal_diagnostic()`].
258-
///
259-
/// As with `buffer`, this is unless the dcx has disabled such buffering.
260258
pub fn stash(self, span: Span, key: StashKey) {
261-
if let Some((diag, dcx)) = self.into_diagnostic() {
262-
dcx.stash_diagnostic(span, key, diag);
263-
}
264-
}
265-
266-
/// Converts the builder to a `Diagnostic` for later emission,
267-
/// unless dcx has disabled such buffering.
268-
fn into_diagnostic(mut self) -> Option<(Diagnostic, &'a DiagCtxt)> {
269-
if self.dcx.inner.lock().flags.treat_err_as_bug.is_some() {
270-
self.emit();
271-
return None;
272-
}
273-
274-
let diag = self.take_diag();
275-
276-
// Logging here is useful to help track down where in logs an error was
277-
// actually emitted.
278-
debug!("buffer: diag={:?}", diag);
279-
280-
Some((diag, self.dcx))
259+
self.dcx.stash_diagnostic(span, key, self.into_diagnostic());
281260
}
282261

283-
/// Buffers the diagnostic for later emission,
284-
/// unless dcx has disabled such buffering.
285-
pub fn buffer(self, buffered_diagnostics: &mut Vec<Diagnostic>) {
286-
buffered_diagnostics.extend(self.into_diagnostic().map(|(diag, _)| diag));
262+
/// Converts the builder to a `Diagnostic` for later emission.
263+
pub fn into_diagnostic(mut self) -> Diagnostic {
264+
self.take_diag()
287265
}
288266

289267
/// Delay emission of this diagnostic as a bug.

compiler/rustc_errors/src/lib.rs

+10-17
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,10 @@ impl DiagCtxt {
981981

982982
inner.emit_stashed_diagnostics();
983983

984+
if inner.treat_err_as_bug() {
985+
return;
986+
}
987+
984988
let warnings = match inner.deduplicated_warn_count {
985989
0 => Cow::from(""),
986990
1 => Cow::from("1 warning emitted"),
@@ -991,9 +995,6 @@ impl DiagCtxt {
991995
1 => Cow::from("aborting due to 1 previous error"),
992996
count => Cow::from(format!("aborting due to {count} previous errors")),
993997
};
994-
if inner.treat_err_as_bug() {
995-
return;
996-
}
997998

998999
match (errors.len(), warnings.len()) {
9991000
(0, 0) => return,
@@ -1168,7 +1169,8 @@ impl DiagCtxt {
11681169
let mut inner = self.inner.borrow_mut();
11691170

11701171
if loud && lint_level.is_error() {
1171-
inner.bump_err_count();
1172+
inner.err_count += 1;
1173+
inner.panic_if_treat_err_as_bug();
11721174
}
11731175

11741176
inner.emitter.emit_unused_externs(lint_level, unused_externs)
@@ -1255,7 +1257,7 @@ impl DiagCtxtInner {
12551257
}
12561258

12571259
fn emit_diagnostic(&mut self, mut diagnostic: Diagnostic) -> Option<ErrorGuaranteed> {
1258-
if matches!(diagnostic.level, Error | Fatal) && self.treat_err_as_bug() {
1260+
if matches!(diagnostic.level, Error | Fatal) && self.treat_next_err_as_bug() {
12591261
diagnostic.level = Bug;
12601262
}
12611263

@@ -1353,10 +1355,11 @@ impl DiagCtxtInner {
13531355
}
13541356
if diagnostic.is_error() {
13551357
if diagnostic.is_lint {
1356-
self.bump_lint_err_count();
1358+
self.lint_err_count += 1;
13571359
} else {
1358-
self.bump_err_count();
1360+
self.err_count += 1;
13591361
}
1362+
self.panic_if_treat_err_as_bug();
13601363

13611364
#[allow(deprecated)]
13621365
{
@@ -1447,16 +1450,6 @@ impl DiagCtxtInner {
14471450
panic::panic_any(DelayedBugPanic);
14481451
}
14491452

1450-
fn bump_lint_err_count(&mut self) {
1451-
self.lint_err_count += 1;
1452-
self.panic_if_treat_err_as_bug();
1453-
}
1454-
1455-
fn bump_err_count(&mut self) {
1456-
self.err_count += 1;
1457-
self.panic_if_treat_err_as_bug();
1458-
}
1459-
14601453
fn panic_if_treat_err_as_bug(&self) {
14611454
if self.treat_err_as_bug() {
14621455
match (

compiler/rustc_hir_typeck/src/writeback.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -498,14 +498,14 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
498498
// order when emitting them.
499499
let err =
500500
self.tcx().dcx().struct_span_err(span, format!("user args: {user_args:?}"));
501-
err.buffer(&mut errors_buffer);
501+
errors_buffer.push(err);
502502
}
503503
}
504504

505505
if !errors_buffer.is_empty() {
506506
errors_buffer.sort_by_key(|diag| diag.span.primary_span());
507-
for diag in errors_buffer {
508-
self.tcx().dcx().emit_diagnostic(diag);
507+
for err in errors_buffer {
508+
err.emit();
509509
}
510510
}
511511
}

compiler/rustc_interface/src/interface.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub(crate) fn parse_cfg(dcx: &DiagCtxt, cfgs: Vec<String>) -> Cfg {
8282
Ok(..) => {}
8383
Err(err) => err.cancel(),
8484
},
85-
Err(errs) => drop(errs),
85+
Err(errs) => errs.into_iter().for_each(|err| err.cancel()),
8686
}
8787

8888
// If the user tried to use a key="value" flag, but is missing the quotes, provide
@@ -129,9 +129,12 @@ pub(crate) fn parse_check_cfg(dcx: &DiagCtxt, specs: Vec<String>) -> CheckCfg {
129129
error!("expected `cfg(name, values(\"value1\", \"value2\", ... \"valueN\"))`")
130130
};
131131

132-
let Ok(mut parser) = maybe_new_parser_from_source_str(&sess, filename, s.to_string())
133-
else {
134-
expected_error();
132+
let mut parser = match maybe_new_parser_from_source_str(&sess, filename, s.to_string()) {
133+
Ok(parser) => parser,
134+
Err(errs) => {
135+
errs.into_iter().for_each(|err| err.cancel());
136+
expected_error();
137+
}
135138
};
136139

137140
let meta_item = match parser.parse_meta_item() {

compiler/rustc_parse/src/lexer/mod.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_ast::ast::{self, AttrStyle};
77
use rustc_ast::token::{self, CommentKind, Delimiter, Token, TokenKind};
88
use rustc_ast::tokenstream::TokenStream;
99
use rustc_ast::util::unicode::contains_text_flow_control_chars;
10-
use rustc_errors::{error_code, Applicability, DiagCtxt, Diagnostic, StashKey};
10+
use rustc_errors::{error_code, Applicability, DiagCtxt, DiagnosticBuilder, StashKey};
1111
use rustc_lexer::unescape::{self, EscapeError, Mode};
1212
use rustc_lexer::{Base, DocStyle, RawStrError};
1313
use rustc_lexer::{Cursor, LiteralKind};
@@ -42,12 +42,12 @@ pub struct UnmatchedDelim {
4242
pub candidate_span: Option<Span>,
4343
}
4444

45-
pub(crate) fn parse_token_trees<'a>(
46-
sess: &'a ParseSess,
47-
mut src: &'a str,
45+
pub(crate) fn parse_token_trees<'sess, 'src>(
46+
sess: &'sess ParseSess,
47+
mut src: &'src str,
4848
mut start_pos: BytePos,
4949
override_span: Option<Span>,
50-
) -> Result<TokenStream, Vec<Diagnostic>> {
50+
) -> Result<TokenStream, Vec<DiagnosticBuilder<'sess>>> {
5151
// Skip `#!`, if present.
5252
if let Some(shebang_len) = rustc_lexer::strip_shebang(src) {
5353
src = &src[shebang_len..];
@@ -76,39 +76,39 @@ pub(crate) fn parse_token_trees<'a>(
7676
let mut buffer = Vec::with_capacity(1);
7777
for unmatched in unmatched_delims {
7878
if let Some(err) = make_unclosed_delims_error(unmatched, sess) {
79-
err.buffer(&mut buffer);
79+
buffer.push(err);
8080
}
8181
}
8282
if let Err(errs) = res {
8383
// Add unclosing delimiter or diff marker errors
8484
for err in errs {
85-
err.buffer(&mut buffer);
85+
buffer.push(err);
8686
}
8787
}
8888
Err(buffer)
8989
}
9090
}
9191
}
9292

93-
struct StringReader<'a> {
94-
sess: &'a ParseSess,
93+
struct StringReader<'sess, 'src> {
94+
sess: &'sess ParseSess,
9595
/// Initial position, read-only.
9696
start_pos: BytePos,
9797
/// The absolute offset within the source_map of the current character.
9898
pos: BytePos,
9999
/// Source text to tokenize.
100-
src: &'a str,
100+
src: &'src str,
101101
/// Cursor for getting lexer tokens.
102-
cursor: Cursor<'a>,
102+
cursor: Cursor<'src>,
103103
override_span: Option<Span>,
104104
/// When a "unknown start of token: \u{a0}" has already been emitted earlier
105105
/// in this file, it's safe to treat further occurrences of the non-breaking
106106
/// space character as whitespace.
107107
nbsp_is_whitespace: bool,
108108
}
109109

110-
impl<'a> StringReader<'a> {
111-
pub fn dcx(&self) -> &'a DiagCtxt {
110+
impl<'sess, 'src> StringReader<'sess, 'src> {
111+
pub fn dcx(&self) -> &'sess DiagCtxt {
112112
&self.sess.dcx
113113
}
114114

@@ -526,7 +526,7 @@ impl<'a> StringReader<'a> {
526526

527527
/// Slice of the source text from `start` up to but excluding `self.pos`,
528528
/// meaning the slice does not include the character `self.ch`.
529-
fn str_from(&self, start: BytePos) -> &'a str {
529+
fn str_from(&self, start: BytePos) -> &'src str {
530530
self.str_from_to(start, self.pos)
531531
}
532532

@@ -537,12 +537,12 @@ impl<'a> StringReader<'a> {
537537
}
538538

539539
/// Slice of the source text spanning from `start` up to but excluding `end`.
540-
fn str_from_to(&self, start: BytePos, end: BytePos) -> &'a str {
540+
fn str_from_to(&self, start: BytePos, end: BytePos) -> &'src str {
541541
&self.src[self.src_index(start)..self.src_index(end)]
542542
}
543543

544544
/// Slice of the source text spanning from `start` until the end
545-
fn str_from_to_end(&self, start: BytePos) -> &'a str {
545+
fn str_from_to_end(&self, start: BytePos) -> &'src str {
546546
&self.src[self.src_index(start)..]
547547
}
548548

0 commit comments

Comments
 (0)