Skip to content

Commit

Permalink
Allow legacy custom derive authors to disable warnings in downstream …
Browse files Browse the repository at this point in the history
…crates.
  • Loading branch information
jseyfried committed Dec 23, 2016
1 parent 164619a commit c12fc66
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/librustc_driver/driver.rs
Expand Up @@ -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;

Expand Down Expand Up @@ -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", || {
Expand Down
18 changes: 18 additions & 0 deletions src/librustc_plugin/registry.rs
Expand Up @@ -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> {
Expand All @@ -80,6 +82,7 @@ impl<'a> Registry<'a> {
llvm_passes: vec![],
attributes: vec![],
mir_passes: Vec::new(),
whitelisted_custom_derives: Vec::new(),
}
}

Expand Down Expand Up @@ -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<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`.
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_resolve/lib.rs
Expand Up @@ -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>>,
Expand Down Expand Up @@ -1292,6 +1293,7 @@ impl<'a> Resolver<'a> {
macro_exports: Vec::new(),
invocations: invocations,
name_already_seen: FxHashMap(),
whitelisted_legacy_custom_derives: Vec::new(),
}
}

Expand Down
4 changes: 4 additions & 0 deletions src/librustc_resolve/macros.rs
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions src/libsyntax/ext/base.rs
Expand Up @@ -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>);
Expand All @@ -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>) {}
Expand Down
4 changes: 3 additions & 1 deletion src/libsyntax_ext/deriving/mod.rs
Expand Up @@ -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));
}
Expand Down
Expand Up @@ -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));
}
Expand Down

0 comments on commit c12fc66

Please sign in to comment.