Skip to content

Commit 467a7f0

Browse files
committed
Auto merge of #38533 - jseyfried:legacy_custom_derive_deprecation, r=nrc
Allow legacy custom derive authors to disable warnings in downstream crates This PR allows legacy custom derive authors to use a pre-deprecated method `registry.register_custom_derive()` instead of `registry.register_syntax_extension()` to avoid downstream deprecation warnings. r? @nrc
2 parents 00b4019 + c12fc66 commit 467a7f0

File tree

7 files changed

+32
-2
lines changed

7 files changed

+32
-2
lines changed

src/librustc_driver/driver.rs

+2
Original file line numberDiff line numberDiff line change
@@ -592,6 +592,7 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
592592
}
593593
});
594594

595+
let whitelisted_legacy_custom_derives = registry.take_whitelisted_custom_derives();
595596
let Registry { syntax_exts, early_lint_passes, late_lint_passes, lint_groups,
596597
llvm_passes, attributes, mir_passes, .. } = registry;
597598

@@ -631,6 +632,7 @@ pub fn phase_2_configure_and_expand<F>(sess: &Session,
631632
let resolver_arenas = Resolver::arenas();
632633
let mut resolver =
633634
Resolver::new(sess, &krate, make_glob_map, &mut crate_loader, &resolver_arenas);
635+
resolver.whitelisted_legacy_custom_derives = whitelisted_legacy_custom_derives;
634636
syntax_ext::register_builtins(&mut resolver, syntax_exts, sess.features.borrow().quote);
635637

636638
krate = time(time_passes, "expansion", || {

src/librustc_plugin/registry.rs

+18
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ pub struct Registry<'a> {
6464

6565
#[doc(hidden)]
6666
pub attributes: Vec<(String, AttributeType)>,
67+
68+
whitelisted_custom_derives: Vec<ast::Name>,
6769
}
6870

6971
impl<'a> Registry<'a> {
@@ -80,6 +82,7 @@ impl<'a> Registry<'a> {
8082
llvm_passes: vec![],
8183
attributes: vec![],
8284
mir_passes: Vec::new(),
85+
whitelisted_custom_derives: Vec::new(),
8386
}
8487
}
8588

@@ -115,6 +118,21 @@ impl<'a> Registry<'a> {
115118
}));
116119
}
117120

121+
/// This can be used in place of `register_syntax_extension` to register legacy custom derives
122+
/// (i.e. attribute syntax extensions whose name begins with `derive_`). Legacy custom
123+
/// derives defined by this function do not trigger deprecation warnings when used.
124+
#[unstable(feature = "rustc_private", issue = "27812")]
125+
#[rustc_deprecated(since = "1.15.0", reason = "replaced by macros 1.1 (RFC 1861)")]
126+
pub fn register_custom_derive(&mut self, name: ast::Name, extension: SyntaxExtension) {
127+
assert!(name.as_str().starts_with("derive_"));
128+
self.whitelisted_custom_derives.push(name);
129+
self.register_syntax_extension(name, extension);
130+
}
131+
132+
pub fn take_whitelisted_custom_derives(&mut self) -> Vec<ast::Name> {
133+
::std::mem::replace(&mut self.whitelisted_custom_derives, Vec::new())
134+
}
135+
118136
/// Register a macro of the usual kind.
119137
///
120138
/// This is a convenience wrapper for `register_syntax_extension`.

src/librustc_resolve/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1104,6 +1104,7 @@ pub struct Resolver<'a> {
11041104
lexical_macro_resolutions: Vec<(Name, &'a Cell<LegacyScope<'a>>)>,
11051105
macro_map: FxHashMap<DefId, Rc<SyntaxExtension>>,
11061106
macro_exports: Vec<Export>,
1107+
pub whitelisted_legacy_custom_derives: Vec<Name>,
11071108

11081109
// Maps the `Mark` of an expansion to its containing module or block.
11091110
invocations: FxHashMap<Mark, &'a InvocationData<'a>>,
@@ -1284,6 +1285,7 @@ impl<'a> Resolver<'a> {
12841285
macro_exports: Vec::new(),
12851286
invocations: invocations,
12861287
name_already_seen: FxHashMap(),
1288+
whitelisted_legacy_custom_derives: Vec::new(),
12871289
}
12881290
}
12891291

src/librustc_resolve/macros.rs

+4
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,10 @@ impl<'a> base::Resolver for Resolver<'a> {
122122
EliminateCrateVar(self).fold_item(item).expect_one("")
123123
}
124124

125+
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool {
126+
self.whitelisted_legacy_custom_derives.contains(&name)
127+
}
128+
125129
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion) {
126130
let invocation = self.invocations[&mark];
127131
self.collect_def_ids(invocation, expansion);

src/libsyntax/ext/base.rs

+2
Original file line numberDiff line numberDiff line change
@@ -516,6 +516,7 @@ pub trait Resolver {
516516
fn next_node_id(&mut self) -> ast::NodeId;
517517
fn get_module_scope(&mut self, id: ast::NodeId) -> Mark;
518518
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item>;
519+
fn is_whitelisted_legacy_custom_derive(&self, name: Name) -> bool;
519520

520521
fn visit_expansion(&mut self, mark: Mark, expansion: &Expansion);
521522
fn add_ext(&mut self, ident: ast::Ident, ext: Rc<SyntaxExtension>);
@@ -539,6 +540,7 @@ impl Resolver for DummyResolver {
539540
fn next_node_id(&mut self) -> ast::NodeId { ast::DUMMY_NODE_ID }
540541
fn get_module_scope(&mut self, _id: ast::NodeId) -> Mark { Mark::root() }
541542
fn eliminate_crate_var(&mut self, item: P<ast::Item>) -> P<ast::Item> { item }
543+
fn is_whitelisted_legacy_custom_derive(&self, _name: Name) -> bool { false }
542544

543545
fn visit_expansion(&mut self, _invoc: Mark, _expansion: &Expansion) {}
544546
fn add_ext(&mut self, _ident: ast::Ident, _ext: Rc<SyntaxExtension>) {}

src/libsyntax_ext/deriving/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,10 @@ pub fn expand_derive(cx: &mut ExtCtxt,
175175
feature_gate::GateIssue::Language,
176176
feature_gate::EXPLAIN_CUSTOM_DERIVE);
177177
} else {
178-
cx.span_warn(titem.span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);
179178
let name = Symbol::intern(&format!("derive_{}", tname));
179+
if !cx.resolver.is_whitelisted_legacy_custom_derive(name) {
180+
cx.span_warn(titem.span, feature_gate::EXPLAIN_DEPR_CUSTOM_DERIVE);
181+
}
180182
let mitem = cx.meta_word(titem.span, name);
181183
new_attributes.push(cx.attribute(mitem.span, mitem));
182184
}

src/test/run-pass-fulldeps/auxiliary/custom_derive_plugin.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use rustc_plugin::Registry;
3131

3232
#[plugin_registrar]
3333
pub fn plugin_registrar(reg: &mut Registry) {
34-
reg.register_syntax_extension(
34+
reg.register_custom_derive(
3535
Symbol::intern("derive_TotalSum"),
3636
MultiDecorator(box expand));
3737
}

0 commit comments

Comments
 (0)