diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2016-11-22 23:32:16 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2016-11-23 23:44:17 -0800 |
| commit | b7982bbbe0a002272b86ed2f7f7902b2c3471087 (patch) | |
| tree | e73b2abe3a40ed5c0d2e32e02e903812acdb02c2 /src/librustc_errors | |
| parent | eb53ca3aad616069cb8f6f8fff71c27e9ba9640c (diff) | |
| download | rust-b7982bbbe0a002272b86ed2f7f7902b2c3471087.tar.gz rust-b7982bbbe0a002272b86ed2f7f7902b2c3471087.zip | |
review comments
Diffstat (limited to 'src/librustc_errors')
| -rw-r--r-- | src/librustc_errors/emitter.rs | 82 | ||||
| -rw-r--r-- | src/librustc_errors/snippet.rs | 108 |
2 files changed, 84 insertions, 106 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 808a1683b84..808fe504b95 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -14,7 +14,7 @@ use syntax_pos::{COMMAND_LINE_SP, DUMMY_SP, FileMap, Span, MultiSpan, CharPos}; use {Level, CodeSuggestion, DiagnosticBuilder, SubDiagnostic, CodeMapper}; use RenderSpan::*; -use snippet::{Annotation, AnnotationType, Line, StyledString, Style}; +use snippet::{Annotation, AnnotationType, Line, MultilineAnnotation, StyledString, Style}; use styled_buffer::StyledBuffer; use std::io::prelude::*; @@ -181,12 +181,17 @@ impl EmitterWriter { if is_minimized { ann.annotation_type = AnnotationType::Minimized; } else if lo.line != hi.line { - ann.annotation_type = AnnotationType::Multiline { + let ml = MultilineAnnotation { depth: 1, line_start: lo.line, line_end: hi.line, + start_col: lo.col.0, + end_col: hi.col.0, + is_primary: span_label.is_primary, + label: span_label.label.clone(), }; - multiline_annotations.push((lo.file.clone(), ann.clone())); + ann.annotation_type = AnnotationType::Multiline(ml.clone()); + multiline_annotations.push((lo.file.clone(), ml)); }; if !ann.is_multiline() { @@ -200,65 +205,34 @@ impl EmitterWriter { // Find overlapping multiline annotations, put them at different depths multiline_annotations.sort_by(|a, b| { - if let AnnotationType::Multiline { - line_start: a_start, - line_end: a_end, - .. - } = a.1.annotation_type { - if let AnnotationType::Multiline { - line_start: b_start, - line_end: b_end, - .. - } = b.1.annotation_type { - (a_start, a_end).cmp(&(b_start, b_end)) - } else { - panic!("tried to sort multiline annotations, but found `{:?}`", b) - } - } else { - panic!("tried to sort multiline annotations, but found `{:?}`", a) - } + (a.1.line_start, a.1.line_end).cmp(&(b.1.line_start, b.1.line_end)) }); for item in multiline_annotations.clone() { let ann = item.1; - if let AnnotationType::Multiline {line_start, line_end, ..} = ann.annotation_type { - for item in multiline_annotations.iter_mut() { - let ref mut a = item.1; - if let AnnotationType::Multiline { - line_start: start, - line_end: end, - .. - } = a.annotation_type { - // Move all other multiline annotations overlapping with this one - // one level to the right. - if &ann != a && num_overlap(line_start, line_end, start, end, true) { - a.annotation_type.increase_depth(); - } else { - break; - } - } else { - panic!("tried to find depth for multiline annotation, but found `{:?}`", - ann) - }; + for item in multiline_annotations.iter_mut() { + let ref mut a = item.1; + // Move all other multiline annotations overlapping with this one + // one level to the right. + if &ann != a && + num_overlap(ann.line_start, ann.line_end, a.line_start, a.line_end, true) + { + a.increase_depth(); + } else { + break; } - } else { - panic!("tried to find depth for multiline annotation, but found `{:?}`", ann) - }; + } } let mut max_depth = 0; // max overlapping multiline spans for (file, ann) in multiline_annotations { - if let AnnotationType::Multiline {line_start, line_end, depth} = ann.annotation_type { - if depth > max_depth { - max_depth = depth; - } - add_annotation_to_file(&mut output, file.clone(), line_start, ann.as_start()); - for line in line_start + 1..line_end { - add_annotation_to_file(&mut output, file.clone(), line, ann.as_line()); - } - add_annotation_to_file(&mut output, file, line_end, ann.as_end()); - } else { - panic!("non-multiline annotation `{:?}` in `multiline_annotations`!", ann); + if ann.depth > max_depth { + max_depth = ann.depth; + } + add_annotation_to_file(&mut output, file.clone(), ann.line_start, ann.as_start()); + for line in ann.line_start + 1..ann.line_end { + add_annotation_to_file(&mut output, file.clone(), line, ann.as_line()); } + add_annotation_to_file(&mut output, file, ann.line_end, ann.as_end()); } for file_vec in output.iter_mut() { file_vec.multiline_depth = max_depth; @@ -572,7 +546,7 @@ impl EmitterWriter { // | | something about `foo` // | something about `fn foo()` annotations_position.sort_by(|a, b| { - fn len(a: Annotation) -> usize { + fn len(a: &Annotation) -> usize { // Account for usize underflows if a.end_col > a.start_col { a.end_col - a.start_col diff --git a/src/librustc_errors/snippet.rs b/src/librustc_errors/snippet.rs index 3bf428af994..b8c1726443d 100644 --- a/src/librustc_errors/snippet.rs +++ b/src/librustc_errors/snippet.rs @@ -41,6 +41,57 @@ pub struct Line { pub annotations: Vec<Annotation>, } + +#[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)] +pub struct MultilineAnnotation { + pub depth: usize, + pub line_start: usize, + pub line_end: usize, + pub start_col: usize, + pub end_col: usize, + pub is_primary: bool, + pub label: Option<String>, +} + +impl MultilineAnnotation { + pub fn increase_depth(&mut self) { + self.depth += 1; + } + + pub fn as_start(&self) -> Annotation { + Annotation { + start_col: self.start_col, + end_col: self.start_col + 1, + is_primary: self.is_primary, + label: Some("starting here...".to_owned()), + annotation_type: AnnotationType::MultilineStart(self.depth) + } + } + + pub fn as_end(&self) -> Annotation { + Annotation { + start_col: self.end_col - 1, + end_col: self.end_col, + is_primary: self.is_primary, + label: match self.label { + Some(ref label) => Some(format!("...ending here: {}", label)), + None => Some("...ending here".to_owned()), + }, + annotation_type: AnnotationType::MultilineEnd(self.depth) + } + } + + pub fn as_line(&self) -> Annotation { + Annotation { + start_col: 0, + end_col: 0, + is_primary: self.is_primary, + label: None, + annotation_type: AnnotationType::MultilineLine(self.depth) + } + } +} + #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)] pub enum AnnotationType { /// Annotation under a single line of code @@ -50,11 +101,7 @@ pub enum AnnotationType { Minimized, /// Annotation enclosing the first and last character of a multiline span - Multiline { - depth: usize, - line_start: usize, - line_end: usize, - }, + Multiline(MultilineAnnotation), // The Multiline type above is replaced with the following three in order // to reuse the current label drawing code. @@ -74,24 +121,6 @@ pub enum AnnotationType { MultilineLine(usize), } -impl AnnotationType { - pub fn depth(&self) -> usize { - match self { - &AnnotationType::Multiline {depth, ..} | - &AnnotationType::MultilineStart(depth) | - &AnnotationType::MultilineLine(depth) | - &AnnotationType::MultilineEnd(depth) => depth, - _ => 0, - } - } - - pub fn increase_depth(&mut self) { - if let AnnotationType::Multiline {ref mut depth, ..} = *self { - *depth += 1; - } - } -} - #[derive(Clone, Debug, PartialOrd, Ord, PartialEq, Eq)] pub struct Annotation { /// Start column, 0-based indexing -- counting *characters*, not @@ -124,39 +153,14 @@ impl Annotation { pub fn is_multiline(&self) -> bool { match self.annotation_type { - AnnotationType::Multiline {..} | - AnnotationType::MultilineStart(_) | - AnnotationType::MultilineLine(_) | - AnnotationType::MultilineEnd(_) => true, + AnnotationType::Multiline(_) | + AnnotationType::MultilineStart(_) | + AnnotationType::MultilineLine(_) | + AnnotationType::MultilineEnd(_) => true, _ => false, } } - pub fn as_start(&self) -> Annotation { - let mut a = self.clone(); - a.annotation_type = AnnotationType::MultilineStart(self.annotation_type.depth()); - a.end_col = a.start_col + 1; - a.label = Some("starting here...".to_owned()); - a - } - - pub fn as_end(&self) -> Annotation { - let mut a = self.clone(); - a.annotation_type = AnnotationType::MultilineEnd(self.annotation_type.depth()); - a.start_col = a.end_col - 1; - a.label = match a.label { - Some(l) => Some(format!("...ending here: {}", l)), - None => Some("..ending here".to_owned()), - }; - a - } - - pub fn as_line(&self) -> Annotation { - let mut a = self.clone(); - a.annotation_type = AnnotationType::MultilineLine(self.annotation_type.depth()); - a.label = None; - a - } } #[derive(Debug)] |
