From d06a4ded13b948a6a5b546514ccc7009097f145a Mon Sep 17 00:00:00 2001
From: Andy Russell <arussell123@gmail.com>
Date: Sun, 3 Nov 2019 12:04:01 -0500
Subject: [PATCH] use silent emitter for rustdoc highlighting pass

---
 src/librustc_errors/emitter.rs            |  8 +++
 src/librustc_interface/interface.rs       | 14 +----
 src/librustdoc/html/highlight.rs          |  4 +-
 src/libsyntax/sess.rs                     |  8 ++-
 src/test/rustdoc-ui/invalid-syntax.stderr | 64 -----------------------
 5 files changed, 19 insertions(+), 79 deletions(-)

diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs
index b153f0f0e82bf..5f74df13fae21 100644
--- a/src/librustc_errors/emitter.rs
+++ b/src/librustc_errors/emitter.rs
@@ -424,6 +424,14 @@ impl Emitter for EmitterWriter {
     }
 }
 
+/// An emitter that does nothing when emitting a diagnostic.
+pub struct SilentEmitter;
+
+impl Emitter for SilentEmitter {
+    fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> { None }
+    fn emit_diagnostic(&mut self, _: &Diagnostic) {}
+}
+
 /// maximum number of lines we will print for each error; arbitrary.
 pub const MAX_HIGHLIGHT_LINES: usize = 6;
 /// maximum number of suggestions to be shown
diff --git a/src/librustc_interface/interface.rs b/src/librustc_interface/interface.rs
index e014e4ed0fdc1..4e4d6d982fbc5 100644
--- a/src/librustc_interface/interface.rs
+++ b/src/librustc_interface/interface.rs
@@ -17,10 +17,9 @@ use std::sync::{Arc, Mutex};
 use syntax::{self, parse};
 use syntax::ast::{self, MetaItemKind};
 use syntax::parse::token;
-use syntax::source_map::{FileName, FilePathMapping, FileLoader, SourceMap};
+use syntax::source_map::{FileName, FileLoader, SourceMap};
 use syntax::sess::ParseSess;
 use syntax_pos::edition;
-use rustc_errors::{Diagnostic, emitter::Emitter, Handler, SourceMapperDyn};
 
 pub type Result<T> = result::Result<T, ErrorReported>;
 
@@ -63,18 +62,9 @@ impl Compiler {
 
 /// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
 pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
-    struct NullEmitter;
-    impl Emitter for NullEmitter {
-        fn emit_diagnostic(&mut self, _: &Diagnostic) {}
-        fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> { None }
-    }
-
     syntax::with_default_globals(move || {
         let cfg = cfgspecs.into_iter().map(|s| {
-
-            let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
-            let handler = Handler::with_emitter(false, None, Box::new(NullEmitter));
-            let sess = ParseSess::with_span_handler(handler, cm);
+            let sess = ParseSess::with_silent_emitter();
             let filename = FileName::cfg_spec_source_code(&s);
             let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string());
 
diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs
index 30c9453a643d4..88ba13f2796a0 100644
--- a/src/librustdoc/html/highlight.rs
+++ b/src/librustdoc/html/highlight.rs
@@ -11,7 +11,7 @@ use std::fmt::Display;
 use std::io;
 use std::io::prelude::*;
 
-use syntax::source_map::{SourceMap, FilePathMapping};
+use syntax::source_map::SourceMap;
 use syntax::parse::lexer;
 use syntax::parse::token::{self, Token};
 use syntax::sess::ParseSess;
@@ -33,7 +33,7 @@ pub fn render_with_highlighting(
                class, tooltip).unwrap();
     }
 
-    let sess = ParseSess::new(FilePathMapping::empty());
+    let sess = ParseSess::with_silent_emitter();
     let fm = sess.source_map().new_source_file(
         FileName::Custom(String::from("rustdoc-highlighting")),
         src.to_owned(),
diff --git a/src/libsyntax/sess.rs b/src/libsyntax/sess.rs
index 28a0868d5dd1e..2251c3961db2e 100644
--- a/src/libsyntax/sess.rs
+++ b/src/libsyntax/sess.rs
@@ -6,7 +6,7 @@ use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
 use crate::source_map::{SourceMap, FilePathMapping};
 use crate::feature_gate::UnstableFeatures;
 
-use errors::{Applicability, Handler, ColorConfig, DiagnosticBuilder};
+use errors::{Applicability, emitter::SilentEmitter, Handler, ColorConfig, DiagnosticBuilder};
 use rustc_data_structures::fx::{FxHashSet, FxHashMap};
 use rustc_data_structures::sync::{Lrc, Lock, Once};
 use syntax_pos::{Symbol, Span, MultiSpan};
@@ -104,6 +104,12 @@ impl ParseSess {
         }
     }
 
+    pub fn with_silent_emitter() -> Self {
+        let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
+        let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter));
+        ParseSess::with_span_handler(handler, cm)
+    }
+
     #[inline]
     pub fn source_map(&self) -> &SourceMap {
         &self.source_map
diff --git a/src/test/rustdoc-ui/invalid-syntax.stderr b/src/test/rustdoc-ui/invalid-syntax.stderr
index 8ec4338e13f96..84c10028fd1f9 100644
--- a/src/test/rustdoc-ui/invalid-syntax.stderr
+++ b/src/test/rustdoc-ui/invalid-syntax.stderr
@@ -222,67 +222,3 @@ warning: could not parse code block as Rust code
 LL | ///     \____/
    |         ^^^^^^
 
-error: unknown start of token: \
- --> <rustdoc-highlighting>:1:1
-  |
-1 | \____/
-  | ^
-
-error: unknown start of token: \
- --> <rustdoc-highlighting>:1:1
-  |
-1 | \_
-  | ^
-
-error: unknown start of token: \
- --> <rustdoc-highlighting>:1:1
-  |
-1 | \_
-  | ^
-
-error: unknown start of token: `
- --> <rustdoc-highlighting>:1:1
-  |
-1 | ```
-  | ^
-  |
-help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
-  |
-1 | '``
-  | ^
-
-error: unknown start of token: \
- --> <rustdoc-highlighting>:2:1
-  |
-2 | \_
-  | ^
-
-error: unknown start of token: \
- --> <rustdoc-highlighting>:1:1
-  |
-1 | \_
-  | ^
-
-error: unknown start of token: \
- --> <rustdoc-highlighting>:1:1
-  |
-1 | \_
-  | ^
-
-error: unknown start of token: `
- --> <rustdoc-highlighting>:3:30
-  |
-3 |    |     ^^^^^^ did you mean `baz::foobar`?
-  |                              ^
-  |
-help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
-  |
-3 |    |     ^^^^^^ did you mean 'baz::foobar`?
-  |                              ^
-
-error: unknown start of token: \
- --> <rustdoc-highlighting>:1:1
-  |
-1 | \__________pkt->size___________/          \_result->size_/ \__pkt->size__/
-  | ^
-