diff options
| author | Esteban Küber <esteban@kuber.com.ar> | 2017-07-16 11:43:24 -0700 |
|---|---|---|
| committer | Esteban Küber <esteban@kuber.com.ar> | 2017-07-17 09:27:51 -0700 |
| commit | faf90351b79de04de0176b1e0de07cc5f1730eaa (patch) | |
| tree | 56e8a580fc683a21d2b6e623af9758e0b36b250c | |
| parent | 7239d7717188f6dc2b380a40335a0c8571be6502 (diff) | |
Add flag to hide code on inline suggestions
Now there's a way to add suggestions that hide the suggested code when presented inline, to avoid weird wording when short code snippets are added at the end.
| -rw-r--r-- | src/librustc_errors/diagnostic.rs | 18 | ||||
| -rw-r--r-- | src/librustc_errors/diagnostic_builder.rs | 5 | ||||
| -rw-r--r-- | src/librustc_errors/emitter.rs | 5 | ||||
| -rw-r--r-- | src/librustc_errors/lib.rs | 1 | ||||
| -rw-r--r-- | src/libsyntax/parse/parser.rs | 6 | ||||
| -rw-r--r-- | src/test/ui/issue-22644.stderr | 2 | ||||
| -rw-r--r-- | src/test/ui/suggestions/type-ascription-instead-of-statement-end.stderr | 2 |
7 files changed, 32 insertions, 7 deletions
diff --git a/src/librustc_errors/diagnostic.rs b/src/librustc_errors/diagnostic.rs index d7c21127474..2ffa5fc796e 100644 --- a/src/librustc_errors/diagnostic.rs +++ b/src/librustc_errors/diagnostic.rs @@ -209,6 +209,22 @@ impl Diagnostic { self } + /// Prints out a message with a suggested edit of the code. If the suggestion is presented + /// inline it will only show the text message and not the text. + /// + /// See `diagnostic::CodeSuggestion` for more information. + pub fn span_suggestion_short(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self { + self.suggestions.push(CodeSuggestion { + substitution_parts: vec![Substitution { + span: sp, + substitutions: vec![suggestion], + }], + msg: msg.to_owned(), + show_code_when_inline: false, + }); + self + } + /// Prints out a message with a suggested edit of the code. /// /// See `diagnostic::CodeSuggestion` for more information. @@ -219,6 +235,7 @@ impl Diagnostic { substitutions: vec![suggestion], }], msg: msg.to_owned(), + show_code_when_inline: true, }); self } @@ -230,6 +247,7 @@ impl Diagnostic { substitutions: suggestions, }], msg: msg.to_owned(), + show_code_when_inline: true, }); self } diff --git a/src/librustc_errors/diagnostic_builder.rs b/src/librustc_errors/diagnostic_builder.rs index 0081339a363..6f6470089d7 100644 --- a/src/librustc_errors/diagnostic_builder.rs +++ b/src/librustc_errors/diagnostic_builder.rs @@ -146,6 +146,11 @@ impl<'a> DiagnosticBuilder<'a> { sp: S, msg: &str) -> &mut Self); + forward!(pub fn span_suggestion_short(&mut self, + sp: Span, + msg: &str, + suggestion: String) + -> &mut Self); forward!(pub fn span_suggestion(&mut self, sp: Span, msg: &str, diff --git a/src/librustc_errors/emitter.rs b/src/librustc_errors/emitter.rs index 2aea6d125f2..94b3a4396d7 100644 --- a/src/librustc_errors/emitter.rs +++ b/src/librustc_errors/emitter.rs @@ -47,8 +47,9 @@ impl Emitter for EmitterWriter { // don't display multiline suggestions as labels sugg.substitution_parts[0].substitutions[0].find('\n').is_none() { let substitution = &sugg.substitution_parts[0].substitutions[0]; - let msg = if substitution.len() == 0 { - // This substitution is only removal, don't show it + let msg = if substitution.len() == 0 || !sugg.show_code_when_inline { + // This substitution is only removal or we explicitely don't want to show the + // code inline, don't show it format!("help: {}", sugg.msg) } else { format!("help: {} `{}`", sugg.msg, substitution) diff --git a/src/librustc_errors/lib.rs b/src/librustc_errors/lib.rs index c4beb5ebc42..e873137444d 100644 --- a/src/librustc_errors/lib.rs +++ b/src/librustc_errors/lib.rs @@ -84,6 +84,7 @@ pub struct CodeSuggestion { /// ``` pub substitution_parts: Vec<Substitution>, pub msg: String, + pub show_code_when_inline: bool, } #[derive(Clone, Debug, PartialEq, RustcEncodable, RustcDecodable)] diff --git a/src/libsyntax/parse/parser.rs b/src/libsyntax/parse/parser.rs index 1b6c3cf94e4..b3291e9e0b5 100644 --- a/src/libsyntax/parse/parser.rs +++ b/src/libsyntax/parse/parser.rs @@ -2807,9 +2807,9 @@ impl<'a> Parser<'a> { let cur_pos = cm.lookup_char_pos(self.span.lo); let op_pos = cm.lookup_char_pos(cur_op_span.hi); if cur_pos.line != op_pos.line { - err.span_suggestion(cur_op_span, - "did you mean to end the statement here instead?", - ";".to_string()); + err.span_suggestion_short(cur_op_span, + "did you mean to use `;` here?", + ";".to_string()); } return Err(err); } diff --git a/src/test/ui/issue-22644.stderr b/src/test/ui/issue-22644.stderr index 144c5ef1ed6..fd39a27a9f7 100644 --- a/src/test/ui/issue-22644.stderr +++ b/src/test/ui/issue-22644.stderr @@ -100,5 +100,5 @@ error: expected type, found `4` --> $DIR/issue-22644.rs:38:28 | 38 | println!("{}", a: &mut 4); - | ^ + | ^ expecting a type here because of type ascription diff --git a/src/test/ui/suggestions/type-ascription-instead-of-statement-end.stderr b/src/test/ui/suggestions/type-ascription-instead-of-statement-end.stderr index e4cf78dbb2d..550048c7b88 100644 --- a/src/test/ui/suggestions/type-ascription-instead-of-statement-end.stderr +++ b/src/test/ui/suggestions/type-ascription-instead-of-statement-end.stderr @@ -2,7 +2,7 @@ error: expected type, found `0` --> $DIR/type-ascription-instead-of-statement-end.rs:15:5 | 14 | println!("test"): - | - help: did you mean to end the statement here instead? `;` + | - help: did you mean to use `;` here? 15 | 0; | ^ expecting a type here because of type ascription |
