Allow legacy custom derive authors to disable warnings in downstream crates.

This commit is contained in:
Jeffrey Seyfried 2016-12-22 06:03:19 +00:00
parent 164619a8cf
commit c12fc66a9d
7 changed files with 32 additions and 2 deletions

View File

@ -592,6 +592,7 @@ pub fn phase_2_configure_and_expand<F>(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<F>(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", || {

View File

@ -64,6 +64,8 @@ pub struct Registry<'a> {
#[doc(hidden)]
pub attributes: Vec<(String, AttributeType)>,
whitelisted_custom_derives: Vec<ast::Name>,
}
impl<'a> Registry<'a> {
@ -80,6 +82,7 @@ pub fn new(sess: &'a Session, krate_span: Span) -> Registry<'a> {
llvm_passes: vec![],
attributes: vec![],
mir_passes: Vec::new(),
whitelisted_custom_derives: Vec::new(),
}
}
@ -115,6 +118,21 @@ pub fn register_syntax_extension(&mut self, name: ast::Name, extension: SyntaxEx
}));
}
/// 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<ast::Name> {
::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`.

View File

@ -1111,6 +1111,7 @@ pub struct Resolver<'a> {
lexical_macro_resolutions: Vec<(Name, &'a Cell<LegacyScope<'a>>)>,
macro_map: FxHashMap<DefId, Rc<SyntaxExtension>>,
macro_exports: Vec<Export>,
pub whitelisted_legacy_custom_derives: Vec<Name>,
// Maps the `Mark` of an expansion to its containing module or block.
invocations: FxHashMap<Mark, &'a InvocationData<'a>>,
@ -1292,6 +1293,7 @@ pub fn new(session: &'a Session,
macro_exports: Vec::new(),
invocations: invocations,
name_already_seen: FxHashMap(),
whitelisted_legacy_custom_derives: Vec::new(),
}
}

View File

@ -123,6 +123,10 @@ fn fold_path(&mut self, mut path: ast::Path) -> ast::Path {
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);

View File

@ -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<ast::Item>) -> P<ast::Item>;
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<SyntaxExtension>);
@ -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<ast::Item>) -> P<ast::Item> { 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<SyntaxExtension>) {}

View File

@ -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));
}

View File

@ -31,7 +31,7 @@
#[plugin_registrar]
pub fn plugin_registrar(reg: &mut Registry) {
reg.register_syntax_extension(
reg.register_custom_derive(
Symbol::intern("derive_TotalSum"),
MultiDecorator(box expand));
}