Skip to content

Commit 580dbb5

Browse files
authored
Rollup merge of rust-lang#66068 - euclio:null-emitter, r=estebank
use silent emitter for rustdoc highlighting pass Partially addresses rust-lang#63284.
2 parents de02742 + d06a4de commit 580dbb5

File tree

5 files changed

+19
-79
lines changed

5 files changed

+19
-79
lines changed

src/librustc_errors/emitter.rs

+8
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,14 @@ impl Emitter for EmitterWriter {
424424
}
425425
}
426426

427+
/// An emitter that does nothing when emitting a diagnostic.
428+
pub struct SilentEmitter;
429+
430+
impl Emitter for SilentEmitter {
431+
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> { None }
432+
fn emit_diagnostic(&mut self, _: &Diagnostic) {}
433+
}
434+
427435
/// maximum number of lines we will print for each error; arbitrary.
428436
pub const MAX_HIGHLIGHT_LINES: usize = 6;
429437
/// maximum number of suggestions to be shown

src/librustc_interface/interface.rs

+2-12
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@ use std::sync::{Arc, Mutex};
1717
use syntax::{self, parse};
1818
use syntax::ast::{self, MetaItemKind};
1919
use syntax::parse::token;
20-
use syntax::source_map::{FileName, FilePathMapping, FileLoader, SourceMap};
20+
use syntax::source_map::{FileName, FileLoader, SourceMap};
2121
use syntax::sess::ParseSess;
2222
use syntax_pos::edition;
23-
use rustc_errors::{Diagnostic, emitter::Emitter, Handler, SourceMapperDyn};
2423

2524
pub type Result<T> = result::Result<T, ErrorReported>;
2625

@@ -63,18 +62,9 @@ impl Compiler {
6362

6463
/// Converts strings provided as `--cfg [cfgspec]` into a `crate_cfg`.
6564
pub fn parse_cfgspecs(cfgspecs: Vec<String>) -> FxHashSet<(String, Option<String>)> {
66-
struct NullEmitter;
67-
impl Emitter for NullEmitter {
68-
fn emit_diagnostic(&mut self, _: &Diagnostic) {}
69-
fn source_map(&self) -> Option<&Lrc<SourceMapperDyn>> { None }
70-
}
71-
7265
syntax::with_default_globals(move || {
7366
let cfg = cfgspecs.into_iter().map(|s| {
74-
75-
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
76-
let handler = Handler::with_emitter(false, None, Box::new(NullEmitter));
77-
let sess = ParseSess::with_span_handler(handler, cm);
67+
let sess = ParseSess::with_silent_emitter();
7868
let filename = FileName::cfg_spec_source_code(&s);
7969
let mut parser = parse::new_parser_from_source_str(&sess, filename, s.to_string());
8070

src/librustdoc/html/highlight.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use std::fmt::Display;
1111
use std::io;
1212
use std::io::prelude::*;
1313

14-
use syntax::source_map::{SourceMap, FilePathMapping};
14+
use syntax::source_map::SourceMap;
1515
use syntax::parse::lexer;
1616
use syntax::parse::token::{self, Token};
1717
use syntax::sess::ParseSess;
@@ -33,7 +33,7 @@ pub fn render_with_highlighting(
3333
class, tooltip).unwrap();
3434
}
3535

36-
let sess = ParseSess::new(FilePathMapping::empty());
36+
let sess = ParseSess::with_silent_emitter();
3737
let fm = sess.source_map().new_source_file(
3838
FileName::Custom(String::from("rustdoc-highlighting")),
3939
src.to_owned(),

src/libsyntax/sess.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::early_buffered_lints::{BufferedEarlyLint, BufferedEarlyLintId};
66
use crate::source_map::{SourceMap, FilePathMapping};
77
use crate::feature_gate::UnstableFeatures;
88

9-
use errors::{Applicability, Handler, ColorConfig, DiagnosticBuilder};
9+
use errors::{Applicability, emitter::SilentEmitter, Handler, ColorConfig, DiagnosticBuilder};
1010
use rustc_data_structures::fx::{FxHashSet, FxHashMap};
1111
use rustc_data_structures::sync::{Lrc, Lock, Once};
1212
use syntax_pos::{Symbol, Span, MultiSpan};
@@ -107,6 +107,12 @@ impl ParseSess {
107107
}
108108
}
109109

110+
pub fn with_silent_emitter() -> Self {
111+
let cm = Lrc::new(SourceMap::new(FilePathMapping::empty()));
112+
let handler = Handler::with_emitter(false, None, Box::new(SilentEmitter));
113+
ParseSess::with_span_handler(handler, cm)
114+
}
115+
110116
#[inline]
111117
pub fn source_map(&self) -> &SourceMap {
112118
&self.source_map

src/test/rustdoc-ui/invalid-syntax.stderr

-64
Original file line numberDiff line numberDiff line change
@@ -222,67 +222,3 @@ warning: could not parse code block as Rust code
222222
LL | /// \____/
223223
| ^^^^^^
224224

225-
error: unknown start of token: \
226-
--> <rustdoc-highlighting>:1:1
227-
|
228-
1 | \____/
229-
| ^
230-
231-
error: unknown start of token: \
232-
--> <rustdoc-highlighting>:1:1
233-
|
234-
1 | \_
235-
| ^
236-
237-
error: unknown start of token: \
238-
--> <rustdoc-highlighting>:1:1
239-
|
240-
1 | \_
241-
| ^
242-
243-
error: unknown start of token: `
244-
--> <rustdoc-highlighting>:1:1
245-
|
246-
1 | ```
247-
| ^
248-
|
249-
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
250-
|
251-
1 | '``
252-
| ^
253-
254-
error: unknown start of token: \
255-
--> <rustdoc-highlighting>:2:1
256-
|
257-
2 | \_
258-
| ^
259-
260-
error: unknown start of token: \
261-
--> <rustdoc-highlighting>:1:1
262-
|
263-
1 | \_
264-
| ^
265-
266-
error: unknown start of token: \
267-
--> <rustdoc-highlighting>:1:1
268-
|
269-
1 | \_
270-
| ^
271-
272-
error: unknown start of token: `
273-
--> <rustdoc-highlighting>:3:30
274-
|
275-
3 | | ^^^^^^ did you mean `baz::foobar`?
276-
| ^
277-
|
278-
help: Unicode character '`' (Grave Accent) looks like ''' (Single Quote), but it is not
279-
|
280-
3 | | ^^^^^^ did you mean 'baz::foobar`?
281-
| ^
282-
283-
error: unknown start of token: \
284-
--> <rustdoc-highlighting>:1:1
285-
|
286-
1 | \__________pkt->size___________/ \_result->size_/ \__pkt->size__/
287-
| ^
288-

0 commit comments

Comments
 (0)