Skip to content

Commit

Permalink
Auto merge of #39444 - keeperofdakeys:derive-error, r=jseyfried
Browse files Browse the repository at this point in the history
[beta] Give a better error message for unknown derive messages

This PR improves the error message for unknown derive macros.

Currently unknown derives give the following error, which is very misleading:
```
`#[derive]` for custom traits is not stable enough for use. It is deprecated and will be removed in v1.15 (see issue #29644)
```

I'm currently working on a PR that will change this (#39442) to the following:
```
cannot find derive macro `Foo` in this scope
```

This PR backports the (as yet unmerged) error message to beta/1.16 (it's a pity that this is probably too late for 1.15).

r? @jseyfried
  • Loading branch information
bors committed Feb 6, 2017
2 parents 2b6bd50 + 6519e8b commit 762a037
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 66 deletions.
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.

0 comments on commit 762a037

Please sign in to comment.