From c12fc66a9d643a6942d0bf4175d1a046e8d808de Mon Sep 17 00:00:00 2001 From: Jeffrey Seyfried Date: Thu, 22 Dec 2016 06:03:19 +0000 Subject: [PATCH] Allow legacy custom derive authors to disable warnings in downstream crates. --- src/librustc_driver/driver.rs | 2 ++ src/librustc_plugin/registry.rs | 18 ++++++++++++++++++ src/librustc_resolve/lib.rs | 2 ++ src/librustc_resolve/macros.rs | 4 ++++ src/libsyntax/ext/base.rs | 2 ++ src/libsyntax_ext/deriving/mod.rs | 4 +++- .../auxiliary/custom_derive_plugin.rs | 2 +- 7 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/librustc_driver/driver.rs b/src/librustc_driver/driver.rs index 360933c6b6695..8da9a23f4faad 100644 --- a/src/librustc_driver/driver.rs +++ b/src/librustc_driver/driver.rs @@ -592,6 +592,7 @@ pub fn phase_2_configure_and_expand(sess: &Session, } }); + let whitelisted_legacy_custom_derives = registry.take_whitelisted_custom_derives(); let Registry { syntax_exts, early_lint_passes, late_lint_passes, lint_groups, llvm_passes, attributes, mir_passes, .. } = registry; @@ -631,6 +632,7 @@ pub fn phase_2_configure_and_expand(sess: &Session, let resolver_arenas = Resolver::arenas(); let mut resolver = Resolver::new(sess, &krate, make_glob_map, &mut crate_loader, &resolver_arenas); + resolver.whitelisted_legacy_custom_derives = whitelisted_legacy_custom_derives; syntax_ext::register_builtins(&mut resolver, syntax_exts, sess.features.borrow().quote); krate = time(time_passes, "expansion", || { diff --git a/src/librustc_plugin/registry.rs b/src/librustc_plugin/registry.rs index fe2f9713d1beb..3700d0295e963 100644 --- a/src/librustc_plugin/registry.rs +++ b/src/librustc_plugin/registry.rs @@ -64,6 +64,8 @@ pub struct Registry<'a> { #[doc(hidden)] pub attributes: Vec<(String, AttributeType)>, + + whitelisted_custom_derives: Vec, } impl<'a> Registry<'a> { @@ -80,6 +82,7 @@ impl<'a> Registry<'a> { llvm_passes: vec![], attributes: vec![], mir_passes: Vec::new(), + whitelisted_custom_derives: Vec::new(), } } @@ -115,6 +118,21 @@ impl<'a> Registry<'a> { })); } + /// This can be used in place of `register_syntax_extension` to register legacy custom derives + /// (i.e. attribute syntax extensions whose name begins with `derive_`). Legacy custom + /// derives defined by this function do not trigger deprecation warnings when used. + #[unstable(feature = "rustc_private", issue = "27812")] + #[rustc_deprecated(since = "1.15.0", reason = "replaced by macros 1.1 (RFC 1861)")] + pub fn register_custom_derive(&mut self, name: ast::Name, extension: SyntaxExtension) { + assert!(name.as_str().starts_with("derive_")); + self.whitelisted_custom_derives.push(name); + self.register_syntax_extension(name, extension); + } + + pub fn take_whitelisted_custom_derives(&mut self) -> Vec { + ::std::mem::replace(&mut self.whitelisted_custom_derives, Vec::new()) + } + /// Register a macro of the usual kind. /// /// This is a convenience wrapper for `register_syntax_extension`. diff --git a/src/librustc_resolve/lib.rs b/src/librustc_resolve/lib.rs index f73227681c5e1..fa9e67bb40ae6 100644 --- a/src/librustc_resolve/lib.rs +++ b/src/librustc_resolve/lib.rs @@ -1111,6 +1111,7 @@ pub struct Resolver<'a> { lexical_macro_resolutions: Vec<(Name, &'a Cell>)>, macro_map: FxHashMap>, macro_exports: Vec, + pub whitelisted_legacy_custom_derives: Vec, // Maps the `Mark` of an expansion to its containing module or block. invocations: FxHashMap>, @@ -1292,6 +1293,7 @@ impl<'a> Resolver<'a> { macro_exports: Vec::new(), invocations: invocations, name_already_seen: FxHashMap(), + whitelisted_legacy_custom_derives: Vec::new(), } } diff --git a/src/librustc_resolve/macros.rs b/src/librustc_resolve/macros.rs index 204d1127fc48d..cdff84c8b8a59 100644 --- a/src/librustc_resolve/macros.rs +++ b/src/librustc_resolve/macros.rs @@ -123,6 +123,10 @@ impl<'a> base::Resolver for Resolver<'a> { EliminateCrateVar(self).fold_item(item).expect_one("") } + fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool { + self.whitelisted_legacy_custom_derives.contains(&name) + } + fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion) { let invocation = self.invocations[&mark]; self.collect_def_ids(invocation, expansion); diff --git a/src/libsyntax/ext/base.rs b/src/libsyntax/ext/base.rs index 8e63f73fdaa7a..68d261c64f83c 100644 --- a/src/libsyntax/ext/base.rs +++ b/src/libsyntax/ext/base.rs @@ -516,6 +516,7 @@ pub trait Resolver { fn next_node_id(&mut self) -> ast::NodeId; fn get_module_scope(&mut self, id: ast::NodeId) -> Mark; fn eliminate_crate_var(&mut self, item: P) -> P; + fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool; fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion); fn add_ext(&mut self, ident: ast::Ident, ext: Rc); @@ -539,6 +540,7 @@ impl Resolver for DummyResolver { fn next_node_id(&mut self) -> ast::NodeId { ast::DUMMY_NODE_ID } fn get_module_scope(&mut self, _id: ast::NodeId) -> Mark { Mark::root() } fn eliminate_crate_var(&mut self, item: P) -> P { item } + fn is_whitelisted_legacy_custom_derive(&self, _name: Name) -> bool { false } fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion) {} fn add_ext(&mut self, _ident: ast::Ident, _ext: Rc) {} diff --git a/src/libsyntax_ext/deriving/mod.rs b/src/libsyntax_ext/deriving/mod.rs index 535d7de19e341..0511b0d252b60 100644 --- a/src/libsyntax_ext/deriving/mod.rs +++ b/src/libsyntax_ext/deriving/mod.rs @@ -175,8 +175,10 @@ pub fn expand_derive(cx: &mut ExtCtxt, feature_gate::GateIssue::Language, feature_gate::EXPLAIN_CUSTOM_DERIVE); } else { - cx.span_warn(titem.span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE); let name = Symbol::intern(&format!("derive_{}", tname)); + if !cx.resolver.is_whitelisted_legacy_custom_derive(name) { + cx.span_warn(titem.span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE); + } let mitem = cx.meta_word(titem.span, name); new_attributes.push(cx.attribute(mitem.span, mitem)); } diff --git a/src/test/run-pass-fulldeps/auxiliary/custom_derive_plugin.rs b/src/test/run-pass-fulldeps/auxiliary/custom_derive_plugin.rs index 07f7d6bad7bf2..e46e4fb3766d3 100644 --- a/src/test/run-pass-fulldeps/auxiliary/custom_derive_plugin.rs +++ b/src/test/run-pass-fulldeps/auxiliary/custom_derive_plugin.rs @@ -31,7 +31,7 @@ use rustc_plugin::Registry; #[plugin_registrar] pub fn plugin_registrar(reg: &mut Registry) { - reg.register_syntax_extension( + reg.register_custom_derive( Symbol::intern("derive_TotalSum"), MultiDecorator(box expand)); }