Skip to content

Commit

Permalink
Rollup merge of rust-lang#42033 - oli-obk:suggestions, r=petrochenkov
Browse files Browse the repository at this point in the history
Change some notes into suggestions

r? @petrochenkov since you commented on the same edits in rust-lang#39458
  • Loading branch information
Mark-Simulacrum authored May 23, 2017
2 parents 5b13bff + 72eb010 commit c20a157
Show file tree
Hide file tree
Showing 32 changed files with 392 additions and 66 deletions.
2 changes: 1 addition & 1 deletion src/librustc_borrowck/borrowck/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,7 +1002,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
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,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
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ 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 = format!("help: {} `{}`", sugg.msg, substitution);
let msg = format!("help: {}: `{}`", sugg.msg, substitution);
primary_span.push_span_label(sugg.substitution_spans().next().unwrap(), msg);
} else {
// if there are multiple suggestions, print them all in full
Expand Down
20 changes: 12 additions & 8 deletions src/librustc_resolve/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2307,13 +2307,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 @@ -2322,18 +2324,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
Original file line number Diff line number Diff line change
Expand Up @@ -649,9 +649,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
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,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 @@ -295,7 +295,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
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,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
Original file line number Diff line number Diff line change
Expand Up @@ -1507,7 +1507,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 @@ -5180,7 +5180,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
23 changes: 23 additions & 0 deletions src/test/ui-fulldeps/auxiliary/attr_proc_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// force-host
// no-prefer-dynamic
#![feature(proc_macro)]
#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_attribute]
pub fn attr_proc_macro(_: TokenStream, input: TokenStream) -> TokenStream {
input
}
23 changes: 23 additions & 0 deletions src/test/ui-fulldeps/auxiliary/bang_proc_macro.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// force-host
// no-prefer-dynamic
#![feature(proc_macro)]
#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro]
pub fn bang_proc_macro(input: TokenStream) -> TokenStream {
input
}
23 changes: 23 additions & 0 deletions src/test/ui-fulldeps/auxiliary/derive-clona.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// force-host
// no-prefer-dynamic

#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_derive(Clona)]
pub fn derive_clonea(input: TokenStream) -> TokenStream {
"".parse().unwrap()
}
23 changes: 23 additions & 0 deletions src/test/ui-fulldeps/auxiliary/derive-foo.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

// force-host
// no-prefer-dynamic

#![crate_type = "proc-macro"]

extern crate proc_macro;

use proc_macro::TokenStream;

#[proc_macro_derive(FooWithLongName)]
pub fn derive_foo(input: TokenStream) -> TokenStream {
"".parse().unwrap()
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,10 @@ macro_rules! attr_proc_mac {

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

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

#[FooWithLongNan]
Expand All @@ -50,12 +48,10 @@ struct Asdf;

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

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

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

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

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

bang_proc_macrp!();
//~^ ERROR cannot find macro `bang_proc_macrp!` in this scope
//~^^ HELP did you mean `bang_proc_macro!`?
}
62 changes: 62 additions & 0 deletions src/test/ui-fulldeps/resolve-error.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
error: cannot find derive macro `FooWithLongNan` in this scope
--> $DIR/resolve-error.rs:37:10
|
37 | #[derive(FooWithLongNan)]
| ^^^^^^^^^^^^^^ help: try: `FooWithLongName`

error: cannot find attribute macro `attr_proc_macra` in this scope
--> $DIR/resolve-error.rs:41:3
|
41 | #[attr_proc_macra]
| ^^^^^^^^^^^^^^^ help: try: `attr_proc_macro`

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

error: cannot find derive macro `Dlone` in this scope
--> $DIR/resolve-error.rs:49:10
|
49 | #[derive(Dlone)]
| ^^^^^ help: try: `Clone`

error: cannot find derive macro `Dlona` in this scope
--> $DIR/resolve-error.rs:53:10
|
53 | #[derive(Dlona)]
| ^^^^^ help: try: `Clona`

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

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

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

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

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

error: aborting due to 10 previous errors

Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
fn main() {
&1 as Send;
//~^ ERROR cast to unsized type
//~| HELP try casting to a reference instead:
//~| SUGGESTION &1 as &Send;
Box::new(1) as Send;
//~^ ERROR cast to unsized type
//~| HELP try casting to a `Box` instead:
//~| SUGGESTION Box::new(1) as Box<Send>;
}
18 changes: 18 additions & 0 deletions src/test/ui/cast-to-unsized-trait-object-suggestion.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
error: 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`
--> $DIR/cast-to-unsized-trait-object-suggestion.rs:14:5
|
14 | Box::new(1) as Send;
| ^^^^^^^^^^^^^^^----
| |
| help: try casting to a `Box` instead: `Box<Send>`

error: aborting due to 2 previous errors

File renamed without changes.
Loading

0 comments on commit c20a157

Please sign in to comment.