diff options
| author | Jonathan Turner <jturner@mozilla.com> | 2016-06-23 15:19:40 -0400 |
|---|---|---|
| committer | Jonathan Turner <jturner@mozilla.com> | 2016-06-23 15:19:40 -0400 |
| commit | 80f1c7875278b4dde63d5a523fe773e61d0f754e (patch) | |
| tree | 27e086b40da848f4cb6371ead7b14b48d115338d /src/librustc_errors | |
| parent | d4e79dec92e2f478add8423decf728cdae147860 (diff) | |
| download | rust-80f1c7875278b4dde63d5a523fe773e61d0f754e.tar.gz rust-80f1c7875278b4dde63d5a523fe773e61d0f754e.zip | |
make old school mode a bit more configurable
Diffstat (limited to 'src/librustc_errors')
| -rw-r--r-- | src/librustc_errors/emitter.rs | 46 | ||||
| -rw-r--r-- | src/librustc_errors/lib.rs | 3 | ||||
| -rw-r--r-- | src/librustc_errors/snippet.rs | 43 |
3 files changed, 68 insertions, 24 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index d46a8f3c453..a7c68e3a87b 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -17,7 +17,7 @@ use check_old_skool; use {Level, RenderSpan, CodeSuggestion, DiagnosticBuilder, CodeMapper}; use RenderSpan::*; use Level::*; -use snippet::{RenderedLineKind, SnippetData, Style}; +use snippet::{RenderedLineKind, SnippetData, Style, FormatMode}; use std::{cmp, fmt}; use std::io::prelude::*; @@ -159,7 +159,7 @@ pub struct EmitterWriter { first: bool, // For now, allow an old-school mode while we transition - old_school: bool, + format_mode: FormatMode } impl CoreEmitter for EmitterWriter { @@ -194,35 +194,35 @@ macro_rules! println_maybe_styled { impl EmitterWriter { pub fn stderr(color_config: ColorConfig, registry: Option<registry::Registry>, - code_map: Rc<CodeMapper>) + code_map: Rc<CodeMapper>, + format_mode: FormatMode) -> EmitterWriter { - let old_school = check_old_skool(); if color_config.use_color() { let dst = Destination::from_stderr(); EmitterWriter { dst: dst, registry: registry, cm: code_map, first: true, - old_school: old_school } + format_mode: format_mode.clone() } } else { EmitterWriter { dst: Raw(Box::new(io::stderr())), registry: registry, cm: code_map, first: true, - old_school: old_school } + format_mode: format_mode.clone() } } } pub fn new(dst: Box<Write + Send>, registry: Option<registry::Registry>, - code_map: Rc<CodeMapper>) + code_map: Rc<CodeMapper>, + format_mode: FormatMode) -> EmitterWriter { - let old_school = check_old_skool(); EmitterWriter { dst: Raw(dst), registry: registry, cm: code_map, first: true, - old_school: old_school } + format_mode: format_mode.clone() } } fn emit_message_(&mut self, @@ -233,11 +233,17 @@ impl EmitterWriter { is_header: bool, show_snippet: bool) -> io::Result<()> { + let old_school = match self.format_mode { + FormatMode::NewErrorFormat => false, + FormatMode::OriginalErrorFormat => true, + FormatMode::EnvironmentSelected => check_old_skool() + }; + if is_header { if self.first { self.first = false; } else { - if !self.old_school { + if !old_school { write!(self.dst, "\n")?; } } @@ -248,7 +254,7 @@ impl EmitterWriter { .and_then(|registry| registry.find_description(code)) .is_some() => { let code_with_explain = String::from("--explain ") + code; - if self.old_school { + if old_school { let loc = match rsp.span().primary_span() { Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(), Some(ps) => self.cm.span_to_string(ps), @@ -261,7 +267,7 @@ impl EmitterWriter { } } _ => { - if self.old_school { + if old_school { let loc = match rsp.span().primary_span() { Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(), Some(ps) => self.cm.span_to_string(ps), @@ -303,7 +309,7 @@ impl EmitterWriter { } } } - if self.old_school { + if old_school { match code { Some(code) if self.registry.as_ref() .and_then(|registry| registry.find_description(code)) @@ -363,14 +369,22 @@ impl EmitterWriter { lvl: Level) -> io::Result<()> { + let old_school = match self.format_mode { + FormatMode::NewErrorFormat => false, + FormatMode::OriginalErrorFormat => true, + FormatMode::EnvironmentSelected => check_old_skool() + }; + let mut snippet_data = SnippetData::new(self.cm.clone(), - msp.primary_span()); - if self.old_school { + msp.primary_span(), + self.format_mode.clone()); + if old_school { let mut output_vec = vec![]; for span_label in msp.span_labels() { let mut snippet_data = SnippetData::new(self.cm.clone(), - Some(span_label.span)); + Some(span_label.span), + self.format_mode.clone()); snippet_data.push(span_label.span, span_label.is_primary, diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 47047769d31..18fc826f9aa 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -425,7 +425,8 @@ impl Handler { treat_err_as_bug: bool, cm: Rc<CodeMapper>) -> Handler { - let emitter = Box::new(EmitterWriter::stderr(color_config, registry, cm)); + let emitter = Box::new(EmitterWriter::stderr(color_config, registry, cm, + snippet::FormatMode::EnvironmentSelected)); Handler::with_emitter(can_emit_warnings, treat_err_as_bug, emitter) } diff --git a/src/librustc_errors/snippet.rs b/src/librustc_errors/snippet.rs index c472994599c..33f40ffc71a 100644 --- a/src/librustc_errors/snippet.rs +++ b/src/librustc_errors/snippet.rs @@ -18,9 +18,17 @@ use std::rc::Rc; use std::mem; #[derive(Clone)] +pub enum FormatMode { + NewErrorFormat, + OriginalErrorFormat, + EnvironmentSelected +} + +#[derive(Clone)] pub struct SnippetData { codemap: Rc<CodeMapper>, files: Vec<FileInfo>, + format_mode: FormatMode, } #[derive(Clone)] @@ -35,6 +43,10 @@ pub struct FileInfo { primary_span: Option<Span>, lines: Vec<Line>, + + /// The type of error format to render. We keep it here so that + /// it's easy to configure for both tests and regular usage + format_mode: FormatMode, } #[derive(Clone, Debug)] @@ -111,7 +123,8 @@ pub enum RenderedLineKind { impl SnippetData { pub fn new(codemap: Rc<CodeMapper>, - primary_span: Option<Span>) // (*) + primary_span: Option<Span>, + format_mode: FormatMode) // (*) -> Self { // (*) The primary span indicates the file that must appear // first, and which will have a line number etc in its @@ -125,7 +138,8 @@ impl SnippetData { let mut data = SnippetData { codemap: codemap.clone(), - files: vec![] + files: vec![], + format_mode: format_mode.clone() }; if let Some(primary_span) = primary_span { let lo = codemap.lookup_char_pos(primary_span.lo); @@ -134,6 +148,7 @@ impl SnippetData { file: lo.file, primary_span: Some(primary_span), lines: vec![], + format_mode: format_mode.clone(), }); } data @@ -166,6 +181,7 @@ impl SnippetData { file: file_map.clone(), lines: vec![], primary_span: None, + format_mode: self.format_mode.clone() }); self.files.last_mut().unwrap() } @@ -177,7 +193,7 @@ impl SnippetData { self.files.iter() .flat_map(|f| f.render_file_lines(&self.codemap)) .collect(); - prepend_prefixes(&mut rendered_lines); + prepend_prefixes(&mut rendered_lines, &self.format_mode); trim_lines(&mut rendered_lines); rendered_lines } @@ -454,7 +470,11 @@ impl FileInfo { } fn render_file_lines(&self, codemap: &Rc<CodeMapper>) -> Vec<RenderedLine> { - let old_school = check_old_skool(); + let old_school = match self.format_mode { + FormatMode::OriginalErrorFormat => true, + FormatMode::NewErrorFormat => false, + FormatMode::EnvironmentSelected => check_old_skool() + }; // As a first step, we elide any instance of more than one // continuous unannotated line. @@ -590,7 +610,12 @@ impl FileInfo { } fn render_line(&self, line: &Line) -> Vec<RenderedLine> { - let old_school = check_old_skool(); + let old_school = match self.format_mode { + FormatMode::OriginalErrorFormat => true, + FormatMode::NewErrorFormat => false, + FormatMode::EnvironmentSelected => check_old_skool() + }; + let source_string = self.file.get_line(line.line_index) .unwrap_or(""); let source_kind = RenderedLineKind::SourceText { @@ -775,8 +800,12 @@ impl FileInfo { } } -fn prepend_prefixes(rendered_lines: &mut [RenderedLine]) { - let old_school = check_old_skool(); +fn prepend_prefixes(rendered_lines: &mut [RenderedLine], format_mode: &FormatMode) { + let old_school = match *format_mode { + FormatMode::OriginalErrorFormat => true, + FormatMode::NewErrorFormat => false, + FormatMode::EnvironmentSelected => check_old_skool() + }; if old_school { return; } |
