Skip to content

Commit 5960da2

Browse files
authored
Rollup 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 5b13bff + 72eb010 commit 5960da2

32 files changed

+392
-66
lines changed

src/librustc_borrowck/borrowck/mod.rs

+1-1
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

+12
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

+1-1
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

+12-8
Original file line numberDiff line numberDiff line change
@@ -2307,13 +2307,15 @@ impl<'a> Resolver<'a> {
23072307
.map(|suggestion| import_candidate_to_paths(&suggestion)).collect::<Vec<_>>();
23082308
enum_candidates.sort();
23092309
for (sp, variant_path, enum_path) in enum_candidates {
2310-
let msg = format!("there is an enum variant `{}`, did you mean to use `{}`?",
2311-
variant_path,
2312-
enum_path);
23132310
if sp == DUMMY_SP {
2311+
let msg = format!("there is an enum variant `{}`, \
2312+
try using `{}`?",
2313+
variant_path,
2314+
enum_path);
23142315
err.help(&msg);
23152316
} else {
2316-
err.span_help(sp, &msg);
2317+
err.span_suggestion(span, "you can try using the variant's enum",
2318+
enum_path);
23172319
}
23182320
}
23192321
}
@@ -2322,18 +2324,20 @@ impl<'a> Resolver<'a> {
23222324
let self_is_available = this.self_value_is_available(path[0].ctxt, span);
23232325
match candidate {
23242326
AssocSuggestion::Field => {
2325-
err.span_label(span, format!("did you mean `self.{}`?", path_str));
2327+
err.span_suggestion(span, "try",
2328+
format!("self.{}", path_str));
23262329
if !self_is_available {
23272330
err.span_label(span, format!("`self` value is only available in \
23282331
methods with `self` parameter"));
23292332
}
23302333
}
23312334
AssocSuggestion::MethodWithSelf if self_is_available => {
2332-
err.span_label(span, format!("did you mean `self.{}(...)`?",
2333-
path_str));
2335+
err.span_suggestion(span, "try",
2336+
format!("self.{}", path_str));
23342337
}
23352338
AssocSuggestion::MethodWithSelf | AssocSuggestion::AssocItem => {
2336-
err.span_label(span, format!("did you mean `Self::{}`?", path_str));
2339+
err.span_suggestion(span, "try",
2340+
format!("Self::{}", path_str));
23372341
}
23382342
}
23392343
return err;

src/librustc_resolve/macros.rs

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

src/librustc_typeck/check/cast.rs

+2-2
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

+1-1
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

+2-2
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));
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+
}
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]
21+
pub fn bang_proc_macro(input: TokenStream) -> TokenStream {
22+
input
23+
}
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+
14+
#![crate_type = "proc-macro"]
15+
16+
extern crate proc_macro;
17+
18+
use proc_macro::TokenStream;
19+
20+
#[proc_macro_derive(Clona)]
21+
pub fn derive_clonea(input: TokenStream) -> TokenStream {
22+
"".parse().unwrap()
23+
}
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+
14+
#![crate_type = "proc-macro"]
15+
16+
extern crate proc_macro;
17+
18+
use proc_macro::TokenStream;
19+
20+
#[proc_macro_derive(FooWithLongName)]
21+
pub fn derive_foo(input: TokenStream) -> TokenStream {
22+
"".parse().unwrap()
23+
}

src/test/compile-fail-fulldeps/proc-macro/resolve-error.rs src/test/ui-fulldeps/resolve-error.rs

-7
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,10 @@ macro_rules! attr_proc_mac {
3636

3737
#[derive(FooWithLongNan)]
3838
//~^ ERROR cannot find derive macro `FooWithLongNan` in this scope
39-
//~^^ HELP did you mean `FooWithLongName`?
4039
struct Foo;
4140

4241
#[attr_proc_macra]
4342
//~^ ERROR cannot find attribute macro `attr_proc_macra` in this scope
44-
//~^^ HELP did you mean `attr_proc_macro`?
4543
struct Bar;
4644

4745
#[FooWithLongNan]
@@ -50,12 +48,10 @@ struct Asdf;
5048

5149
#[derive(Dlone)]
5250
//~^ ERROR cannot find derive macro `Dlone` in this scope
53-
//~^^ HELP did you mean `Clone`?
5451
struct A;
5552

5653
#[derive(Dlona)]
5754
//~^ ERROR cannot find derive macro `Dlona` in this scope
58-
//~^^ HELP did you mean `Clona`?
5955
struct B;
6056

6157
#[derive(attr_proc_macra)]
@@ -65,16 +61,13 @@ struct C;
6561
fn main() {
6662
FooWithLongNama!();
6763
//~^ ERROR cannot find macro `FooWithLongNama!` in this scope
68-
//~^^ HELP did you mean `FooWithLongNam!`?
6964

7065
attr_proc_macra!();
7166
//~^ ERROR cannot find macro `attr_proc_macra!` in this scope
72-
//~^^ HELP did you mean `attr_proc_mac!`?
7367

7468
Dlona!();
7569
//~^ ERROR cannot find macro `Dlona!` in this scope
7670

7771
bang_proc_macrp!();
7872
//~^ ERROR cannot find macro `bang_proc_macrp!` in this scope
79-
//~^^ HELP did you mean `bang_proc_macro!`?
8073
}
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
error: cannot find derive macro `FooWithLongNan` in this scope
2+
--> $DIR/resolve-error.rs:37:10
3+
|
4+
37 | #[derive(FooWithLongNan)]
5+
| ^^^^^^^^^^^^^^ help: try: `FooWithLongName`
6+
7+
error: cannot find attribute macro `attr_proc_macra` in this scope
8+
--> $DIR/resolve-error.rs:41:3
9+
|
10+
41 | #[attr_proc_macra]
11+
| ^^^^^^^^^^^^^^^ help: try: `attr_proc_macro`
12+
13+
error: cannot find attribute macro `FooWithLongNan` in this scope
14+
--> $DIR/resolve-error.rs:45:3
15+
|
16+
45 | #[FooWithLongNan]
17+
| ^^^^^^^^^^^^^^
18+
19+
error: cannot find derive macro `Dlone` in this scope
20+
--> $DIR/resolve-error.rs:49:10
21+
|
22+
49 | #[derive(Dlone)]
23+
| ^^^^^ help: try: `Clone`
24+
25+
error: cannot find derive macro `Dlona` in this scope
26+
--> $DIR/resolve-error.rs:53:10
27+
|
28+
53 | #[derive(Dlona)]
29+
| ^^^^^ help: try: `Clona`
30+
31+
error: cannot find derive macro `attr_proc_macra` in this scope
32+
--> $DIR/resolve-error.rs:57:10
33+
|
34+
57 | #[derive(attr_proc_macra)]
35+
| ^^^^^^^^^^^^^^^
36+
37+
error: cannot find macro `FooWithLongNama!` in this scope
38+
--> $DIR/resolve-error.rs:62:5
39+
|
40+
62 | FooWithLongNama!();
41+
| ^^^^^^^^^^^^^^^ help: you could try the macro: `FooWithLongNam!`
42+
43+
error: cannot find macro `attr_proc_macra!` in this scope
44+
--> $DIR/resolve-error.rs:65:5
45+
|
46+
65 | attr_proc_macra!();
47+
| ^^^^^^^^^^^^^^^ help: you could try the macro: `attr_proc_mac!`
48+
49+
error: cannot find macro `Dlona!` in this scope
50+
--> $DIR/resolve-error.rs:68:5
51+
|
52+
68 | Dlona!();
53+
| ^^^^^
54+
55+
error: cannot find macro `bang_proc_macrp!` in this scope
56+
--> $DIR/resolve-error.rs:71:5
57+
|
58+
71 | bang_proc_macrp!();
59+
| ^^^^^^^^^^^^^^^ help: you could try the macro: `bang_proc_macro!`
60+
61+
error: aborting due to 10 previous errors
62+

src/test/compile-fail/cast-to-unsized-trait-object-suggestion.rs src/test/ui/cast-to-unsized-trait-object-suggestion.rs

-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
fn main() {
1212
&1 as Send;
1313
//~^ ERROR cast to unsized type
14-
//~| HELP try casting to a reference instead:
15-
//~| SUGGESTION &1 as &Send;
1614
Box::new(1) as Send;
1715
//~^ ERROR cast to unsized type
18-
//~| HELP try casting to a `Box` instead:
19-
//~| SUGGESTION Box::new(1) as Box<Send>;
2016
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
error: cast to unsized type: `&{integer}` as `std::marker::Send`
2+
--> $DIR/cast-to-unsized-trait-object-suggestion.rs:12:5
3+
|
4+
12 | &1 as Send;
5+
| ^^^^^^----
6+
| |
7+
| help: try casting to a reference instead: `&Send`
8+
9+
error: cast to unsized type: `std::boxed::Box<{integer}>` as `std::marker::Send`
10+
--> $DIR/cast-to-unsized-trait-object-suggestion.rs:14:5
11+
|
12+
14 | Box::new(1) as Send;
13+
| ^^^^^^^^^^^^^^^----
14+
| |
15+
| help: try casting to a `Box` instead: `Box<Send>`
16+
17+
error: aborting due to 2 previous errors
18+
File renamed without changes.

0 commit comments

Comments
 (0)