Skip to content

Commit 8612838

Browse files
author
Jonathan Turner
committed
Add back in old school mode
1 parent 71ec286 commit 8612838

File tree

1 file changed

+265
-26
lines changed

1 file changed

+265
-26
lines changed

src/librustc_errors/emitter.rs

+265-26
Original file line numberDiff line numberDiff line change
@@ -34,31 +34,14 @@ pub trait Emitter {
3434

3535
impl Emitter for EmitterWriter {
3636
fn emit(&mut self, db: &DiagnosticBuilder) {
37-
if check_old_skool() {
38-
self.emit_message(&FullSpan(db.span.clone()),
39-
&db.message,
40-
db.code.as_ref().map(|s| &**s),
41-
db.level,
42-
true,
43-
true);
44-
let db_span = FullSpan(db.span.clone());
45-
46-
for child in &db.children {
47-
let render_span = child.render_span
48-
.clone()
49-
.unwrap_or_else(
50-
|| FullSpan(child.span.clone()));
51-
let (render_span, show_snippet) = match render_span.span().primary_span() {
52-
None => (db_span.clone(), false),
53-
_ => (render_span, true)
54-
};
55-
self.emit_message(&render_span,
56-
&child.message,
57-
None,
58-
child.level,
59-
false,
60-
show_snippet);
61-
}
37+
let old_school = match self.format_mode {
38+
FormatMode::NewErrorFormat => false,
39+
FormatMode::OriginalErrorFormat => true,
40+
FormatMode::EnvironmentSelected => check_old_skool()
41+
};
42+
43+
if old_school {
44+
self.emit_messages_old_school(db);
6245
} else {
6346
self.emit_messages_default(db);
6447
}
@@ -718,6 +701,239 @@ impl EmitterWriter {
718701
}
719702
write!(&mut self.dst, "\n");
720703
}
704+
fn emit_message_old_school(&mut self,
705+
msp: &MultiSpan,
706+
msg: &str,
707+
code: &Option<String>,
708+
level: &Level,
709+
show_snippet: bool)
710+
-> io::Result<()> {
711+
let mut buffer = StyledBuffer::new();
712+
713+
let loc = match msp.primary_span() {
714+
Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => "".to_string(),
715+
Some(ps) => if let Some(ref cm) = self.cm {
716+
cm.span_to_string(ps)
717+
} else {
718+
"".to_string()
719+
},
720+
None => {
721+
"".to_string()
722+
}
723+
};
724+
if loc != "" {
725+
buffer.append(0, &loc, Style::NoStyle);
726+
buffer.append(0, " ", Style::NoStyle);
727+
}
728+
buffer.append(0, &level.to_string(), Style::Level(level.clone()));
729+
buffer.append(0, ": ", Style::HeaderMsg);
730+
buffer.append(0, msg, Style::HeaderMsg);
731+
buffer.append(0, " ", Style::NoStyle);
732+
match code {
733+
&Some(ref code) => {
734+
buffer.append(0, "[", Style::ErrorCode);
735+
buffer.append(0, &code, Style::ErrorCode);
736+
buffer.append(0, "]", Style::ErrorCode);
737+
}
738+
_ => {}
739+
}
740+
741+
if !show_snippet {
742+
emit_to_destination(&buffer.render(), level, &mut self.dst);
743+
return Ok(());
744+
}
745+
746+
// Watch out for various nasty special spans; don't try to
747+
// print any filename or anything for those.
748+
match msp.primary_span() {
749+
Some(COMMAND_LINE_SP) | Some(DUMMY_SP) => {
750+
emit_to_destination(&buffer.render(), level, &mut self.dst);
751+
return Ok(());
752+
}
753+
_ => { }
754+
}
755+
756+
let mut annotated_files = self.preprocess_annotations(msp);
757+
758+
if let (Some(ref cm), Some(ann_file), Some(ref primary_span)) =
759+
(self.cm.as_ref(), annotated_files.first(), msp.primary_span().as_ref()) {
760+
761+
// Next, print the source line and its squiggle
762+
// for old school mode, we will render them to the buffer, then insert the file loc
763+
// (or space the same amount) in front of the line and the squiggle
764+
let source_string = ann_file.file.get_line(ann_file.lines[0].line_index - 1)
765+
.unwrap_or("");
766+
767+
let line_offset = buffer.num_lines();
768+
769+
let lo = cm.lookup_char_pos(primary_span.lo);
770+
//Before each secondary line in old skool-mode, print the label
771+
//as an old-style note
772+
let file_pos = format!("{}:{} ", lo.file.name.clone(), lo.line);
773+
let file_pos_len = file_pos.len();
774+
775+
// First create the source line we will highlight.
776+
buffer.puts(line_offset, 0, &file_pos, Style::FileNameStyle);
777+
buffer.puts(line_offset, file_pos_len, &source_string, Style::Quotation);
778+
// Sort the annotations by (start, end col)
779+
let mut annotations = ann_file.lines[0].annotations.clone();
780+
781+
// Next, create the highlight line.
782+
for annotation in &annotations {
783+
for p in annotation.start_col..annotation.end_col {
784+
if p == annotation.start_col {
785+
buffer.putc(line_offset + 1,
786+
file_pos_len + p,
787+
'^',
788+
if annotation.is_primary {
789+
Style::UnderlinePrimary
790+
} else {
791+
Style::OldSchoolNote
792+
});
793+
} else {
794+
buffer.putc(line_offset + 1,
795+
file_pos_len + p,
796+
'~',
797+
if annotation.is_primary {
798+
Style::UnderlinePrimary
799+
} else {
800+
Style::OldSchoolNote
801+
});
802+
}
803+
}
804+
}
805+
}
806+
if let Some(ref primary_span) = msp.primary_span().as_ref() {
807+
self.render_macro_backtrace_old_school(primary_span, &mut buffer)?;
808+
}
809+
810+
match code {
811+
&Some(ref code) if self.registry.as_ref()
812+
.and_then(|registry| registry.find_description(code))
813+
.is_some() => {
814+
let msg = "run `rustc --explain ".to_string() + &code.to_string() +
815+
"` to see a detailed explanation";
816+
817+
let line_offset = buffer.num_lines();
818+
buffer.append(line_offset, &loc, Style::NoStyle);
819+
buffer.append(line_offset, " ", Style::NoStyle);
820+
buffer.append(line_offset, &Level::Help.to_string(), Style::Level(Level::Help));
821+
buffer.append(line_offset, ": ", Style::HeaderMsg);
822+
buffer.append(line_offset, &msg, Style::HeaderMsg);
823+
}
824+
_ => ()
825+
}
826+
827+
// final step: take our styled buffer, render it, then output it
828+
emit_to_destination(&buffer.render(), level, &mut self.dst);
829+
Ok(())
830+
}
831+
fn emit_suggestion_old_school(&mut self,
832+
suggestion: &CodeSuggestion,
833+
level: &Level,
834+
msg: &str)
835+
-> io::Result<()> {
836+
use std::borrow::Borrow;
837+
838+
let primary_span = suggestion.msp.primary_span().unwrap();
839+
if let Some(ref cm) = self.cm {
840+
let mut buffer = StyledBuffer::new();
841+
842+
let loc = cm.span_to_string(primary_span);
843+
844+
if loc != "" {
845+
buffer.append(0, &loc, Style::NoStyle);
846+
buffer.append(0, " ", Style::NoStyle);
847+
}
848+
849+
buffer.append(0, &level.to_string(), Style::Level(level.clone()));
850+
buffer.append(0, ": ", Style::HeaderMsg);
851+
buffer.append(0, msg, Style::HeaderMsg);
852+
853+
let lines = cm.span_to_lines(primary_span).unwrap();
854+
855+
assert!(!lines.lines.is_empty());
856+
857+
let complete = suggestion.splice_lines(cm.borrow());
858+
let line_count = cmp::min(lines.lines.len(), MAX_HIGHLIGHT_LINES);
859+
let display_lines = &lines.lines[..line_count];
860+
861+
let fm = &*lines.file;
862+
// Calculate the widest number to format evenly
863+
let max_digits = line_num_max_digits(display_lines.last().unwrap());
864+
865+
// print the suggestion without any line numbers, but leave
866+
// space for them. This helps with lining up with previous
867+
// snippets from the actual error being reported.
868+
let mut lines = complete.lines();
869+
let mut row_num = 1;
870+
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
871+
buffer.append(row_num, &fm.name, Style::FileNameStyle);
872+
for i in 0..max_digits+2 {
873+
buffer.append(row_num, &" ", Style::NoStyle);
874+
}
875+
buffer.append(row_num, line, Style::NoStyle);
876+
row_num += 1;
877+
}
878+
879+
// if we elided some lines, add an ellipsis
880+
if let Some(_) = lines.next() {
881+
buffer.append(row_num, "...", Style::NoStyle);
882+
}
883+
emit_to_destination(&buffer.render(), level, &mut self.dst);
884+
}
885+
Ok(())
886+
}
887+
888+
fn emit_messages_old_school(&mut self, db: &DiagnosticBuilder) {
889+
match self.emit_message_old_school(&db.span,
890+
&db.message,
891+
&db.code,
892+
&db.level,
893+
true) {
894+
Ok(()) => {
895+
for child in &db.children {
896+
let (span, show_snippet) = if child.span.primary_spans().is_empty() {
897+
(db.span.clone(), false)
898+
} else {
899+
(child.span.clone(), true)
900+
};
901+
902+
match child.render_span {
903+
Some(FullSpan(ref msp)) => {
904+
match self.emit_message_old_school(&span,
905+
&child.message,
906+
&None,
907+
&child.level,
908+
show_snippet) {
909+
Err(e) => panic!("failed to emit error: {}", e),
910+
_ => ()
911+
}
912+
},
913+
Some(Suggestion(ref cs)) => {
914+
match self.emit_suggestion_old_school(cs,
915+
&child.level,
916+
&child.message) {
917+
Err(e) => panic!("failed to emit error: {}", e),
918+
_ => ()
919+
}
920+
},
921+
None => {
922+
match self.emit_message_old_school(&span,
923+
&child.message,
924+
&None,
925+
&child.level,
926+
show_snippet) {
927+
Err(e) => panic!("failed to emit error: {}", e),
928+
_ => ()
929+
}
930+
}
931+
}
932+
}
933+
}
934+
Err(e) => panic!("failed to emit error: {}", e)
935+
}
936+
}
721937

722938
fn emit_message_(&mut self,
723939
rsp: &RenderSpan,
@@ -948,6 +1164,29 @@ impl EmitterWriter {
9481164
Ok(())
9491165
}
9501166

1167+
fn render_macro_backtrace_old_school(&mut self,
1168+
sp: &Span,
1169+
buffer: &mut StyledBuffer) -> io::Result<()> {
1170+
if let Some(ref cm) = self.cm {
1171+
for trace in cm.macro_backtrace(sp.clone()) {
1172+
let line_offset = buffer.num_lines();
1173+
1174+
let mut diag_string =
1175+
format!("in this expansion of {}", trace.macro_decl_name);
1176+
if let Some(def_site_span) = trace.def_site_span {
1177+
diag_string.push_str(
1178+
&format!(" (defined in {})",
1179+
cm.span_to_filename(def_site_span)));
1180+
}
1181+
let snippet = cm.span_to_string(trace.call_site);
1182+
buffer.append(line_offset, &format!("{} ", snippet), Style::NoStyle);
1183+
buffer.append(line_offset, "Note", Style::Level(Level::Note));
1184+
buffer.append(line_offset, ": ", Style::NoStyle);
1185+
buffer.append(line_offset, &diag_string, Style::OldSchoolNoteText);
1186+
}
1187+
}
1188+
Ok(())
1189+
}
9511190
fn print_macro_backtrace(&mut self,
9521191
sp: Span)
9531192
-> io::Result<()> {
@@ -1087,7 +1326,7 @@ impl Destination {
10871326
}
10881327
Style::ErrorCode => {
10891328
try!(self.start_attr(term::Attr::Bold));
1090-
//try!(self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_MAGENTA)));
1329+
try!(self.start_attr(term::Attr::ForegroundColor(term::color::BRIGHT_MAGENTA)));
10911330
}
10921331
Style::Quotation => {}
10931332
Style::OldSchoolNote => {

0 commit comments

Comments
 (0)