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

+46-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,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

+1
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

+1
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

+1
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

+1
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

+1
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

+1
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

+1
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

+2
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

+1
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"

src/test/ui/associated-types-in-ambiguous-context.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ error[E0223]: ambiguous associated type
2424

2525
error: aborting due to 3 previous errors
2626

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

src/test/ui/attr-usage-repr.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -40,3 +40,4 @@ error[E0517]: attribute should be applied to struct
4040

4141
error: aborting due to 5 previous errors
4242

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

src/test/ui/augmented-assignments.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,5 @@ error[E0382]: use of moved value: `x`
2020

2121
error: aborting due to 2 previous errors
2222

23+
You've got a few errors: E0382, E0596
24+
If you want more information on an error, try using "rustc --explain E0382"

src/test/ui/binary-op-on-double-ref.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ error[E0369]: binary operation `%` cannot be applied to type `&&{integer}`
99

1010
error: aborting due to previous error
1111

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

src/test/ui/blind-item-item-shadow.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ help: You can use `as` to change the binding name of the import
1515

1616
error: aborting due to previous error
1717

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

src/test/ui/block-result/block-must-not-have-result-do.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ error[E0308]: mismatched types
99

1010
error: aborting due to previous error
1111

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

src/test/ui/block-result/block-must-not-have-result-res.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ error[E0308]: mismatched types
1111

1212
error: aborting due to previous error
1313

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

src/test/ui/block-result/block-must-not-have-result-while.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ error[E0308]: mismatched types
99

1010
error: aborting due to previous error
1111

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

src/test/ui/block-result/consider-removing-last-semi.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ error[E0308]: mismatched types
2828

2929
error: aborting due to 2 previous errors
3030

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

src/test/ui/block-result/issue-11714.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ error[E0308]: mismatched types
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/block-result/issue-13428.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,4 @@ error[E0308]: mismatched types
3131

3232
error: aborting due to 2 previous errors
3333

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

src/test/ui/block-result/issue-13624.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ error[E0308]: mismatched types
2020

2121
error: aborting due to 2 previous errors
2222

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

src/test/ui/block-result/issue-20862.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,5 @@ error[E0618]: expected function, found `()`
1717

1818
error: aborting due to 2 previous errors
1919

20+
You've got a few errors: E0308, E0618
21+
If you want more information on an error, try using "rustc --explain E0308"

src/test/ui/block-result/issue-22645.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,5 @@ error[E0308]: mismatched types
2222

2323
error: aborting due to 2 previous errors
2424

25+
You've got a few errors: E0277, E0308
26+
If you want more information on an error, try using "rustc --explain E0277"

src/test/ui/block-result/issue-3563.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ error[E0599]: no method named `b` found for type `&Self` in the current scope
88

99
error: aborting due to previous error
1010

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

src/test/ui/block-result/issue-5500.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ error[E0308]: mismatched types
1111

1212
error: aborting due to previous error
1313

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

src/test/ui/block-result/unexpected-return-on-unit.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,4 @@ help: try adding a return type
1717

1818
error: aborting due to previous error
1919

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

src/test/ui/bogus-tag.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ error[E0599]: no variant named `hsl` found for type `color` in the current scope
99

1010
error: aborting due to previous error
1111

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

src/test/ui/borrowck/borrowck-box-insensitivity.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -161,3 +161,5 @@ error[E0502]: cannot borrow `a.y` as mutable because `a.x.x` is also borrowed as
161161

162162
error: aborting due to 16 previous errors
163163

164+
You've got a few errors: E0382, E0502, E0503, E0505
165+
If you want more information on an error, try using "rustc --explain E0382"

src/test/ui/borrowck/borrowck-closures-two-mut.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,4 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time (Mir)
150150

151151
error: aborting due to 10 previous errors
152152

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

src/test/ui/borrowck/borrowck-escaping-closure-error-1.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ help: to force the closure to take ownership of `books` (and any other reference
1212

1313
error: aborting due to previous error
1414

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

src/test/ui/borrowck/borrowck-escaping-closure-error-2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,4 @@ help: to force the closure to take ownership of `books` (and any other reference
1212

1313
error: aborting due to previous error
1414

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

src/test/ui/borrowck/borrowck-in-static.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ error[E0507]: cannot move out of captured outer variable in an `Fn` closure
88

99
error: aborting due to previous error
1010

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

src/test/ui/borrowck/borrowck-move-error-with-note.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,5 @@ error[E0507]: cannot move out of borrowed content
3434

3535
error: aborting due to 3 previous errors
3636

37+
You've got a few errors: E0507, E0509
38+
If you want more information on an error, try using "rustc --explain E0507"

src/test/ui/borrowck/borrowck-move-out-of-vec-tail.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ error[E0508]: cannot move out of type `[Foo]`, a non-copy slice
1515

1616
error: aborting due to previous error
1717

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

src/test/ui/borrowck/borrowck-reinit.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ error[E0382]: use of moved value: `x` (Mir)
2020

2121
error: aborting due to 2 previous errors
2222

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

src/test/ui/borrowck/borrowck-report-with-custom-diagnostic.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,5 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time
3636

3737
error: aborting due to 3 previous errors
3838

39+
You've got a few errors: E0499, E0502
40+
If you want more information on an error, try using "rustc --explain E0499"

src/test/ui/borrowck/borrowck-vec-pattern-nesting.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,5 @@ error[E0508]: cannot move out of type `[std::boxed::Box<isize>]`, a non-copy sli
8080

8181
error: aborting due to 8 previous errors
8282

83+
You've got a few errors: E0506, E0508
84+
If you want more information on an error, try using "rustc --explain E0506"

src/test/ui/borrowck/immutable-arg.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ error[E0384]: cannot assign to immutable argument `_x` (Mir)
1616

1717
error: aborting due to 2 previous errors
1818

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

src/test/ui/borrowck/issue-41962.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,4 @@ error[E0382]: use of moved value: `maybe.0` (Mir)
5454

5555
error: aborting due to 5 previous errors
5656

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

src/test/ui/borrowck/mut-borrow-in-loop.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ error[E0499]: cannot borrow `*arg` as mutable more than once at a time
2727

2828
error: aborting due to 3 previous errors
2929

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

src/test/ui/borrowck/mut-borrow-outside-loop.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,4 @@ error[E0499]: cannot borrow `inner_void` as mutable more than once at a time
2121

2222
error: aborting due to 2 previous errors
2323

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

src/test/ui/borrowck/regions-bound-missing-bound-in-impl.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,5 @@ error[E0276]: impl has stricter requirements than trait
4646

4747
error: aborting due to 4 previous errors
4848

49+
You've got a few errors: E0195, E0276, E0308
50+
If you want more information on an error, try using "rustc --explain E0195"

src/test/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ error[E0507]: cannot move out of captured outer variable in an `Fn` closure
99

1010
error: aborting due to previous error
1111

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

src/test/ui/cast-as-bool.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ error[E0054]: cannot cast as `bool`
88

99
error: aborting due to previous error
1010

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

src/test/ui/cast-errors-issue-43825.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ error[E0425]: cannot find value `error` in this scope
66

77
error: aborting due to previous error
88

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

src/test/ui/cast-rfc0401-2.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ error[E0054]: cannot cast as `bool`
88

99
error: aborting due to previous error
1010

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

src/test/ui/cast-to-unsized-trait-object-suggestion.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -16,3 +16,4 @@ error[E0620]: cast to unsized type: `std::boxed::Box<{integer}>` as `std::marker
1616

1717
error: aborting due to 2 previous errors
1818

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

src/test/ui/casts-differing-anon.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ error[E0606]: casting `*mut impl std::fmt::Debug+?Sized` as `*mut impl std::fmt:
88

99
error: aborting due to previous error
1010

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

src/test/ui/casts-issue-46365.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@ error[E0412]: cannot find type `Ipsum` in this scope
66

77
error: aborting due to previous error
88

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

src/test/ui/changing-crates.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ error[E0460]: found possibly newer version of crate `a` which `b` depends on
1111

1212
error: aborting due to previous error
1313

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

0 commit comments

Comments
 (0)