Skip to content

Commit 762a037

Browse files
committed
Auto merge of #39444 - keeperofdakeys:derive-error, r=jseyfried
[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
2 parents 2b6bd50 + 6519e8b commit 762a037

File tree

4 files changed

+17
-66
lines changed

4 files changed

+17
-66
lines changed

src/libsyntax_ext/deriving/mod.rs

+15-13
Original file line numberDiff line numberDiff line change
@@ -158,12 +158,13 @@ pub fn expand_derive(cx: &mut ExtCtxt,
158158
traits.retain(|titem| {
159159
let tword = titem.word().unwrap();
160160
let tname = tword.name();
161+
let legacy_name = Symbol::intern(&format!("derive_{}", tname));
161162

162-
if is_builtin_trait(tname) || {
163-
let derive_mode = ast::Path::from_ident(titem.span, ast::Ident::with_empty_ctxt(tname));
164-
cx.resolver.resolve_macro(cx.current_expansion.mark, &derive_mode, false).map(|ext| {
165-
if let SyntaxExtension::CustomDerive(_) = *ext { true } else { false }
166-
}).unwrap_or(false)
163+
// Skip macros that are builtin derives, and don't resolve as legacy derives.
164+
if is_builtin_trait(tname) || !{
165+
let derive_mode = ast::Path::from_ident(titem.span,
166+
ast::Ident::with_empty_ctxt(legacy_name));
167+
cx.resolver.resolve_macro(cx.current_expansion.mark, &derive_mode, false).is_ok()
167168
} {
168169
return true;
169170
}
@@ -175,11 +176,10 @@ pub fn expand_derive(cx: &mut ExtCtxt,
175176
feature_gate::GateIssue::Language,
176177
feature_gate::EXPLAIN_CUSTOM_DERIVE);
177178
} else {
178-
let name = Symbol::intern(&format!("derive_{}", tname));
179-
if !cx.resolver.is_whitelisted_legacy_custom_derive(name) {
179+
if !cx.resolver.is_whitelisted_legacy_custom_derive(legacy_name) {
180180
cx.span_warn(titem.span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);
181181
}
182-
let mitem = cx.meta_word(titem.span, name);
182+
let mitem = cx.meta_word(titem.span, legacy_name);
183183
new_attributes.push(cx.attribute(mitem.span, mitem));
184184
}
185185
false
@@ -222,7 +222,6 @@ pub fn expand_derive(cx: &mut ExtCtxt,
222222
if let Some((i, titem)) = macros_11_derive {
223223
let tname = ast::Ident::with_empty_ctxt(titem.name().unwrap());
224224
let path = ast::Path::from_ident(titem.span, tname);
225-
let ext = cx.resolver.resolve_macro(cx.current_expansion.mark, &path, false).unwrap();
226225

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

251-
if let SyntaxExtension::CustomDerive(ref ext) = *ext {
252-
return ext.expand(cx, span, &mitem, item);
253-
} else {
254-
unreachable!()
250+
if let Ok(ext) = cx.resolver.resolve_macro(cx.current_expansion.mark, &path, false) {
251+
if let SyntaxExtension::CustomDerive(ref ext) = *ext {
252+
return ext.expand(cx, span, &mitem, item);
253+
}
255254
}
255+
256+
cx.span_err(span, &format!("cannot find derive macro `{}` in this scope", tname));
257+
return vec![item];
256258
}
257259

258260
// Ok, at this point we know that there are no old-style `#[derive_Foo]` nor

src/test/compile-fail/deriving-meta-unknown-trait.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
// ignore-tidy-linelength
1212

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

1717
pub fn main() {}

src/test/compile-fail/deriving-primitive.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

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

1414
fn main() {}

src/test/pretty/attr-variant-data.rs

-51
This file was deleted.

0 commit comments

Comments
 (0)