Skip to content

Commit bedbad6

Browse files
committed
Auto merge of #48337 - GuillaumeGomez:rustc-explain, r=estebank
Rustc explain Fixes #48041. To make the review easier, I separated tests update to code update. Also, I used this script to generate new ui tests stderr: ```python from os import listdir from os.path import isdir, isfile, join PATH = "src/test/ui" def do_something(path): files = [join(path, f) for f in listdir(path)] for f in files: if isdir(f): do_something(f) continue if not isfile(f) or not f.endswith(".stderr"): continue x = open(f, "r") content = x.read().strip() if "error[E" not in content: continue errors = dict() for y in content.splitlines(): if y.startswith("error[E"): errors[y[6:11]] = True errors = sorted(errors.keys()) if len(errors) < 1: print("weird... {}".format(f)) continue if len(errors) > 1: content += "\n\nYou've got a few errors: {}".format(", ".join(errors)) content += "\nIf you want more information on an error, try using \"rustc --explain {}\"".format(errors[0]) else: content += "\n\nIf you want more information on this error, try using \"rustc --explain {}\"".format(errors[0]) content += "\n" x = open(f, "w") x.write(content) do_something(PATH) ```
2 parents 4a70e27 + ce6429a commit bedbad6

File tree

1,042 files changed

+1352
-45
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,042 files changed

+1352
-45
lines changed

src/librustc_errors/emitter.rs

Lines changed: 46 additions & 7 deletions
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,33 @@ 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+
let limit = if error_codes.len() > 9 { 9 } else { error_codes.len() };
126+
writeln!(self.dst,
127+
"You've got a few errors: {}{}",
128+
error_codes[..limit].join(", "),
129+
if error_codes.len() > 9 { "..." } else { "" }
130+
).expect("failed to give tips...");
131+
writeln!(self.dst,
132+
"If you want more information on an error, try using \
133+
\"rustc --explain {}\"",
134+
&error_codes[0]).expect("failed to give tips...");
135+
} else {
136+
writeln!(self.dst,
137+
"If you want more information on this error, try using \
138+
\"rustc --explain {}\"",
139+
&error_codes[0]).expect("failed to give tips...");
140+
}
141+
self.dst.flush().expect("failed to emit errors");
142+
}
143+
}
144+
}
145+
118146
impl EmitterWriter {
119147
pub fn stderr(color_config: ColorConfig,
120148
code_map: Option<Rc<CodeMapper>>,
@@ -128,13 +156,15 @@ impl EmitterWriter {
128156
cm: code_map,
129157
short_message,
130158
teach,
159+
error_codes: HashSet::new(),
131160
}
132161
} else {
133162
EmitterWriter {
134163
dst: Raw(Box::new(io::stderr())),
135164
cm: code_map,
136165
short_message,
137166
teach,
167+
error_codes: HashSet::new(),
138168
}
139169
}
140170
}
@@ -149,6 +179,7 @@ impl EmitterWriter {
149179
cm: code_map,
150180
short_message,
151181
teach,
182+
error_codes: HashSet::new(),
152183
}
153184
}
154185

@@ -975,12 +1006,14 @@ impl EmitterWriter {
9751006
if primary_span != &&DUMMY_SP {
9761007
(cm.lookup_char_pos(primary_span.lo()), cm)
9771008
} else {
978-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1009+
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
1010+
&mut self.error_codes)?;
9791011
return Ok(());
9801012
}
9811013
} else {
9821014
// If we don't have span information, emit and exit
983-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1015+
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
1016+
&mut self.error_codes)?;
9841017
return Ok(());
9851018
};
9861019
if let Ok(pos) =
@@ -1153,7 +1186,8 @@ impl EmitterWriter {
11531186
}
11541187

11551188
// final step: take our styled buffer, render it, then output it
1156-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1189+
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
1190+
&mut self.error_codes)?;
11571191

11581192
Ok(())
11591193

@@ -1241,7 +1275,8 @@ impl EmitterWriter {
12411275
let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS);
12421276
buffer.puts(row_num, 0, &msg, Style::NoStyle);
12431277
}
1244-
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
1278+
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
1279+
&mut self.error_codes)?;
12451280
}
12461281
Ok(())
12471282
}
@@ -1269,7 +1304,7 @@ impl EmitterWriter {
12691304
draw_col_separator_no_space(&mut buffer, 0, max_line_num_len + 1);
12701305
}
12711306
match emit_to_destination(&buffer.render(), level, &mut self.dst,
1272-
self.short_message) {
1307+
self.short_message, &mut self.error_codes) {
12731308
Ok(()) => (),
12741309
Err(e) => panic!("failed to emit error: {}", e)
12751310
}
@@ -1362,7 +1397,8 @@ fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
13621397
fn emit_to_destination(rendered_buffer: &Vec<Vec<StyledString>>,
13631398
lvl: &Level,
13641399
dst: &mut Destination,
1365-
short_message: bool)
1400+
short_message: bool,
1401+
error_codes: &mut HashSet<String>)
13661402
-> io::Result<()> {
13671403
use lock;
13681404

@@ -1383,6 +1419,9 @@ fn emit_to_destination(rendered_buffer: &Vec<Vec<StyledString>>,
13831419
for part in line {
13841420
dst.apply_style(lvl.clone(), part.style)?;
13851421
write!(dst, "{}", part.text)?;
1422+
if !short_message && part.text.len() == 12 && part.text.starts_with("error[E") {
1423+
error_codes.insert(part.text[6..11].to_owned());
1424+
}
13861425
dst.reset_attrs()?;
13871426
}
13881427
if !short_message {

src/test/ui-fulldeps/lint-plugin-forbid-attrs.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ error[E0453]: allow(test_lint) overruled by outer forbid(test_lint)
2121

2222
error: aborting due to 2 previous errors
2323

24+
If you want more information on this error, try using "rustc --explain E0453"

src/test/ui-fulldeps/proc-macro/signature.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ error[E0308]: mismatched types
1212

1313
error: aborting due to previous error
1414

15+
If you want more information on this error, try using "rustc --explain E0308"

src/test/ui/anonymous-higher-ranked-lifetime.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,4 @@ note: required by `h2`
154154

155155
error: aborting due to 11 previous errors
156156

157+
If you want more information on this error, try using "rustc --explain E0631"

src/test/ui/arbitrary-self-types-not-object-safe.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ error[E0038]: the trait `Foo` cannot be made into an object
1717

1818
error: aborting due to 2 previous errors
1919

20+
If you want more information on this error, try using "rustc --explain E0038"

src/test/ui/asm-out-assign-imm.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ error[E0384]: cannot assign twice to immutable variable `x`
99

1010
error: aborting due to previous error
1111

12+
If you want more information on this error, try using "rustc --explain E0384"

src/test/ui/associated-const-impl-wrong-lifetime.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ note: the lifetime 'a as defined on the impl at 17:1...
1515

1616
error: aborting due to previous error
1717

18+
If you want more information on this error, try using "rustc --explain E0308"

src/test/ui/associated-const-impl-wrong-type.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ error[E0326]: implemented const `BAR` has an incompatible type for trait
99

1010
error: aborting due to previous error
1111

12+
If you want more information on this error, try using "rustc --explain E0326"

src/test/ui/associated-type-projection-from-multiple-supertraits.stderr

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,5 @@ error[E0221]: ambiguous associated type `Color` in bounds of `C`
4242

4343
error: aborting due to 4 previous errors
4444

45+
You've got a few errors: E0191, E0221
46+
If you want more information on an error, try using "rustc --explain E0191"

src/test/ui/associated-types-ICE-when-projecting-out-of-err.stderr

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ error[E0277]: the trait bound `(): Add<A>` is not satisfied
66

77
error: aborting due to previous error
88

9+
If you want more information on this error, try using "rustc --explain E0277"

0 commit comments

Comments
 (0)