Skip to content

Commit e618a4f

Browse files
committed
Add some guidelines for suggestion messages and follow them
1 parent 8add02a commit e618a4f

File tree

8 files changed

+27
-15
lines changed

8 files changed

+27
-15
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
10021002
.span_suggestion(err.span,
10031003
&format!("to force the closure to take ownership of {} \
10041004
(and any other referenced variables), \
1005-
use the `move` keyword, as shown:",
1005+
use the `move` keyword",
10061006
cmt_path_or_string),
10071007
suggestion)
10081008
.emit();

src/librustc_errors/diagnostic.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,18 @@ impl Diagnostic {
203203

204204
/// Prints out a message with a suggested edit of the code.
205205
///
206+
/// In case of short messages and a simple suggestion,
207+
/// rustc displays it as a label like
208+
///
209+
/// "try adding parentheses: `(tup.0).1`"
210+
///
211+
/// The message
212+
/// * should not end in any punctuation (a `:` is added automatically)
213+
/// * should not be a question
214+
/// * should not contain any parts like "the following", "as shown"
215+
/// * may look like "to do xyz, use" or "to do xyz, use abc"
216+
/// * may contain a name of a function, variable or type, but not whole expressions
217+
///
206218
/// See `diagnostic::CodeSuggestion` for more information.
207219
pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
208220
self.suggestions.push(CodeSuggestion {

src/librustc_errors/emitter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Emitter for EmitterWriter {
4646
// don't display multiline suggestions as labels
4747
sugg.substitution_parts[0].substitutions[0].find('\n').is_none() {
4848
let substitution = &sugg.substitution_parts[0].substitutions[0];
49-
let msg = format!("help: {} `{}`", sugg.msg, substitution);
49+
let msg = format!("help: {}: `{}`", sugg.msg, substitution);
5050
primary_span.push_span_label(sugg.substitution_spans().next().unwrap(), msg);
5151
} else {
5252
// if there are multiple suggestions, print them all in full

src/librustc_resolve/lib.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,13 +2308,13 @@ impl<'a> Resolver<'a> {
23082308
enum_candidates.sort();
23092309
for (sp, variant_path, enum_path) in enum_candidates {
23102310
if sp == DUMMY_SP {
2311-
let msg = format!("there is an enum variant `{}`,\
2312-
did you mean to use `{}`?",
2311+
let msg = format!("there is an enum variant `{}`, \
2312+
try using `{}`?",
23132313
variant_path,
23142314
enum_path);
23152315
err.help(&msg);
23162316
} else {
2317-
err.span_suggestion(span, "did you mean the variant's enum", enum_path);
2317+
err.span_suggestion(span, "you can try using the variant's enum", enum_path);
23182318
}
23192319
}
23202320
}
@@ -2323,19 +2323,19 @@ impl<'a> Resolver<'a> {
23232323
let self_is_available = this.self_value_is_available(path[0].ctxt, span);
23242324
match candidate {
23252325
AssocSuggestion::Field => {
2326-
err.span_suggestion(span, "did you mean",
2326+
err.span_suggestion(span, "try",
23272327
format!("self.{}", path_str));
23282328
if !self_is_available {
23292329
err.span_label(span, format!("`self` value is only available in \
23302330
methods with `self` parameter"));
23312331
}
23322332
}
23332333
AssocSuggestion::MethodWithSelf if self_is_available => {
2334-
err.span_suggestion(span, "did you mean",
2334+
err.span_suggestion(span, "try",
23352335
format!("self.{}", path_str));
23362336
}
23372337
AssocSuggestion::MethodWithSelf | AssocSuggestion::AssocItem => {
2338-
err.span_suggestion(span, "did you mean",
2338+
err.span_suggestion(span, "try",
23392339
format!("Self::{}", path_str));
23402340
}
23412341
}

src/librustc_resolve/macros.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -649,9 +649,9 @@ impl<'a> Resolver<'a> {
649649
if let Some(suggestion) = suggestion {
650650
if suggestion != name {
651651
if let MacroKind::Bang = kind {
652-
err.span_suggestion(span, "did you mean", format!("{}!", suggestion));
652+
err.span_suggestion(span, "you could try the macro", format!("{}!", suggestion));
653653
} else {
654-
err.span_suggestion(span, "did you mean", suggestion.to_string());
654+
err.span_suggestion(span, "try", suggestion.to_string());
655655
}
656656
} else {
657657
err.help("have you added the `#[macro_use]` on the module/import?");

src/librustc_typeck/check/cast.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
276276
match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
277277
Ok(s) => {
278278
err.span_suggestion(self.cast_span,
279-
"try casting to a reference instead:",
279+
"try casting to a reference instead",
280280
format!("&{}{}", mtstr, s));
281281
}
282282
Err(_) => {
@@ -295,7 +295,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
295295
match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
296296
Ok(s) => {
297297
err.span_suggestion(self.cast_span,
298-
"try casting to a `Box` instead:",
298+
"try casting to a `Box` instead",
299299
format!("Box<{}>", s));
300300
}
301301
Err(_) => span_help!(err, self.cast_span, "did you mean `Box<{}>`?", tstr),

src/librustc_typeck/check/op.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
290290
from a string reference. String concatenation \
291291
appends the string on the right to the string \
292292
on the left and may require reallocation. This \
293-
requires ownership of the string on the left."), suggestion);
293+
requires ownership of the string on the left"), suggestion);
294294
is_string_addition = true;
295295
}
296296

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1507,7 +1507,7 @@ impl<'a> Parser<'a> {
15071507
s.print_bounds(" +", &bounds)?;
15081508
s.pclose()
15091509
});
1510-
err.span_suggestion(sum_span, "try adding parentheses:", sum_with_parens);
1510+
err.span_suggestion(sum_span, "try adding parentheses", sum_with_parens);
15111511
}
15121512
TyKind::Ptr(..) | TyKind::BareFn(..) => {
15131513
err.span_label(sum_span, "perhaps you forgot parentheses?");
@@ -5180,7 +5180,7 @@ impl<'a> Parser<'a> {
51805180
`pub(in path::to::module)`: visible only on the specified path"##;
51815181
let path = self.parse_path(PathStyle::Mod)?;
51825182
let path_span = self.prev_span;
5183-
let help_msg = format!("make this visible only to module `{}` with `in`:", path);
5183+
let help_msg = format!("make this visible only to module `{}` with `in`", path);
51845184
self.expect(&token::CloseDelim(token::Paren))?; // `)`
51855185
let mut err = self.span_fatal_help(path_span, msg, suggestion);
51865186
err.span_suggestion(path_span, &help_msg, format!("in {}", path));

0 commit comments

Comments
 (0)