diff options
| author | bors <bors@rust-lang.org> | 2017-05-02 01:04:27 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2017-05-02 01:04:27 +0000 |
| commit | 33535afda497e1de8a831e8270ae8099434f662b (patch) | |
| tree | 17b14bd7f6ab382784b26bdf01722525d3bbdb2f /src/librustc_errors | |
| parent | de4bdd20f87d95d164c883dc141a2763e4df155a (diff) | |
| parent | d64af4a627532c978ed2682de0e9411aa3a83e75 (diff) | |
| download | rust-33535afda497e1de8a831e8270ae8099434f662b.tar.gz rust-33535afda497e1de8a831e8270ae8099434f662b.zip | |
Auto merge of #40851 - oli-obk:multisugg, r=jonathandturner
Minimize single span suggestions into a label
changes
```
14 | println!("☃{}", tup[0]);
| ^^^^^^
|
help: to access tuple elements, use tuple indexing syntax as shown
| println!("☃{}", tup.0);
```
into
```
14 | println!("☃{}", tup[0]);
| ^^^^^^ to access tuple elements, use `tup.0`
```
Also makes suggestions explicit in the backend in preparation of adding multiple suggestions to a single diagnostic. Currently that's already possible, but results in a full help message + modified code snippet per suggestion, and has no rate limit (might show 100+ suggestions).
Diffstat (limited to 'src/librustc_errors')
| -rw-r--r-- | src/librustc_errors/diagnostic.rs | 24 | ||||
| -rw-r--r-- | src/librustc_errors/diagnostic_builder.rs | 10 | ||||
| -rw-r--r-- | src/librustc_errors/emitter.rs | 27 | ||||
| -rw-r--r-- | src/librustc_errors/lib.rs | 1 |
4 files changed, 39 insertions, 23 deletions
diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index 9715ace3e2e..38fa35ecb12 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -11,7 +11,6 @@ use CodeSuggestion; use Level; use RenderSpan; -use RenderSpan::Suggestion; use std::fmt; use syntax_pos::{MultiSpan, Span}; use snippet::Style; @@ -24,6 +23,7 @@ pub struct Diagnostic { pub code: Option<String>, pub span: MultiSpan, pub children: Vec<SubDiagnostic>, + pub suggestion: Option<CodeSuggestion>, } /// For example a note attached to an error. @@ -87,6 +87,7 @@ impl Diagnostic { code: code, span: MultiSpan::new(), children: vec![], + suggestion: None, } } @@ -202,19 +203,14 @@ impl Diagnostic { /// Prints out a message with a suggested edit of the code. /// - /// See `diagnostic::RenderSpan::Suggestion` for more information. - pub fn span_suggestion<S: Into<MultiSpan>>(&mut self, - sp: S, - msg: &str, - suggestion: String) - -> &mut Self { - self.sub(Level::Help, - msg, - MultiSpan::new(), - Some(Suggestion(CodeSuggestion { - msp: sp.into(), - substitutes: vec![suggestion], - }))); + /// See `diagnostic::CodeSuggestion` for more information. + pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self { + assert!(self.suggestion.is_none()); + self.suggestion = Some(CodeSuggestion { + msp: sp.into(), + substitutes: vec![suggestion], + msg: msg.to_owned(), + }); self } diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index 7b27f13951b..9dfd47b8464 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -141,11 +141,11 @@ impl<'a> DiagnosticBuilder<'a> { sp: S, msg: &str) -> &mut Self); - forward!(pub fn span_suggestion<S: Into<MultiSpan>>(&mut self, - sp: S, - msg: &str, - suggestion: String) - -> &mut Self); + forward!(pub fn span_suggestion(&mut self, + sp: Span, + msg: &str, + suggestion: String) + -> &mut Self); forward!(pub fn set_span<S: Into<MultiSpan>>(&mut self, sp: S) -> &mut Self); forward!(pub fn code(&mut self, s: String) -> &mut Self); diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 64652bb308b..1a38018e1b3 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -34,6 +34,27 @@ impl Emitter for EmitterWriter { fn emit(&mut self, db: &DiagnosticBuilder) { let mut primary_span = db.span.clone(); let mut children = db.children.clone(); + + if let Some(sugg) = db.suggestion.clone() { + assert_eq!(sugg.msp.primary_spans().len(), sugg.substitutes.len()); + // don't display multispans as labels + if sugg.substitutes.len() == 1 && + // don't display long messages as labels + sugg.msg.split_whitespace().count() < 10 && + // don't display multiline suggestions as labels + sugg.substitutes[0].find('\n').is_none() { + let msg = format!("help: {} `{}`", sugg.msg, sugg.substitutes[0]); + primary_span.push_span_label(sugg.msp.primary_spans()[0], msg); + } else { + children.push(SubDiagnostic { + level: Level::Help, + message: Vec::new(), + span: MultiSpan::new(), + render_span: Some(Suggestion(sugg)), + }); + } + } + self.fix_multispans_in_std_macros(&mut primary_span, &mut children); self.emit_messages_default(&db.level, &db.styled_message(), @@ -756,7 +777,7 @@ impl EmitterWriter { /// displayed, keeping the provided highlighting. fn msg_to_buffer(&self, buffer: &mut StyledBuffer, - msg: &Vec<(String, Style)>, + msg: &[(String, Style)], padding: usize, label: &str, override_style: Option<Style>) { @@ -1022,7 +1043,6 @@ impl EmitterWriter { fn emit_suggestion_default(&mut self, suggestion: &CodeSuggestion, level: &Level, - msg: &Vec<(String, Style)>, max_line_num_len: usize) -> io::Result<()> { use std::borrow::Borrow; @@ -1034,7 +1054,7 @@ impl EmitterWriter { buffer.append(0, &level.to_string(), Style::Level(level.clone())); buffer.append(0, ": ", Style::HeaderMsg); self.msg_to_buffer(&mut buffer, - msg, + &[(suggestion.msg.to_owned(), Style::NoStyle)], max_line_num_len, "suggestion", Some(Style::HeaderMsg)); @@ -1099,7 +1119,6 @@ impl EmitterWriter { Some(Suggestion(ref cs)) => { match self.emit_suggestion_default(cs, &child.level, - &child.styled_message(), max_line_num_len) { Err(e) => panic!("failed to emit error: {}", e), _ => () diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index cc8012d965a..02d8297dd46 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -67,6 +67,7 @@ pub enum RenderSpan { pub struct CodeSuggestion { pub msp: MultiSpan, pub substitutes: Vec<String>, + pub msg: String, } pub trait CodeMapper { |
