Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rustc explain #48337

Merged
merged 5 commits into from
Feb 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
53 changes: 46 additions & 7 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ use std::io::prelude::*;
use std::io;
use std::rc::Rc;
use term;
use std::collections::HashMap;
use std::collections::{HashMap, HashSet};
use std::cmp::min;
use unicode_width;

Expand Down Expand Up @@ -107,6 +107,7 @@ pub struct EmitterWriter {
cm: Option<Rc<CodeMapper>>,
short_message: bool,
teach: bool,
error_codes: HashSet<String>,
}

struct FileWithAnnotatedLines {
Expand All @@ -115,6 +116,33 @@ struct FileWithAnnotatedLines {
multiline_depth: usize,
}

impl Drop for EmitterWriter {
fn drop(&mut self) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@GuillaumeGomez (more belated commentary in light of #48550) What was the motivation for putting the this in drop()? Semantically, printing the message notifying the user about --explain does not seem like a "cleanup" action. As Niko suggested, I would kind of expect this to go with the "aborting due to previous error" message in Handler.abort_if_errors—and it looks like the Handler already knows which codes have been emitted, too.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hum, if this is true, it might allow to make my task for #48562.

if !self.short_message && !self.error_codes.is_empty() {
let mut error_codes = self.error_codes.clone().into_iter().collect::<Vec<_>>();
error_codes.sort();
if error_codes.len() > 1 {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this restrict the error list to some reasonable amount? I'm ok with any amount, but I'd constrain it to at most two full 80 col lines.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it really matter? I can do it but I'm not sure if this is very useful... You're not supposed to try to have as much errors as possible when writing code haha.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be rare, but I see no reason for us not to gracefully handle that case :)

It won't ever be possible for a given project to have more than a subset of the ~600 errors the compiler has, but regardless, it is worthless to list the error codes, once the user understands how to use the flag, it makes more sense to scroll to the error that they want more information on and copy the error code from there, as the error code on it's own is meaningless.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll limit then.

let limit = if error_codes.len() > 9 { 9 } else { error_codes.len() };
writeln!(self.dst,
"You've got a few errors: {}{}",
error_codes[..limit].join(", "),
if error_codes.len() > 9 { "..." } else { "" }
).expect("failed to give tips...");
writeln!(self.dst,
"If you want more information on an error, try using \
\"rustc --explain {}\"",
&error_codes[0]).expect("failed to give tips...");
} else {
writeln!(self.dst,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this message show up for errors that don't show any extra information if --explain is run?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure to understand your point...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry I confused this with the teach flag

"If you want more information on this error, try using \
\"rustc --explain {}\"",
&error_codes[0]).expect("failed to give tips...");
}
self.dst.flush().expect("failed to emit errors");
}
}
}

impl EmitterWriter {
pub fn stderr(color_config: ColorConfig,
code_map: Option<Rc<CodeMapper>>,
Expand All @@ -128,13 +156,15 @@ impl EmitterWriter {
cm: code_map,
short_message,
teach,
error_codes: HashSet::new(),
}
} else {
EmitterWriter {
dst: Raw(Box::new(io::stderr())),
cm: code_map,
short_message,
teach,
error_codes: HashSet::new(),
}
}
}
Expand All @@ -149,6 +179,7 @@ impl EmitterWriter {
cm: code_map,
short_message,
teach,
error_codes: HashSet::new(),
}
}

Expand Down Expand Up @@ -975,12 +1006,14 @@ impl EmitterWriter {
if primary_span != &&DUMMY_SP {
(cm.lookup_char_pos(primary_span.lo()), cm)
} else {
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
&mut self.error_codes)?;
return Ok(());
}
} else {
// If we don't have span information, emit and exit
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
&mut self.error_codes)?;
return Ok(());
};
if let Ok(pos) =
Expand Down Expand Up @@ -1153,7 +1186,8 @@ impl EmitterWriter {
}

// final step: take our styled buffer, render it, then output it
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
&mut self.error_codes)?;

Ok(())

Expand Down Expand Up @@ -1241,7 +1275,8 @@ impl EmitterWriter {
let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS);
buffer.puts(row_num, 0, &msg, Style::NoStyle);
}
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message)?;
emit_to_destination(&buffer.render(), level, &mut self.dst, self.short_message,
&mut self.error_codes)?;
}
Ok(())
}
Expand Down Expand Up @@ -1269,7 +1304,7 @@ impl EmitterWriter {
draw_col_separator_no_space(&mut buffer, 0, max_line_num_len + 1);
}
match emit_to_destination(&buffer.render(), level, &mut self.dst,
self.short_message) {
self.short_message, &mut self.error_codes) {
Ok(()) => (),
Err(e) => panic!("failed to emit error: {}", e)
}
Expand Down Expand Up @@ -1362,7 +1397,8 @@ fn overlaps(a1: &Annotation, a2: &Annotation, padding: usize) -> bool {
fn emit_to_destination(rendered_buffer: &Vec<Vec<StyledString>>,
lvl: &Level,
dst: &mut Destination,
short_message: bool)
short_message: bool,
error_codes: &mut HashSet<String>)
-> io::Result<()> {
use lock;

Expand All @@ -1383,6 +1419,9 @@ fn emit_to_destination(rendered_buffer: &Vec<Vec<StyledString>>,
for part in line {
dst.apply_style(lvl.clone(), part.style)?;
write!(dst, "{}", part.text)?;
if !short_message && part.text.len() == 12 && part.text.starts_with("error[E") {
error_codes.insert(part.text[6..11].to_owned());
}
dst.reset_attrs()?;
}
if !short_message {
Expand Down
1 change: 1 addition & 0 deletions src/test/ui-fulldeps/lint-plugin-forbid-attrs.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ error[E0453]: allow(test_lint) overruled by outer forbid(test_lint)

error: aborting due to 2 previous errors

If you want more information on this error, try using "rustc --explain E0453"
1 change: 1 addition & 0 deletions src/test/ui-fulldeps/proc-macro/signature.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ error[E0308]: mismatched types

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0308"
1 change: 1 addition & 0 deletions src/test/ui/anonymous-higher-ranked-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,4 @@ note: required by `h2`

error: aborting due to 11 previous errors

If you want more information on this error, try using "rustc --explain E0631"
1 change: 1 addition & 0 deletions src/test/ui/arbitrary-self-types-not-object-safe.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ error[E0038]: the trait `Foo` cannot be made into an object

error: aborting due to 2 previous errors

If you want more information on this error, try using "rustc --explain E0038"
1 change: 1 addition & 0 deletions src/test/ui/asm-out-assign-imm.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ error[E0384]: cannot assign twice to immutable variable `x`

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0384"
1 change: 1 addition & 0 deletions src/test/ui/associated-const-impl-wrong-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ note: the lifetime 'a as defined on the impl at 17:1...

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0308"
1 change: 1 addition & 0 deletions src/test/ui/associated-const-impl-wrong-type.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ error[E0326]: implemented const `BAR` has an incompatible type for trait

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0326"
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,5 @@ error[E0221]: ambiguous associated type `Color` in bounds of `C`

error: aborting due to 4 previous errors

You've got a few errors: E0191, E0221
If you want more information on an error, try using "rustc --explain E0191"
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ error[E0277]: the trait bound `(): Add<A>` is not satisfied

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0277"
1 change: 1 addition & 0 deletions src/test/ui/associated-types-in-ambiguous-context.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ error[E0223]: ambiguous associated type

error: aborting due to 3 previous errors

If you want more information on this error, try using "rustc --explain E0223"
1 change: 1 addition & 0 deletions src/test/ui/attr-usage-repr.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,4 @@ error[E0517]: attribute should be applied to struct

error: aborting due to 5 previous errors

If you want more information on this error, try using "rustc --explain E0517"
2 changes: 2 additions & 0 deletions src/test/ui/augmented-assignments.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@ error[E0382]: use of moved value: `x`

error: aborting due to 2 previous errors

You've got a few errors: E0382, E0596
If you want more information on an error, try using "rustc --explain E0382"
1 change: 1 addition & 0 deletions src/test/ui/binary-op-on-double-ref.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ error[E0369]: binary operation `%` cannot be applied to type `&&{integer}`

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0369"
1 change: 1 addition & 0 deletions src/test/ui/blind-item-item-shadow.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ help: You can use `as` to change the binding name of the import

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0255"
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ error[E0308]: mismatched types

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0308"
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ error[E0308]: mismatched types

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0308"
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ error[E0308]: mismatched types

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0308"
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@ error[E0308]: mismatched types

error: aborting due to 2 previous errors

If you want more information on this error, try using "rustc --explain E0308"
1 change: 1 addition & 0 deletions src/test/ui/block-result/issue-11714.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ error[E0308]: mismatched types

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0308"
1 change: 1 addition & 0 deletions src/test/ui/block-result/issue-13428.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,4 @@ error[E0308]: mismatched types

error: aborting due to 2 previous errors

If you want more information on this error, try using "rustc --explain E0308"
1 change: 1 addition & 0 deletions src/test/ui/block-result/issue-13624.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ error[E0308]: mismatched types

error: aborting due to 2 previous errors

If you want more information on this error, try using "rustc --explain E0308"
2 changes: 2 additions & 0 deletions src/test/ui/block-result/issue-20862.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,5 @@ error[E0618]: expected function, found `()`

error: aborting due to 2 previous errors

You've got a few errors: E0308, E0618
If you want more information on an error, try using "rustc --explain E0308"
2 changes: 2 additions & 0 deletions src/test/ui/block-result/issue-22645.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,5 @@ error[E0308]: mismatched types

error: aborting due to 2 previous errors

You've got a few errors: E0277, E0308
If you want more information on an error, try using "rustc --explain E0277"
1 change: 1 addition & 0 deletions src/test/ui/block-result/issue-3563.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ error[E0599]: no method named `b` found for type `&Self` in the current scope

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0599"
1 change: 1 addition & 0 deletions src/test/ui/block-result/issue-5500.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ error[E0308]: mismatched types

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0308"
1 change: 1 addition & 0 deletions src/test/ui/block-result/unexpected-return-on-unit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ help: try adding a return type

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0308"
1 change: 1 addition & 0 deletions src/test/ui/bogus-tag.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ error[E0599]: no variant named `hsl` found for type `color` in the current scope

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0599"
2 changes: 2 additions & 0 deletions src/test/ui/borrowck/borrowck-box-insensitivity.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,5 @@ error[E0502]: cannot borrow `a.y` as mutable because `a.x.x` is also borrowed as

error: aborting due to 16 previous errors

You've got a few errors: E0382, E0502, E0503, E0505
If you want more information on an error, try using "rustc --explain E0382"
1 change: 1 addition & 0 deletions src/test/ui/borrowck/borrowck-closures-two-mut.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -150,3 +150,4 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time (Mir)

error: aborting due to 10 previous errors

If you want more information on this error, try using "rustc --explain E0499"
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ help: to force the closure to take ownership of `books` (and any other reference

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0373"
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ help: to force the closure to take ownership of `books` (and any other reference

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0373"
1 change: 1 addition & 0 deletions src/test/ui/borrowck/borrowck-in-static.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ error[E0507]: cannot move out of captured outer variable in an `Fn` closure

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0507"
2 changes: 2 additions & 0 deletions src/test/ui/borrowck/borrowck-move-error-with-note.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ error[E0507]: cannot move out of borrowed content

error: aborting due to 3 previous errors

You've got a few errors: E0507, E0509
If you want more information on an error, try using "rustc --explain E0507"
1 change: 1 addition & 0 deletions src/test/ui/borrowck/borrowck-move-out-of-vec-tail.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ error[E0508]: cannot move out of type `[Foo]`, a non-copy slice

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0508"
1 change: 1 addition & 0 deletions src/test/ui/borrowck/borrowck-reinit.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ error[E0382]: use of moved value: `x` (Mir)

error: aborting due to 2 previous errors

If you want more information on this error, try using "rustc --explain E0382"
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,5 @@ error[E0499]: cannot borrow `x` as mutable more than once at a time

error: aborting due to 3 previous errors

You've got a few errors: E0499, E0502
If you want more information on an error, try using "rustc --explain E0499"
2 changes: 2 additions & 0 deletions src/test/ui/borrowck/borrowck-vec-pattern-nesting.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,5 @@ error[E0508]: cannot move out of type `[std::boxed::Box<isize>]`, a non-copy sli

error: aborting due to 8 previous errors

You've got a few errors: E0506, E0508
If you want more information on an error, try using "rustc --explain E0506"
1 change: 1 addition & 0 deletions src/test/ui/borrowck/immutable-arg.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ error[E0384]: cannot assign to immutable argument `_x` (Mir)

error: aborting due to 2 previous errors

If you want more information on this error, try using "rustc --explain E0384"
1 change: 1 addition & 0 deletions src/test/ui/borrowck/issue-41962.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,4 @@ error[E0382]: use of moved value: `maybe.0` (Mir)

error: aborting due to 5 previous errors

If you want more information on this error, try using "rustc --explain E0382"
1 change: 1 addition & 0 deletions src/test/ui/borrowck/mut-borrow-in-loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,4 @@ error[E0499]: cannot borrow `*arg` as mutable more than once at a time

error: aborting due to 3 previous errors

If you want more information on this error, try using "rustc --explain E0499"
1 change: 1 addition & 0 deletions src/test/ui/borrowck/mut-borrow-outside-loop.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ error[E0499]: cannot borrow `inner_void` as mutable more than once at a time

error: aborting due to 2 previous errors

If you want more information on this error, try using "rustc --explain E0499"
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,5 @@ error[E0276]: impl has stricter requirements than trait

error: aborting due to 4 previous errors

You've got a few errors: E0195, E0276, E0308
If you want more information on an error, try using "rustc --explain E0195"
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ error[E0507]: cannot move out of captured outer variable in an `Fn` closure

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0507"
1 change: 1 addition & 0 deletions src/test/ui/cast-as-bool.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ error[E0054]: cannot cast as `bool`

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0054"
1 change: 1 addition & 0 deletions src/test/ui/cast-errors-issue-43825.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ error[E0425]: cannot find value `error` in this scope

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0425"
1 change: 1 addition & 0 deletions src/test/ui/cast-rfc0401-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ error[E0054]: cannot cast as `bool`

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0054"
1 change: 1 addition & 0 deletions src/test/ui/cast-to-unsized-trait-object-suggestion.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ error[E0620]: cast to unsized type: `std::boxed::Box<{integer}>` as `std::marker

error: aborting due to 2 previous errors

If you want more information on this error, try using "rustc --explain E0620"
1 change: 1 addition & 0 deletions src/test/ui/casts-differing-anon.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ error[E0606]: casting `*mut impl std::fmt::Debug+?Sized` as `*mut impl std::fmt:

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0606"
1 change: 1 addition & 0 deletions src/test/ui/casts-issue-46365.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ error[E0412]: cannot find type `Ipsum` in this scope

error: aborting due to previous error

If you want more information on this error, try using "rustc --explain E0412"
1 change: 1 addition & 0 deletions src/test/ui/changing-crates.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ error[E0460]: found possibly newer version of crate `a` which `b` depends on

error: aborting due to previous error

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