Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use silent emitter for rustdoc highlighting pass #66068

Merged
merged 1 commit into from
Nov 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 2 additions & 12 deletions src/librustc_interface/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down Expand Up @@ -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());

Expand Down
4 changes: 2 additions & 2 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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(),
Expand Down
8 changes: 7 additions & 1 deletion src/libsyntax/sess.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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
Expand Down
64 changes: 0 additions & 64 deletions src/test/rustdoc-ui/invalid-syntax.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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__/
| ^