diff options
| author | Oliver Schneider <git-spam-no-reply9815368754983@oli-obk.de> | 2017-05-10 13:19:29 +0200 |
|---|---|---|
| committer | Oliver Schneider <git-spam-no-reply9815368754983@oli-obk.de> | 2017-05-10 13:20:27 +0200 |
| commit | e2f781c7ead3a9fe69020189decc6c3eebf6f25c (patch) | |
| tree | 8a94ecdac7c2981d39e111f756541bfc4d51787a /src/librustc_errors | |
| parent | 67d762d896c8748009d1843ebf9e2e0760ed33a0 (diff) | |
| download | rust-e2f781c7ead3a9fe69020189decc6c3eebf6f25c.tar.gz rust-e2f781c7ead3a9fe69020189decc6c3eebf6f25c.zip | |
Example usage of multiple suggestions
Diffstat (limited to 'src/librustc_errors')
| -rw-r--r-- | src/librustc_errors/emitter.rs | 28 | ||||
| -rw-r--r-- | src/librustc_errors/lib.rs | 5 |
2 files changed, 23 insertions, 10 deletions
diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 564c472305c..cd72941146c 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -81,6 +81,10 @@ impl Emitter for EmitterWriter { /// maximum number of lines we will print for each error; arbitrary. pub const MAX_HIGHLIGHT_LINES: usize = 6; +/// maximum number of suggestions to be shown +/// +/// Arbitrary, but taken from trait import suggestion limit +pub const MAX_SUGGESTIONS: usize = 4; #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum ColorConfig { @@ -1077,20 +1081,22 @@ impl EmitterWriter { assert!(!lines.lines.is_empty()); - for complete in suggestion.splice_lines(cm.borrow()) { - buffer.append(0, &level.to_string(), Style::Level(level.clone())); - buffer.append(0, ": ", Style::HeaderMsg); - self.msg_to_buffer(&mut buffer, - &[(suggestion.msg.to_owned(), Style::NoStyle)], - max_line_num_len, - "suggestion", - Some(Style::HeaderMsg)); + buffer.append(0, &level.to_string(), Style::Level(level.clone())); + buffer.append(0, ": ", Style::HeaderMsg); + self.msg_to_buffer(&mut buffer, + &[(suggestion.msg.to_owned(), Style::NoStyle)], + max_line_num_len, + "suggestion", + Some(Style::HeaderMsg)); + + let suggestions = suggestion.splice_lines(cm.borrow()); + let mut row_num = 1; + for complete in suggestions.iter().take(MAX_SUGGESTIONS) { // print the suggestion without any line numbers, but leave // space for them. This helps with lining up with previous // snippets from the actual error being reported. let mut lines = complete.lines(); - let mut row_num = 1; for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) { draw_col_separator(&mut buffer, row_num, max_line_num_len + 1); buffer.append(row_num, line, Style::NoStyle); @@ -1102,6 +1108,10 @@ impl EmitterWriter { buffer.append(row_num, "...", Style::NoStyle); } } + if suggestions.len() > MAX_SUGGESTIONS { + let msg = format!("and {} other candidates", suggestions.len() - MAX_SUGGESTIONS); + buffer.append(row_num, &msg, Style::NoStyle); + } emit_to_destination(&buffer.render(), level, &mut self.dst)?; } Ok(()) diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index 8e378935094..82d688d6ba6 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -183,7 +183,10 @@ impl CodeSuggestion { prev_line = fm.get_line(prev_hi.line - 1); } for buf in &mut bufs { - push_trailing(buf, prev_line, &prev_hi, None); + // if the replacement already ends with a newline, don't print the next line + if !buf.ends_with('\n') { + push_trailing(buf, prev_line, &prev_hi, None); + } // remove trailing newline buf.pop(); } |
