Skip to content

Commit 5cf300d

Browse files
Remove warnings/errors from compiler when using typeck_body in rustdoc span map builder
1 parent 0799528 commit 5cf300d

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

compiler/rustc_errors/src/lib.rs

+16-1
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,9 @@ struct HandlerInner {
342342
deduplicated_warn_count: usize,
343343

344344
future_breakage_diagnostics: Vec<Diagnostic>,
345+
346+
/// If set to `true`, no warning or error will be emitted.
347+
quiet: bool,
345348
}
346349

347350
/// A key denoting where from a diagnostic was stashed.
@@ -456,10 +459,19 @@ impl Handler {
456459
emitted_diagnostics: Default::default(),
457460
stashed_diagnostics: Default::default(),
458461
future_breakage_diagnostics: Vec::new(),
462+
quiet: false,
459463
}),
460464
}
461465
}
462466

467+
pub fn with_disabled_diagnostic<T, F: FnOnce() -> T>(&self, f: F) -> T {
468+
let prev = self.inner.borrow_mut().quiet;
469+
self.inner.borrow_mut().quiet = true;
470+
let ret = f();
471+
self.inner.borrow_mut().quiet = prev;
472+
ret
473+
}
474+
463475
// This is here to not allow mutation of flags;
464476
// as of this writing it's only used in tests in librustc_middle.
465477
pub fn can_emit_warnings(&self) -> bool {
@@ -818,7 +830,7 @@ impl HandlerInner {
818830
}
819831

820832
fn emit_diagnostic(&mut self, diagnostic: &Diagnostic) {
821-
if diagnostic.cancelled() {
833+
if diagnostic.cancelled() || self.quiet {
822834
return;
823835
}
824836

@@ -1035,6 +1047,9 @@ impl HandlerInner {
10351047
}
10361048

10371049
fn delay_as_bug(&mut self, diagnostic: Diagnostic) {
1050+
if self.quiet {
1051+
return;
1052+
}
10381053
if self.flags.report_delayed_bugs {
10391054
self.emit_diagnostic(&diagnostic);
10401055
}

compiler/rustc_session/src/session.rs

+4
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,10 @@ impl Session {
500500
&self.parse_sess.span_diagnostic
501501
}
502502

503+
pub fn with_disabled_diagnostic<T, F: FnOnce() -> T>(&self, f: F) -> T {
504+
self.parse_sess.span_diagnostic.with_disabled_diagnostic(f)
505+
}
506+
503507
/// Analogous to calling methods on the given `DiagnosticBuilder`, but
504508
/// deduplicates on lint ID, span (if any), and message for this `Session`
505509
fn diag_once<'a, 'b>(

src/librustdoc/html/render/span_map.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -181,11 +181,11 @@ impl Visitor<'tcx> for SpanMapVisitor<'tcx> {
181181
if let Some(hir_id) = segment.hir_id {
182182
let hir = self.tcx.hir();
183183
let body_id = hir.enclosing_body_owner(hir_id);
184-
// FIXME: this is showing error messages for parts of the code that are not
185-
// compiled (because of cfg)!
186-
let typeck_results = self.tcx.typeck_body(
187-
hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"),
188-
);
184+
let typeck_results = self.tcx.sess.with_disabled_diagnostic(|| {
185+
self.tcx.typeck_body(
186+
hir.maybe_body_owned_by(body_id).expect("a body which isn't a body"),
187+
)
188+
});
189189
if let Some(def_id) = typeck_results.type_dependent_def_id(expr.hir_id) {
190190
self.matches.insert(
191191
LightSpan::new_from_span(method_span),

0 commit comments

Comments
 (0)