Skip to content

Commit d1d94ba

Browse files
Improve E0777 help message
1 parent 8b7aeef commit d1d94ba

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

compiler/rustc_expand/src/proc_macro.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree};
66
use rustc_ast::{self as ast, *};
77
use rustc_data_structures::sync::Lrc;
88
use rustc_errors::{struct_span_err, Applicability, ErrorReported};
9+
use rustc_lexer::is_ident;
910
use rustc_parse::nt_to_tokenstream;
1011
use rustc_span::symbol::sym;
1112
use rustc_span::{Span, DUMMY_SP};
@@ -182,14 +183,22 @@ crate fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec<ast::Attribute>)
182183
.filter_map(|nmi| match nmi {
183184
NestedMetaItem::Literal(lit) => {
184185
error_reported_filter_map = true;
185-
struct_span_err!(
186+
let mut err = struct_span_err!(
186187
cx.sess,
187188
lit.span,
188189
E0777,
189190
"expected path to a trait, found literal",
190-
)
191-
.help("for example, write `#[derive(Debug)]` for `Debug`")
192-
.emit();
191+
);
192+
let token = lit.token.to_string();
193+
if token.starts_with('"')
194+
&& token.len() > 2
195+
&& is_ident(&token[1..token.len() - 1])
196+
{
197+
err.help(&format!("try using `#[derive({})]`", &token[1..token.len() - 1]));
198+
} else {
199+
err.help("for example, write `#[derive(Debug)]` for `Debug`");
200+
}
201+
err.emit();
193202
None
194203
}
195204
NestedMetaItem::MetaItem(mi) => Some(mi),

src/test/ui/error-codes/E0777.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
#[derive("Clone")] //~ ERROR E0777
2+
#[derive("Clone
3+
")]
4+
//~^^ ERROR E0777
25
struct Foo;
36

47
fn main() {}

src/test/ui/error-codes/E0777.stderr

+11-1
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,18 @@ error[E0777]: expected path to a trait, found literal
44
LL | #[derive("Clone")]
55
| ^^^^^^^
66
|
7+
= help: try using `#[derive(Clone)]`
8+
9+
error[E0777]: expected path to a trait, found literal
10+
--> $DIR/E0777.rs:2:10
11+
|
12+
LL | #[derive("Clone
13+
| __________^
14+
LL | | ")]
15+
| |_^
16+
|
717
= help: for example, write `#[derive(Debug)]` for `Debug`
818

9-
error: aborting due to previous error
19+
error: aborting due to 2 previous errors
1020

1121
For more information about this error, try `rustc --explain E0777`.

0 commit comments

Comments
 (0)