Skip to content

Commit 5803f99

Browse files
committed
Auto merge of rust-lang#42033 - oli-obk:suggestions, r=petrochenkov
Change some notes into suggestions r? @petrochenkov since you commented on the same edits in rust-lang#39458
2 parents 88cf76a + eb7f429 commit 5803f99

36 files changed

+438
-90
lines changed

src/librustc_borrowck/borrowck/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -991,7 +991,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
991991
.span_suggestion(err.span,
992992
&format!("to force the closure to take ownership of {} \
993993
(and any other referenced variables), \
994-
use the `move` keyword, as shown:",
994+
use the `move` keyword",
995995
cmt_path_or_string),
996996
suggestion)
997997
.emit();

src/librustc_errors/diagnostic.rs

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

212212
/// Prints out a message with a suggested edit of the code.
213213
///
214+
/// In case of short messages and a simple suggestion,
215+
/// rustc displays it as a label like
216+
///
217+
/// "try adding parentheses: `(tup.0).1`"
218+
///
219+
/// The message
220+
/// * should not end in any punctuation (a `:` is added automatically)
221+
/// * should not be a question
222+
/// * should not contain any parts like "the following", "as shown"
223+
/// * may look like "to do xyz, use" or "to do xyz, use abc"
224+
/// * may contain a name of a function, variable or type, but not whole expressions
225+
///
214226
/// See `diagnostic::CodeSuggestion` for more information.
215227
pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
216228
self.suggestions.push(CodeSuggestion {

src/librustc_errors/emitter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ impl Emitter for EmitterWriter {
5151
// This substitution is only removal, don't show it
5252
format!("help: {}", sugg.msg)
5353
} else {
54-
format!("help: {} `{}`", sugg.msg, substitution)
54+
format!("help: {}: `{}`", sugg.msg, substitution)
5555
};
5656
primary_span.push_span_label(sugg.substitution_spans().next().unwrap(), msg);
5757
} else {

src/librustc_resolve/lib.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2409,13 +2409,15 @@ impl<'a> Resolver<'a> {
24092409
.map(|suggestion| import_candidate_to_paths(&suggestion)).collect::<Vec<_>>();
24102410
enum_candidates.sort();
24112411
for (sp, variant_path, enum_path) in enum_candidates {
2412-
let msg = format!("there is an enum variant `{}`, did you mean to use `{}`?",
2413-
variant_path,
2414-
enum_path);
24152412
if sp == DUMMY_SP {
2413+
let msg = format!("there is an enum variant `{}`, \
2414+
try using `{}`?",
2415+
variant_path,
2416+
enum_path);
24162417
err.help(&msg);
24172418
} else {
2418-
err.span_help(sp, &msg);
2419+
err.span_suggestion(span, "you can try using the variant's enum",
2420+
enum_path);
24192421
}
24202422
}
24212423
}
@@ -2424,18 +2426,20 @@ impl<'a> Resolver<'a> {
24242426
let self_is_available = this.self_value_is_available(path[0].ctxt, span);
24252427
match candidate {
24262428
AssocSuggestion::Field => {
2427-
err.span_label(span, format!("did you mean `self.{}`?", path_str));
2429+
err.span_suggestion(span, "try",
2430+
format!("self.{}", path_str));
24282431
if !self_is_available {
24292432
err.span_label(span, format!("`self` value is only available in \
24302433
methods with `self` parameter"));
24312434
}
24322435
}
24332436
AssocSuggestion::MethodWithSelf if self_is_available => {
2434-
err.span_label(span, format!("did you mean `self.{}(...)`?",
2435-
path_str));
2437+
err.span_suggestion(span, "try",
2438+
format!("self.{}", path_str));
24362439
}
24372440
AssocSuggestion::MethodWithSelf | AssocSuggestion::AssocItem => {
2438-
err.span_label(span, format!("did you mean `Self::{}`?", path_str));
2441+
err.span_suggestion(span, "try",
2442+
format!("Self::{}", path_str));
24392443
}
24402444
}
24412445
return err;

src/librustc_resolve/macros.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -658,9 +658,10 @@ impl<'a> Resolver<'a> {
658658
if let Some(suggestion) = suggestion {
659659
if suggestion != name {
660660
if let MacroKind::Bang = kind {
661-
err.help(&format!("did you mean `{}!`?", suggestion));
661+
err.span_suggestion(span, "you could try the macro",
662+
format!("{}!", suggestion));
662663
} else {
663-
err.help(&format!("did you mean `{}`?", suggestion));
664+
err.span_suggestion(span, "try", suggestion.to_string());
664665
}
665666
} else {
666667
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
@@ -253,7 +253,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
253253
match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
254254
Ok(s) => {
255255
err.span_suggestion(self.cast_span,
256-
"try casting to a reference instead:",
256+
"try casting to a reference instead",
257257
format!("&{}{}", mtstr, s));
258258
}
259259
Err(_) => {
@@ -272,7 +272,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
272272
match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
273273
Ok(s) => {
274274
err.span_suggestion(self.cast_span,
275-
"try casting to a `Box` instead:",
275+
"try casting to a `Box` instead",
276276
format!("Box<{}>", s));
277277
}
278278
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
@@ -320,7 +320,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
320320
from a string reference. String concatenation \
321321
appends the string on the right to the string \
322322
on the left and may require reallocation. This \
323-
requires ownership of the string on the left."), suggestion);
323+
requires ownership of the string on the left"), suggestion);
324324
is_string_addition = true;
325325
}
326326

src/libsyntax/parse/parser.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,7 +1490,7 @@ impl<'a> Parser<'a> {
14901490
s.print_bounds(" +", &bounds)?;
14911491
s.pclose()
14921492
});
1493-
err.span_suggestion(sum_span, "try adding parentheses:", sum_with_parens);
1493+
err.span_suggestion(sum_span, "try adding parentheses", sum_with_parens);
14941494
}
14951495
TyKind::Ptr(..) | TyKind::BareFn(..) => {
14961496
err.span_label(sum_span, "perhaps you forgot parentheses?");
@@ -5280,7 +5280,7 @@ impl<'a> Parser<'a> {
52805280
`pub(in path::to::module)`: visible only on the specified path"##;
52815281
let path = self.parse_path(PathStyle::Mod)?;
52825282
let path_span = self.prev_span;
5283-
let help_msg = format!("make this visible only to module `{}` with `in`:", path);
5283+
let help_msg = format!("make this visible only to module `{}` with `in`", path);
52845284
self.expect(&token::CloseDelim(token::Paren))?; // `)`
52855285
let mut err = self.span_fatal_help(path_span, msg, suggestion);
52865286
err.span_suggestion(path_span, &help_msg, format!("in {}", path));

src/test/compile-fail/issue-35675.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,21 +12,21 @@
1212
enum Fruit { //~ HELP possible candidate is found in another module, you can import it into scope
1313
//~^ HELP possible candidate is found in another module, you can import it into scope
1414
Apple(i64),
15-
//~^ HELP there is an enum variant `Fruit::Apple`, did you mean to use `Fruit`?
16-
//~| HELP there is an enum variant `Fruit::Apple`, did you mean to use `Fruit`?
1715
Orange(i64),
1816
}
1917

2018
fn should_return_fruit() -> Apple {
2119
//~^ ERROR cannot find type `Apple` in this scope
2220
//~| NOTE not found in this scope
21+
//~| HELP you can try using the variant's enum
2322
Apple(5)
2423
//~^ ERROR cannot find function `Apple` in this scope
2524
//~| NOTE not found in this scope
2625
}
2726

2827
fn should_return_fruit_too() -> Fruit::Apple {
2928
//~^ ERROR expected type, found variant `Fruit::Apple`
29+
//~| HELP you can try using the variant's enum
3030
//~| NOTE not a type
3131
Apple(5)
3232
//~^ ERROR cannot find function `Apple` in this scope
@@ -43,6 +43,7 @@ fn foo() -> Ok {
4343

4444
fn bar() -> Variant3 {
4545
//~^ ERROR cannot find type `Variant3` in this scope
46+
//~| HELP you can try using the variant's enum
4647
//~| NOTE not found in this scope
4748
}
4849

@@ -61,7 +62,6 @@ mod x {
6162
Variant1,
6263
Variant2(),
6364
Variant3(usize),
64-
//~^ HELP there is an enum variant `x::Enum::Variant3`, did you mean to use `x::Enum`?
6565
Variant4 {},
6666
}
6767
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// force-host
12+
// no-prefer-dynamic
13+
#![feature(proc_macro)]
14+
#![crate_type = "proc-macro"]
15+
16+
extern crate proc_macro;
17+
18+
use proc_macro::TokenStream;
19+
20+
#[proc_macro_attribute]
21+
pub fn attr_proc_macro(_: TokenStream, input: TokenStream) -> TokenStream {
22+
input
23+
}

0 commit comments

Comments
 (0)