From d1d94ba026eda45a2b998d68524c14929604c748 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Fri, 2 Oct 2020 13:52:03 +0200 Subject: [PATCH] Improve E0777 help message --- compiler/rustc_expand/src/proc_macro.rs | 17 +++++++++++++---- src/test/ui/error-codes/E0777.rs | 3 +++ src/test/ui/error-codes/E0777.stderr | 12 +++++++++++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_expand/src/proc_macro.rs b/compiler/rustc_expand/src/proc_macro.rs index 320d8fdef4d..4c95f19b96d 100644 --- a/compiler/rustc_expand/src/proc_macro.rs +++ b/compiler/rustc_expand/src/proc_macro.rs @@ -6,6 +6,7 @@ use rustc_ast::tokenstream::{TokenStream, TokenTree}; use rustc_ast::{self as ast, *}; use rustc_data_structures::sync::Lrc; use rustc_errors::{struct_span_err, Applicability, ErrorReported}; +use rustc_lexer::is_ident; use rustc_parse::nt_to_tokenstream; use rustc_span::symbol::sym; use rustc_span::{Span, DUMMY_SP}; @@ -182,14 +183,22 @@ crate fn collect_derives(cx: &mut ExtCtxt<'_>, attrs: &mut Vec) .filter_map(|nmi| match nmi { NestedMetaItem::Literal(lit) => { error_reported_filter_map = true; - struct_span_err!( + let mut err = struct_span_err!( cx.sess, lit.span, E0777, "expected path to a trait, found literal", - ) - .help("for example, write `#[derive(Debug)]` for `Debug`") - .emit(); + ); + let token = lit.token.to_string(); + 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 } NestedMetaItem::MetaItem(mi) => Some(mi), diff --git a/src/test/ui/error-codes/E0777.rs b/src/test/ui/error-codes/E0777.rs index ab633e4f535..ff70f736866 100644 --- a/src/test/ui/error-codes/E0777.rs +++ b/src/test/ui/error-codes/E0777.rs @@ -1,4 +1,7 @@ #[derive("Clone")] //~ ERROR E0777 +#[derive("Clone +")] +//~^^ ERROR E0777 struct Foo; fn main() {} diff --git a/src/test/ui/error-codes/E0777.stderr b/src/test/ui/error-codes/E0777.stderr index b356ea508f4..ea73c58993e 100644 --- a/src/test/ui/error-codes/E0777.stderr +++ b/src/test/ui/error-codes/E0777.stderr @@ -4,8 +4,18 @@ error[E0777]: expected path to a trait, found literal 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` -error: aborting due to previous error +error: aborting due to 2 previous errors For more information about this error, try `rustc --explain E0777`.