Improve E0777 help message
This commit is contained in:
parent
8b7aeefede
commit
d1d94ba026
3 changed files with 27 additions and 5 deletions
|
@ -6,6 +6,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
|
||||||
use rustc_ast::{self as ast, *};
|
use rustc_ast::{self as ast, *};
|
||||||
use rustc_data_structures::sync::Lrc;
|
use rustc_data_structures::sync::Lrc;
|
||||||
use rustc_errors::{struct_span_err, Applicability, ErrorReported};
|
use rustc_errors::{struct_span_err, Applicability, ErrorReported};
|
||||||
|
use rustc_lexer::is_ident;
|
||||||
use rustc_parse::nt_to_tokenstream;
|
use rustc_parse::nt_to_tokenstream;
|
||||||
use rustc_span::symbol::sym;
|
use rustc_span::symbol::sym;
|
||||||
use rustc_span::{Span, DUMMY_SP};
|
use rustc_span::{Span, DUMMY_SP};
|
||||||
|
@ -182,14 +183,22 @@ crate fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec<ast::Attribute>)
|
||||||
.filter_map(|nmi| match nmi {
|
.filter_map(|nmi| match nmi {
|
||||||
NestedMetaItem::Literal(lit) => {
|
NestedMetaItem::Literal(lit) => {
|
||||||
error_reported_filter_map = true;
|
error_reported_filter_map = true;
|
||||||
struct_span_err!(
|
let mut err = struct_span_err!(
|
||||||
cx.sess,
|
cx.sess,
|
||||||
lit.span,
|
lit.span,
|
||||||
E0777,
|
E0777,
|
||||||
"expected path to a trait, found literal",
|
"expected path to a trait, found literal",
|
||||||
)
|
);
|
||||||
.help("for example, write `#[derive(Debug)]` for `Debug`")
|
let token = lit.token.to_string();
|
||||||
.emit();
|
if token.starts_with('"')
|
||||||
|
&& token.len() > 2
|
||||||
|
&& is_ident(&token[1..token.len() - 1])
|
||||||
|
{
|
||||||
|
err.help(&format!("try using `#[derive({})]`", &token[1..token.len() - 1]));
|
||||||
|
} else {
|
||||||
|
err.help("for example, write `#[derive(Debug)]` for `Debug`");
|
||||||
|
}
|
||||||
|
err.emit();
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
NestedMetaItem::MetaItem(mi) => Some(mi),
|
NestedMetaItem::MetaItem(mi) => Some(mi),
|
||||||
|
|
|
@ -1,4 +1,7 @@
|
||||||
#[derive("Clone")] //~ ERROR E0777
|
#[derive("Clone")] //~ ERROR E0777
|
||||||
|
#[derive("Clone
|
||||||
|
")]
|
||||||
|
//~^^ ERROR E0777
|
||||||
struct Foo;
|
struct Foo;
|
||||||
|
|
||||||
fn main() {}
|
fn main() {}
|
||||||
|
|
|
@ -4,8 +4,18 @@ error[E0777]: expected path to a trait, found literal
|
||||||
LL | #[derive("Clone")]
|
LL | #[derive("Clone")]
|
||||||
| ^^^^^^^
|
| ^^^^^^^
|
||||||
|
|
|
|
||||||
|
= help: try using `#[derive(Clone)]`
|
||||||
|
|
||||||
|
error[E0777]: expected path to a trait, found literal
|
||||||
|
--> $DIR/E0777.rs:2:10
|
||||||
|
|
|
||||||
|
LL | #[derive("Clone
|
||||||
|
| __________^
|
||||||
|
LL | | ")]
|
||||||
|
| |_^
|
||||||
|
|
|
||||||
= help: for example, write `#[derive(Debug)]` for `Debug`
|
= help: for example, write `#[derive(Debug)]` for `Debug`
|
||||||
|
|
||||||
error: aborting due to previous error
|
error: aborting due to 2 previous errors
|
||||||
|
|
||||||
For more information about this error, try `rustc --explain E0777`.
|
For more information about this error, try `rustc --explain E0777`.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue