diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2017-01-07 21:12:33 -0800 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2017-01-07 23:34:37 -0800 |
| commit | 43b10fa8ed3dd96684ec89812dc7810431cd0d01 (patch) | |
| tree | 03a3a7fc2fb16d35528bd70d8aefc4a840642f4b /src/librustc_errors | |
| parent | 7e38a89a7b970181be083691504825a23e6b0a0f (diff) | |
| download | rust-43b10fa8ed3dd96684ec89812dc7810431cd0d01.tar.gz rust-43b10fa8ed3dd96684ec89812dc7810431cd0d01.zip | |
Teach diagnostics to have correctly padded lists
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()
```
Diffstat (limited to 'src/librustc_errors')
| -rw-r--r-- | src/librustc_errors/diagnostic.rs | 18 | ||||
| -rw-r--r-- | src/librustc_errors/diagnostic_builder.rs | 1 | ||||
| -rw-r--r-- | src/librustc_errors/emitter.rs | 19 |
3 files changed, 36 insertions, 2 deletions
diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index 730ca8f9e2e..73a8343eafc 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -32,6 +32,7 @@ pub struct SubDiagnostic { pub message: String, pub span: MultiSpan, pub render_span: Option<RenderSpan>, + pub list: Vec<String>, } impl Diagnostic { @@ -132,6 +133,11 @@ impl Diagnostic { self } + pub fn help_with_list(&mut self , msg: &str, list: Vec<String>) -> &mut Self { + self.sub_with_list(Level::Help, msg, MultiSpan::new(), None, list); + self + } + pub fn span_help<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) @@ -191,11 +197,23 @@ impl Diagnostic { message: &str, span: MultiSpan, render_span: Option<RenderSpan>) { + self.sub_with_list(level, message, span, render_span, vec![]); + } + + /// Convenience function for internal use, clients should use one of the + /// public methods above. + fn sub_with_list(&mut self, + level: Level, + message: &str, + span: MultiSpan, + render_span: Option<RenderSpan>, + list: Vec<String>) { let sub = SubDiagnostic { level: level, message: message.to_owned(), span: span, render_span: render_span, + list: list, }; self.children.push(sub); } diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index 7dfea6b8951..24f1b86739d 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -135,6 +135,7 @@ impl<'a> DiagnosticBuilder<'a> { forward!(pub fn warn(&mut self, msg: &str) -> &mut Self); forward!(pub fn span_warn<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) -> &mut Self); forward!(pub fn help(&mut self , msg: &str) -> &mut Self); + forward!(pub fn help_with_list(&mut self , msg: &str, list: Vec<String>) -> &mut Self); forward!(pub fn span_help<S: Into<MultiSpan>>(&mut self, sp: S, msg: &str) diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 808fe504b95..015997211bb 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -699,6 +699,7 @@ impl EmitterWriter { .to_string(), span: MultiSpan::new(), render_span: None, + list: vec![], }); } } @@ -923,14 +924,28 @@ impl EmitterWriter { } }, None => { + let msg = if child.list.len() == 0 { + child.message.to_owned() + } else { + format!("{}\n{}", + &child.message, + &child.list.iter().map(|item| { + format!("{} - {}", + (0..max_line_num_len) + .map(|_| " ") + .collect::<String>(), + item) + }).collect::<Vec<String>>() + .join("\n")) + }; match self.emit_message_default(&child.span, - &child.message, + &msg, &None, &child.level, max_line_num_len, true) { Err(e) => panic!("failed to emit error: {}", e), - _ => () + _ => (), } } } |
