Skip to content

Commit 864f6d1

Browse files
committed
Only emit expanded diagnostic information once
1 parent e76d3f6 commit 864f6d1

File tree

3 files changed

+28
-10
lines changed

3 files changed

+28
-10
lines changed

src/librustc_errors/diagnostic.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ pub struct Diagnostic {
2727
pub suggestions: Vec<CodeSuggestion>,
2828
}
2929

30-
#[derive(Clone, Debug, PartialEq, Hash, RustcEncodable, RustcDecodable)]
30+
#[derive(Clone, Debug, PartialEq, Eq, Hash, RustcEncodable, RustcDecodable)]
3131
pub enum DiagnosticId {
3232
Error(String),
3333
Lint(String),
@@ -281,6 +281,10 @@ impl Diagnostic {
281281
self
282282
}
283283

284+
pub fn get_code(&self) -> Option<DiagnosticId> {
285+
self.code.clone()
286+
}
287+
284288
pub fn message(&self) -> String {
285289
self.message.iter().map(|i| i.0.to_owned()).collect::<String>()
286290
}

src/librustc_errors/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ pub struct Handler {
244244
continue_after_error: Cell<bool>,
245245
delayed_span_bug: RefCell<Option<Diagnostic>>,
246246
tracked_diagnostics: RefCell<Option<Vec<Diagnostic>>>,
247+
tracked_diagnostic_codes: RefCell<FxHashSet<DiagnosticId>>,
247248

248249
// This set contains a hash of every diagnostic that has been emitted by
249250
// this handler. These hashes is used to avoid emitting the same error
@@ -303,6 +304,7 @@ impl Handler {
303304
continue_after_error: Cell::new(true),
304305
delayed_span_bug: RefCell::new(None),
305306
tracked_diagnostics: RefCell::new(None),
307+
tracked_diagnostic_codes: RefCell::new(FxHashSet()),
306308
emitted_diagnostics: RefCell::new(FxHashSet()),
307309
}
308310
}
@@ -575,13 +577,21 @@ impl Handler {
575577
(ret, diagnostics)
576578
}
577579

580+
pub fn code_emitted(&self, code: &DiagnosticId) -> bool {
581+
self.tracked_diagnostic_codes.borrow().contains(code)
582+
}
583+
578584
fn emit_db(&self, db: &DiagnosticBuilder) {
579585
let diagnostic = &**db;
580586

581587
if let Some(ref mut list) = *self.tracked_diagnostics.borrow_mut() {
582588
list.push(diagnostic.clone());
583589
}
584590

591+
if let Some(ref code) = diagnostic.code {
592+
self.tracked_diagnostic_codes.borrow_mut().insert(code.clone());
593+
}
594+
585595
let diagnostic_hash = {
586596
use std::hash::Hash;
587597
let mut hasher = StableHasher::new();

src/librustc_typeck/check/cast.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,18 +290,22 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
290290
self.expr_ty,
291291
fcx.ty_to_string(self.cast_ty)
292292
);
293-
if fcx.tcx.sess.opts.debugging_opts.explain {
293+
if fcx.tcx.sess.opts.debugging_opts.explain
294+
&& !fcx.tcx.sess.parse_sess.span_diagnostic
295+
.code_emitted(&err.get_code().unwrap()) {
294296
err.note(
295-
"Thin pointers are \"simple\" pointers: they are purely a reference to a \
296-
memory address.\n\n\
297-
Fat pointers are pointers referencing \"Dynamically Sized Types\" (also \
298-
called DST). DST don't have a statically known size, therefore they can \
299-
only exist behind some kind of pointers that contain additional \
300-
information. Slices and trait objects are DSTs. In the case of slices, \
301-
the additional information the fat pointer holds is their size.");
297+
"Thin pointers are \"simple\" pointers: they are purely a reference to a
298+
memory address.
299+
300+
Fat pointers are pointers referencing \"Dynamically Sized Types\" (also
301+
called DST). DST don't have a statically known size, therefore they can
302+
only exist behind some kind of pointers that contain additional
303+
information. Slices and trait objects are DSTs. In the case of slices,
304+
the additional information the fat pointer holds is their size.");
302305
err.note("to fix this error, don't try to cast directly between thin and fat \
303306
pointers");
304-
err.help("for more information about casts, take a look at [The Book]\
307+
err.help("for more information about casts, take a look at
308+
[The Book]\
305309
(https://doc.rust-lang.org/book/first-edition/\
306310
casting-between-types.html)");
307311
}

0 commit comments

Comments
 (0)