Skip to content

Commit 9b597a1

Browse files
Add rustc --explain back
1 parent 026339e commit 9b597a1

File tree

1 file changed

+43
-7
lines changed

1 file changed

+43
-7
lines changed

src/librustc_errors/emitter.rs

+43-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ use std::io::prelude::*;
2121
use std::io;
2222
use std::rc::Rc;
2323
use term;
24-
use std::collections::HashMap;
24+
use std::collections::{HashMap, HashSet};
2525
use std::cmp::min;
2626
use unicode_width;
2727

@@ -107,6 +107,7 @@ pub struct EmitterWriter {
107107
cm: Option<Rc<CodeMapper>>,
108108
short_message: bool,
109109
teach: bool,
110+
error_codes: HashSet<String>,
110111
}
111112

112113
struct FileWithAnnotatedLines {
@@ -115,6 +116,30 @@ struct FileWithAnnotatedLines {
115116
multiline_depth: usize,
116117
}
117118

119+
impl Drop for EmitterWriter {
120+
fn drop(&mut self) {
121+
if !self.short_message && !self.error_codes.is_empty() {
122+
let mut error_codes = self.error_codes.clone().into_iter().collect::<Vec<_>>();
123+
error_codes.sort();
124+
if error_codes.len() > 1 {
125+
writeln!(self.dst,
126+
"You've got a few errors: {}",
127+
error_codes.join(", ")).expect("failed to give tips...");
128+
writeln!(self.dst,
129+
"If you want more information on an error, try using \
130+
\"rustc --explain {}\"",
131+
&error_codes[0]).expect("failed to give tips...");
132+
} else {
133+
writeln!(self.dst,
134+
"If you want more information on this error, try using \
135+
\"rustc --explain {}\"",
136+
&error_codes[0]).expect("failed to give tips...");
137+
}
138+
self.dst.flush().expect("failed to emit errors");
139+
}
140+
}
141+
}
142+
118143
impl EmitterWriter {
119144
pub fn stderr(color_config: ColorConfig,
120145
code_map: Option<Rc<CodeMapper>>,
@@ -128,13 +153,15 @@ impl EmitterWriter {
128153
cm: code_map,
129154
short_message,
130155
teach,
156+
error_codes: HashSet::new(),
131157
}
132158
} else {
133159
EmitterWriter {
134160
dst: Raw(Box::new(io::stderr())),
135161
cm: code_map,
136162
short_message,
137163
teach,
164+
error_codes: HashSet::new(),
138165
}
139166
}
140167
}
@@ -149,6 +176,7 @@ impl EmitterWriter {
149176
cm: code_map,
150177
short_message,
151178
teach,
179+
error_codes: HashSet::new(),
152180
}
153181
}
154182

@@ -975,12 +1003,14 @@ impl EmitterWriter {
9751003
if primary_span != &&DUMMY_SP {
9761004
(cm.lookup_char_pos(primary_span.lo()), cm)
9771005
} else {
978-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1006+
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
1007+
&mut self.error_codes)?;
9791008
return Ok(());
9801009
}
9811010
} else {
9821011
// If we don't have span information, emit and exit
983-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1012+
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
1013+
&mut self.error_codes)?;
9841014
return Ok(());
9851015
};
9861016
if let Ok(pos) =
@@ -1153,7 +1183,8 @@ impl EmitterWriter {
11531183
}
11541184

11551185
// final step: take our styled buffer, render it, then output it
1156-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1186+
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
1187+
&mut self.error_codes)?;
11571188

11581189
Ok(())
11591190

@@ -1241,7 +1272,8 @@ impl EmitterWriter {
12411272
let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS);
12421273
buffer.puts(row_num, 0, &msg, Style::NoStyle);
12431274
}
1244-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1275+
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
1276+
&mut self.error_codes)?;
12451277
}
12461278
Ok(())
12471279
}
@@ -1269,7 +1301,7 @@ impl EmitterWriter {
12691301
draw_col_separator_no_space(&mut buffer, 0, max_line_num_len + 1);
12701302
}
12711303
match emit_to_destination(&buffer.render(), level, &mut self.dst,
1272-
self.short_message) {
1304+
self.short_message, &mut self.error_codes) {
12731305
Ok(()) => (),
12741306
Err(e) => panic!("failed to emit error: {}", e)
12751307
}
@@ -1362,7 +1394,8 @@ fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
13621394
fn emit_to_destination(rendered_buffer: &Vec<Vec<StyledString>>,
13631395
lvl: &Level,
13641396
dst: &mut Destination,
1365-
short_message: bool)
1397+
short_message: bool,
1398+
error_codes: &mut HashSet<String>)
13661399
-> io::Result<()> {
13671400
use lock;
13681401

@@ -1383,6 +1416,9 @@ fn emit_to_destination(rendered_buffer: &Vec<Vec<StyledString>>,
13831416
for part in line {
13841417
dst.apply_style(lvl.clone(), part.style)?;
13851418
write!(dst, "{}", part.text)?;
1419+
if !short_message && part.text.len() == 12 && part.text.starts_with("error[E") {
1420+
error_codes.insert(part.text[6..11].to_owned());
1421+
}
13861422
dst.reset_attrs()?;
13871423
}
13881424
if !short_message {

0 commit comments

Comments
 (0)