Skip to content

Commit

Permalink
Change some helps to suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
oli-obk committed Jul 17, 2017
1 parent 4a28663 commit a9d9a4a
Show file tree
Hide file tree
Showing 26 changed files with 139 additions and 159 deletions.
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mod.rs
Expand Up @@ -991,7 +991,7 @@ impl<'a, 'tcx> BorrowckCtxt<'a, 'tcx> {
.span_suggestion(err.span,
&format!("to force the closure to take ownership of {} \
(and any other referenced variables), \
use the `move` keyword, as shown:",
use the `move` keyword",
cmt_path_or_string),
suggestion)
.emit();
Expand Down
12 changes: 12 additions & 0 deletions src/librustc_errors/diagnostic.rs
Expand Up @@ -211,6 +211,18 @@ impl Diagnostic {

/// Prints out a message with a suggested edit of the code.
///
/// In case of short messages and a simple suggestion,
/// rustc displays it as a label like
///
/// "try adding parentheses: `(tup.0).1`"
///
/// The message
/// * should not end in any punctuation (a `:` is added automatically)
/// * should not be a question
/// * should not contain any parts like "the following", "as shown"
/// * may look like "to do xyz, use" or "to do xyz, use abc"
/// * may contain a name of a function, variable or type, but not whole expressions
///
/// See `diagnostic::CodeSuggestion` for more information.
pub fn span_suggestion(&mut self, sp: Span, msg: &str, suggestion: String) -> &mut Self {
self.suggestions.push(CodeSuggestion {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_errors/emitter.rs
Expand Up @@ -51,7 +51,7 @@ impl Emitter for EmitterWriter {
// This substitution is only removal, don't show it
format!("help: {}", sugg.msg)
} else {
format!("help: {} `{}`", sugg.msg, substitution)
format!("help: {}: `{}`", sugg.msg, substitution)
};
primary_span.push_span_label(sugg.substitution_spans().next().unwrap(), msg);
} else {
Expand Down
20 changes: 12 additions & 8 deletions src/librustc_resolve/lib.rs
Expand Up @@ -2409,13 +2409,15 @@ impl<'a> Resolver<'a> {
.map(|suggestion| import_candidate_to_paths(&suggestion)).collect::<Vec<_>>();
enum_candidates.sort();
for (sp, variant_path, enum_path) in enum_candidates {
let msg = format!("there is an enum variant `{}`, did you mean to use `{}`?",
variant_path,
enum_path);
if sp == DUMMY_SP {
let msg = format!("there is an enum variant `{}`, \
try using `{}`?",
variant_path,
enum_path);
err.help(&msg);
} else {
err.span_help(sp, &msg);
err.span_suggestion(span, "you can try using the variant's enum",
enum_path);
}
}
}
Expand All @@ -2424,18 +2426,20 @@ impl<'a> Resolver<'a> {
let self_is_available = this.self_value_is_available(path[0].ctxt, span);
match candidate {
AssocSuggestion::Field => {
err.span_label(span, format!("did you mean `self.{}`?", path_str));
err.span_suggestion(span, "try",
format!("self.{}", path_str));
if !self_is_available {
err.span_label(span, format!("`self` value is only available in \
methods with `self` parameter"));
}
}
AssocSuggestion::MethodWithSelf if self_is_available => {
err.span_label(span, format!("did you mean `self.{}(...)`?",
path_str));
err.span_suggestion(span, "try",
format!("self.{}", path_str));
}
AssocSuggestion::MethodWithSelf | AssocSuggestion::AssocItem => {
err.span_label(span, format!("did you mean `Self::{}`?", path_str));
err.span_suggestion(span, "try",
format!("Self::{}", path_str));
}
}
return err;
Expand Down
5 changes: 3 additions & 2 deletions src/librustc_resolve/macros.rs
Expand Up @@ -658,9 +658,10 @@ impl<'a> Resolver<'a> {
if let Some(suggestion) = suggestion {
if suggestion != name {
if let MacroKind::Bang = kind {
err.help(&format!("did you mean `{}!`?", suggestion));
err.span_suggestion(span, "you could try the macro",
format!("{}!", suggestion));
} else {
err.help(&format!("did you mean `{}`?", suggestion));
err.span_suggestion(span, "try", suggestion.to_string());
}
} else {
err.help("have you added the `#[macro_use]` on the module/import?");
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_typeck/check/cast.rs
Expand Up @@ -253,7 +253,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
Ok(s) => {
err.span_suggestion(self.cast_span,
"try casting to a reference instead:",
"try casting to a reference instead",
format!("&{}{}", mtstr, s));
}
Err(_) => {
Expand All @@ -272,7 +272,7 @@ impl<'a, 'gcx, 'tcx> CastCheck<'tcx> {
match fcx.tcx.sess.codemap().span_to_snippet(self.cast_span) {
Ok(s) => {
err.span_suggestion(self.cast_span,
"try casting to a `Box` instead:",
"try casting to a `Box` instead",
format!("Box<{}>", s));
}
Err(_) => span_help!(err, self.cast_span, "did you mean `Box<{}>`?", tstr),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/op.rs
Expand Up @@ -320,7 +320,7 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
from a string reference. String concatenation \
appends the string on the right to the string \
on the left and may require reallocation. This \
requires ownership of the string on the left."), suggestion);
requires ownership of the string on the left"), suggestion);
is_string_addition = true;
}

Expand Down
4 changes: 2 additions & 2 deletions src/libsyntax/parse/parser.rs
Expand Up @@ -1490,7 +1490,7 @@ impl<'a> Parser<'a> {
s.print_bounds(" +", &bounds)?;
s.pclose()
});
err.span_suggestion(sum_span, "try adding parentheses:", sum_with_parens);
err.span_suggestion(sum_span, "try adding parentheses", sum_with_parens);
}
TyKind::Ptr(..) | TyKind::BareFn(..) => {
err.span_label(sum_span, "perhaps you forgot parentheses?");
Expand Down Expand Up @@ -5280,7 +5280,7 @@ impl<'a> Parser<'a> {
`pub(in path::to::module)`: visible only on the specified path"##;
let path = self.parse_path(PathStyle::Mod)?;
let path_span = self.prev_span;
let help_msg = format!("make this visible only to module `{}` with `in`:", path);
let help_msg = format!("make this visible only to module `{}` with `in`", path);
self.expect(&token::CloseDelim(token::Paren))?; // `)`
let mut err = self.span_fatal_help(path_span, msg, suggestion);
err.span_suggestion(path_span, &help_msg, format!("in {}", path));
Expand Down
68 changes: 27 additions & 41 deletions src/test/ui-fulldeps/resolve-error.stderr
@@ -1,76 +1,62 @@
error: cannot find derive macro `FooWithLongNan` in this scope
--> $DIR/resolve-error.rs:36:10
--> $DIR/resolve-error.rs:37:10
|
36 | #[derive(FooWithLongNan)]
| ^^^^^^^^^^^^^^
|
= help: did you mean `FooWithLongName`?
37 | #[derive(FooWithLongNan)]
| ^^^^^^^^^^^^^^ help: try: `FooWithLongName`

error: cannot find attribute macro `attr_proc_macra` in this scope
--> $DIR/resolve-error.rs:39:3
|
39 | #[attr_proc_macra]
| ^^^^^^^^^^^^^^^
--> $DIR/resolve-error.rs:40:3
|
= help: did you mean `attr_proc_macro`?
40 | #[attr_proc_macra]
| ^^^^^^^^^^^^^^^ help: try: `attr_proc_macro`

error: cannot find attribute macro `FooWithLongNan` in this scope
--> $DIR/resolve-error.rs:42:3
--> $DIR/resolve-error.rs:43:3
|
42 | #[FooWithLongNan]
43 | #[FooWithLongNan]
| ^^^^^^^^^^^^^^

error: cannot find derive macro `Dlone` in this scope
--> $DIR/resolve-error.rs:45:10
--> $DIR/resolve-error.rs:46:10
|
45 | #[derive(Dlone)]
| ^^^^^
|
= help: did you mean `Clone`?
46 | #[derive(Dlone)]
| ^^^^^ help: try: `Clone`

error: cannot find derive macro `Dlona` in this scope
--> $DIR/resolve-error.rs:48:10
|
48 | #[derive(Dlona)]
| ^^^^^
--> $DIR/resolve-error.rs:49:10
|
= help: did you mean `Clona`?
49 | #[derive(Dlona)]
| ^^^^^ help: try: `Clona`

error: cannot find derive macro `attr_proc_macra` in this scope
--> $DIR/resolve-error.rs:51:10
--> $DIR/resolve-error.rs:52:10
|
51 | #[derive(attr_proc_macra)]
52 | #[derive(attr_proc_macra)]
| ^^^^^^^^^^^^^^^

error: cannot find macro `FooWithLongNama!` in this scope
--> $DIR/resolve-error.rs:55:5
--> $DIR/resolve-error.rs:56:5
|
55 | FooWithLongNama!();
| ^^^^^^^^^^^^^^^
|
= help: did you mean `FooWithLongNam!`?
56 | FooWithLongNama!();
| ^^^^^^^^^^^^^^^ help: you could try the macro: `FooWithLongNam!`

error: cannot find macro `attr_proc_macra!` in this scope
--> $DIR/resolve-error.rs:57:5
|
57 | attr_proc_macra!();
| ^^^^^^^^^^^^^^^
--> $DIR/resolve-error.rs:58:5
|
= help: did you mean `attr_proc_mac!`?
58 | attr_proc_macra!();
| ^^^^^^^^^^^^^^^ help: you could try the macro: `attr_proc_mac!`

error: cannot find macro `Dlona!` in this scope
--> $DIR/resolve-error.rs:59:5
--> $DIR/resolve-error.rs:60:5
|
59 | Dlona!();
60 | Dlona!();
| ^^^^^

error: cannot find macro `bang_proc_macrp!` in this scope
--> $DIR/resolve-error.rs:61:5
|
61 | bang_proc_macrp!();
| ^^^^^^^^^^^^^^^
--> $DIR/resolve-error.rs:62:5
|
= help: did you mean `bang_proc_macro!`?
62 | bang_proc_macrp!();
| ^^^^^^^^^^^^^^^ help: you could try the macro: `bang_proc_macro!`

error: aborting due to 10 previous errors

6 changes: 3 additions & 3 deletions src/test/ui/cast-to-unsized-trait-object-suggestion.stderr
@@ -1,18 +1,18 @@
error: cast to unsized type: `&{integer}` as `std::marker::Send`
error[E0620]: cast to unsized type: `&{integer}` as `std::marker::Send`
--> $DIR/cast-to-unsized-trait-object-suggestion.rs:12:5
|
12 | &1 as Send;
| ^^^^^^----
| |
| help: try casting to a reference instead: `&Send`

error: cast to unsized type: `std::boxed::Box<{integer}>` as `std::marker::Send`
error[E0620]: cast to unsized type: `std::boxed::Box<{integer}>` as `std::marker::Send`
--> $DIR/cast-to-unsized-trait-object-suggestion.rs:13:5
|
13 | Box::new(1) as Send;
| ^^^^^^^^^^^^^^^----
| |
| help: try casting to a `Box` instead: `Box<Send>`

error: aborting due to previous error(s)
error: aborting due to 2 previous errors

51 changes: 23 additions & 28 deletions src/test/ui/issue-35675.stderr
Expand Up @@ -2,13 +2,10 @@ error[E0412]: cannot find type `Apple` in this scope
--> $DIR/issue-35675.rs:20:29
|
20 | fn should_return_fruit() -> Apple {
| ^^^^^ not found in this scope
|
help: there is an enum variant `Fruit::Apple`, did you mean to use `Fruit`?
--> $DIR/issue-35675.rs:14:5
|
14 | Apple(i64),
| ^^^^^^^^^^
| ^^^^^
| |
| not found in this scope
| help: you can try using the variant's enum: `Fruit`

error[E0425]: cannot find function `Apple` in this scope
--> $DIR/issue-35675.rs:23:5
Expand All @@ -17,19 +14,18 @@ error[E0425]: cannot find function `Apple` in this scope
| ^^^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
| use Fruit::Apple;
|
12 | use Fruit::Apple;
|

error[E0573]: expected type, found variant `Fruit::Apple`
--> $DIR/issue-35675.rs:28:33
|
28 | fn should_return_fruit_too() -> Fruit::Apple {
| ^^^^^^^^^^^^ not a type
|
help: there is an enum variant `Fruit::Apple`, did you mean to use `Fruit`?
--> $DIR/issue-35675.rs:14:5
|
14 | Apple(i64),
| ^^^^^^^^^^
| ^^^^^^^^^^^^
| |
| not a type
| help: you can try using the variant's enum: `Fruit`

error[E0425]: cannot find function `Apple` in this scope
--> $DIR/issue-35675.rs:31:5
Expand All @@ -38,37 +34,36 @@ error[E0425]: cannot find function `Apple` in this scope
| ^^^^^ not found in this scope
|
help: possible candidate is found in another module, you can import it into scope
| use Fruit::Apple;
|
12 | use Fruit::Apple;
|

error[E0573]: expected type, found variant `Ok`
--> $DIR/issue-35675.rs:36:13
|
36 | fn foo() -> Ok {
| ^^ not a type
|
= help: there is an enum variant `std::prelude::v1::Ok`, did you mean to use `std::prelude::v1`?
= help: there is an enum variant `std::prelude::v1::Result::Ok`, did you mean to use `std::prelude::v1::Result`?
= help: there is an enum variant `std::prelude::v1::Ok`, try using `std::prelude::v1`?
= help: there is an enum variant `std::result::Result::Ok`, try using `std::result::Result`?

error[E0412]: cannot find type `Variant3` in this scope
--> $DIR/issue-35675.rs:44:13
|
44 | fn bar() -> Variant3 {
| ^^^^^^^^ not found in this scope
|
help: there is an enum variant `x::Enum::Variant3`, did you mean to use `x::Enum`?
--> $DIR/issue-35675.rs:63:9
|
63 | Variant3(usize),
| ^^^^^^^^^^^^^^^
| ^^^^^^^^
| |
| not found in this scope
| help: you can try using the variant's enum: `x::Enum`

error[E0573]: expected type, found variant `Some`
--> $DIR/issue-35675.rs:49:13
|
49 | fn qux() -> Some {
| ^^^^ not a type
|
= help: there is an enum variant `std::option::Option::Some`, did you mean to use `std::option::Option`?
= help: there is an enum variant `std::prelude::v1::Some`, did you mean to use `std::prelude::v1`?
= help: there is an enum variant `std::option::Option::Some`, try using `std::option::Option`?
= help: there is an enum variant `std::prelude::v1::Some`, try using `std::prelude::v1`?

error: aborting due to previous error(s)
error: aborting due to 7 previous errors

2 changes: 1 addition & 1 deletion src/test/ui/issue-40402-ref-hints/issue-40402-1.stderr
Expand Up @@ -4,7 +4,7 @@ error[E0507]: cannot move out of indexed content
19 | let e = f.v[0];
| ^^^^^^
| |
| help: consider using a reference instead `&f.v[0]`
| help: consider using a reference instead: `&f.v[0]`
| cannot move out of indexed content

error: aborting due to previous error
Expand Down
6 changes: 2 additions & 4 deletions src/test/ui/macros/macro-name-typo.stderr
Expand Up @@ -2,9 +2,7 @@ error: cannot find macro `printlx!` in this scope
--> $DIR/macro-name-typo.rs:12:5
|
12 | printlx!("oh noes!");
| ^^^^^^^
|
= help: did you mean `println!`?
| ^^^^^^^ help: you could try the macro: `println!`

error: aborting due to previous error(s)
error: aborting due to previous error

6 changes: 2 additions & 4 deletions src/test/ui/macros/macro_undefined.stderr
Expand Up @@ -10,9 +10,7 @@ error: cannot find macro `k!` in this scope
--> $DIR/macro_undefined.rs:21:5
|
21 | k!();
| ^
|
= help: did you mean `kl!`?
| ^ help: you could try the macro: `kl!`

error: aborting due to previous error(s)
error: aborting due to 2 previous errors

0 comments on commit a9d9a4a

Please sign in to comment.