Skip to content

Commit

Permalink
Auto merge of #38916 - estebank:pad-suggestion-list, r=nikomatsakis
Browse files Browse the repository at this point in the history
Teach diagnostics to correct margin of multiline messages

Make the suggestion list have a correct padding:

```
error[E0308]: mismatched types
 --> file.rs:3:20
  |
3 |     let x: usize = "";
  |                    ^^ expected usize, found reference
  |
  = note: expected type `usize`
  = note:    found type `&'static str`
  = help: here are some functions which might fulfill your needs:
          - .len()
          - .foo()
          - .bar()
```
  • Loading branch information
bors committed Jan 11, 2017
2 parents e4fee52 + 04e4a60 commit e57f061
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 18 deletions.
44 changes: 41 additions & 3 deletions src/librustc_errors/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -703,6 +703,40 @@ impl EmitterWriter {
}
}

/// Add a left margin to every line but the first, given a padding length and the label being
/// displayed.
fn msg_with_padding(&self, msg: &str, padding: usize, label: &str) -> String {
// The extra 5 ` ` is padding that's always needed to align to the `note: `:
//
// error: message
// --> file.rs:13:20
// |
// 13 | <CODE>
// | ^^^^
// |
// = note: multiline
// message
// ++^^^----xx
// | | | |
// | | | magic `2`
// | | length of label
// | magic `3`
// `max_line_num_len`
let padding = (0..padding + label.len() + 5)
.map(|_| " ")
.collect::<String>();

msg.split('\n').enumerate().fold("".to_owned(), |mut acc, x| {
if x.0 != 0 {
acc.push_str("\n");
// Align every line with first one.
acc.push_str(&padding);
}
acc.push_str(&x.1);
acc
})
}

fn emit_message_default(&mut self,
msp: &MultiSpan,
msg: &str,
Expand All @@ -721,7 +755,9 @@ impl EmitterWriter {
draw_note_separator(&mut buffer, 0, max_line_num_len + 1);
buffer.append(0, &level.to_string(), Style::HeaderMsg);
buffer.append(0, ": ", Style::NoStyle);
buffer.append(0, msg, Style::NoStyle);

let message = self.msg_with_padding(msg, max_line_num_len, "note");
buffer.append(0, &message, Style::NoStyle);
} else {
buffer.append(0, &level.to_string(), Style::Level(level.clone()));
match code {
Expand Down Expand Up @@ -854,7 +890,9 @@ impl EmitterWriter {

buffer.append(0, &level.to_string(), Style::Level(level.clone()));
buffer.append(0, ": ", Style::HeaderMsg);
buffer.append(0, msg, Style::HeaderMsg);

let message = self.msg_with_padding(msg, max_line_num_len, "suggestion");
buffer.append(0, &message, Style::HeaderMsg);

let lines = cm.span_to_lines(primary_span).unwrap();

Expand Down Expand Up @@ -930,7 +968,7 @@ impl EmitterWriter {
max_line_num_len,
true) {
Err(e) => panic!("failed to emit error: {}", e),
_ => ()
_ => (),
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_typeck/check/demand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,15 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let mut err = self.report_mismatched_types(&cause, expected, expr_ty, e);
if suggestions.len() > 0 {
err.help(&format!("here are some functions which \
might fulfill your needs:\n - {}",
self.get_best_match(&suggestions)));
might fulfill your needs:\n{}",
self.get_best_match(&suggestions).join("\n")));
};
err.emit();
}
}

fn format_method_suggestion(&self, method: &AssociatedItem) -> String {
format!(".{}({})",
format!("- .{}({})",
method.name,
if self.has_no_input_arg(method) {
""
Expand All @@ -88,15 +88,14 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
})
}

fn display_suggested_methods(&self, methods: &[AssociatedItem]) -> String {
fn display_suggested_methods(&self, methods: &[AssociatedItem]) -> Vec<String> {
methods.iter()
.take(5)
.map(|method| self.format_method_suggestion(&*method))
.collect::<Vec<String>>()
.join("\n - ")
}

fn get_best_match(&self, methods: &[AssociatedItem]) -> String {
fn get_best_match(&self, methods: &[AssociatedItem]) -> Vec<String> {
let no_argument_methods: Vec<_> =
methods.iter()
.filter(|ref x| self.has_no_input_arg(&*x))
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/resolve/token-error-correct-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ error[E0308]: mismatched types
= note: expected type `()`
= note: found type `std::result::Result<bool, std::io::Error>`
= help: here are some functions which might fulfill your needs:
- .unwrap()
- .unwrap_err()
- .unwrap_or_default()
- .unwrap()
- .unwrap_err()
- .unwrap_or_default()

error: aborting due to previous error

12 changes: 6 additions & 6 deletions src/test/ui/span/coerce-suggestions.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ error[E0308]: mismatched types
= note: expected type `usize`
= note: found type `std::string::String`
= help: here are some functions which might fulfill your needs:
- .capacity()
- .len()
- .capacity()
- .len()

error[E0308]: mismatched types
--> $DIR/coerce-suggestions.rs:23:19
Expand All @@ -19,10 +19,10 @@ error[E0308]: mismatched types
= note: expected type `&str`
= note: found type `std::string::String`
= help: here are some functions which might fulfill your needs:
- .as_str()
- .trim()
- .trim_left()
- .trim_right()
- .as_str()
- .trim()
- .trim_left()
- .trim_right()

error[E0308]: mismatched types
--> $DIR/coerce-suggestions.rs:30:10
Expand Down

0 comments on commit e57f061

Please sign in to comment.