Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[beta] Give a better error message for unknown derive messages #39444

Merged
merged 4 commits into from
Feb 6, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 15 additions & 13 deletions src/libsyntax_ext/deriving/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,12 +158,13 @@ pub fn expand_derive(cx: &mut ExtCtxt,
traits.retain(|titem| {
let tword = titem.word().unwrap();
let tname = tword.name();
let legacy_name = Symbol::intern(&format!("derive_{}", tname));

if is_builtin_trait(tname) || {
let derive_mode = ast::Path::from_ident(titem.span, ast::Ident::with_empty_ctxt(tname));
cx.resolver.resolve_macro(cx.current_expansion.mark, &derive_mode, false).map(|ext| {
if let SyntaxExtension::CustomDerive(_) = *ext { true } else { false }
}).unwrap_or(false)
// Skip macros that are builtin derives, and don't resolve as legacy derives.
if is_builtin_trait(tname) || !{
let derive_mode = ast::Path::from_ident(titem.span,
ast::Ident::with_empty_ctxt(legacy_name));
cx.resolver.resolve_macro(cx.current_expansion.mark, &derive_mode, false).is_ok()
} {
return true;
}
Expand All @@ -175,11 +176,10 @@ pub fn expand_derive(cx: &mut ExtCtxt,
feature_gate::GateIssue::Language,
feature_gate::EXPLAIN_CUSTOM_DERIVE);
} else {
let name = Symbol::intern(&format!("derive_{}", tname));
if !cx.resolver.is_whitelisted_legacy_custom_derive(name) {
if !cx.resolver.is_whitelisted_legacy_custom_derive(legacy_name) {
cx.span_warn(titem.span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);
}
let mitem = cx.meta_word(titem.span, name);
let mitem = cx.meta_word(titem.span, legacy_name);
new_attributes.push(cx.attribute(mitem.span, mitem));
}
false
Expand Down Expand Up @@ -222,7 +222,6 @@ pub fn expand_derive(cx: &mut ExtCtxt,
if let Some((i, titem)) = macros_11_derive {
let tname = ast::Ident::with_empty_ctxt(titem.name().unwrap());
let path = ast::Path::from_ident(titem.span, tname);
let ext = cx.resolver.resolve_macro(cx.current_expansion.mark, &path, false).unwrap();

traits.remove(i);
if traits.len() > 0 {
Expand All @@ -248,11 +247,14 @@ pub fn expand_derive(cx: &mut ExtCtxt,
..mitem.span
};

if let SyntaxExtension::CustomDerive(ref ext) = *ext {
return ext.expand(cx, span, &mitem, item);
} else {
unreachable!()
if let Ok(ext) = cx.resolver.resolve_macro(cx.current_expansion.mark, &path, false) {
if let SyntaxExtension::CustomDerive(ref ext) = *ext {
return ext.expand(cx, span, &mitem, item);
}
}

cx.span_err(span, &format!("cannot find derive macro `{}` in this scope", tname));
return vec![item];
}

// Ok, at this point we know that there are no old-style `#[derive_Foo]` nor
Expand Down
2 changes: 1 addition & 1 deletion src/test/compile-fail/deriving-meta-unknown-trait.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// ignore-tidy-linelength

#[derive(Eqr)]
//~^ ERROR `#[derive]` for custom traits is not stable enough for use. It is deprecated and will be removed in v1.15 (see issue #29644)
//~^ ERROR cannot find derive macro `Eqr` in this scope
struct Foo;

pub fn main() {}
2 changes: 1 addition & 1 deletion src/test/compile-fail/deriving-primitive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

#[derive(FromPrimitive)] //~ERROR `#[derive]` for custom traits is not stable
#[derive(FromPrimitive)] //~ERROR cannot find derive macro `FromPrimitive` in this scope
enum Foo {}

fn main() {}
Expand Down
51 changes: 0 additions & 51 deletions src/test/pretty/attr-variant-data.rs

This file was deleted.