Skip to content

Commit

Permalink
syntax: Use MultiItemModifier for built-in derives
Browse files Browse the repository at this point in the history
  • Loading branch information
petrochenkov committed Jun 6, 2019
1 parent 4419af8 commit cc17dbb
Show file tree
Hide file tree
Showing 15 changed files with 82 additions and 85 deletions.
7 changes: 2 additions & 5 deletions src/libsyntax/ext/base.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub use SyntaxExtension::*;

use crate::ast::{self, Attribute, Name, PatKind, MetaItem};
use crate::ast::{self, Attribute, Name, PatKind};
use crate::attr::HasAttrs;
use crate::source_map::{SourceMap, Spanned, respan};
use crate::edition::Edition;
Expand Down Expand Up @@ -516,9 +516,6 @@ impl MacResult for DummyResult {
}
}

pub type BuiltinDeriveFn =
for<'cx> fn(&'cx mut ExtCtxt<'_>, Span, &MetaItem, &Annotatable, &mut dyn FnMut(Annotatable));

/// Represents different kinds of macro invocations that can be resolved.
#[derive(Clone, Copy, PartialEq, Eq, RustcEncodable, RustcDecodable, Hash, Debug)]
pub enum MacroKind {
Expand Down Expand Up @@ -604,7 +601,7 @@ pub enum SyntaxExtension {
Vec<Symbol> /* inert attribute names */, Edition),

/// An attribute-like procedural macro that derives a builtin trait.
BuiltinDerive(BuiltinDeriveFn),
BuiltinDerive(Box<dyn MultiItemModifier + sync::Sync + sync::Send>),

/// A declarative macro, e.g., `macro m() {}`.
DeclMacro {
Expand Down
40 changes: 20 additions & 20 deletions src/libsyntax/ext/expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -893,29 +893,29 @@ impl<'a, 'b> MacroExpander<'a, 'b> {
edition: ext.edition(self.cx.parse_sess.edition),
};

match *ext {
ProcMacroDerive(ref ext, ..) => {
invoc.expansion_data.mark.set_expn_info(expn_info);
let span = span.with_ctxt(self.cx.backtrace());
let dummy = ast::MetaItem { // FIXME(jseyfried) avoid this
path: Path::from_ident(Ident::invalid()),
span: DUMMY_SP,
node: ast::MetaItemKind::Word,
match ext {
ProcMacroDerive(expander, ..) | BuiltinDerive(expander) => {
let meta = match ext {
ProcMacroDerive(..) => ast::MetaItem { // FIXME(jseyfried) avoid this
path: Path::from_ident(Ident::invalid()),
span: DUMMY_SP,
node: ast::MetaItemKind::Word,
},
_ => {
expn_info.allow_internal_unstable = Some(vec![
sym::rustc_attrs,
Symbol::intern("derive_clone_copy"),
Symbol::intern("derive_eq"),
// RustcDeserialize and RustcSerialize
Symbol::intern("libstd_sys_internals"),
].into());
attr.meta()?
}
};
let items = ext.expand(self.cx, span, &dummy, item);
Some(invoc.fragment_kind.expect_from_annotatables(items))
}
BuiltinDerive(func) => {
expn_info.allow_internal_unstable = Some(vec![
sym::rustc_attrs,
Symbol::intern("derive_clone_copy"),
Symbol::intern("derive_eq"),
Symbol::intern("libstd_sys_internals"), // RustcDeserialize and RustcSerialize
].into());

invoc.expansion_data.mark.set_expn_info(expn_info);
let span = span.with_ctxt(self.cx.backtrace());
let mut items = Vec::new();
func(self.cx, span, &attr.meta()?, &item, &mut |a| items.push(a));
let items = expander.expand(self.cx, span, &meta, item);
Some(invoc.fragment_kind.expect_from_annotatables(items))
}
_ => {
Expand Down
11 changes: 6 additions & 5 deletions src/libsyntax_ext/deriving/bounds.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@ use syntax_pos::Span;
pub fn expand_deriving_unsafe_bound(cx: &mut ExtCtxt<'_>,
span: Span,
_: &MetaItem,
_: &Annotatable,
_: &mut dyn FnMut(Annotatable)) {
_: Annotatable)
-> Vec<Annotatable> {
cx.span_err(span, "this unsafe trait should be implemented explicitly");
Vec::new()
}

pub fn expand_deriving_copy(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
ref item: Annotatable)
-> Vec<Annotatable> {
let trait_def = TraitDef {
span,
attributes: Vec::new(),
Expand All @@ -31,5 +32,5 @@ pub fn expand_deriving_copy(cx: &mut ExtCtxt<'_>,
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push);
trait_def.expand(cx, mitem, item)
}
6 changes: 3 additions & 3 deletions src/libsyntax_ext/deriving/clone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use syntax_pos::Span;
pub fn expand_deriving_clone(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
ref item: Annotatable)
-> Vec<Annotatable> {
// check if we can use a short form
//
// the short form is `fn clone(&self) -> Self { *self }`
Expand Down Expand Up @@ -100,7 +100,7 @@ pub fn expand_deriving_clone(cx: &mut ExtCtxt<'_>,
associated_types: Vec::new(),
};

trait_def.expand_ext(cx, mitem, item, push, is_shallow)
trait_def.expand_ext(cx, mitem, item, is_shallow)
}

fn cs_clone_shallow(name: &str,
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax_ext/deriving/cmp/eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use syntax_pos::Span;
pub fn expand_deriving_eq(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
ref item: Annotatable)
-> Vec<Annotatable> {
let inline = cx.meta_word(span, sym::inline);
let hidden = cx.meta_list_item_word(span, sym::hidden);
let doc = cx.meta_list(span, sym::doc, vec![hidden]);
Expand Down Expand Up @@ -41,7 +41,7 @@ pub fn expand_deriving_eq(cx: &mut ExtCtxt<'_>,
}],
associated_types: Vec::new(),
};
trait_def.expand_ext(cx, mitem, item, push, true)
trait_def.expand_ext(cx, mitem, item, true)
}

fn cs_total_eq_assert(cx: &mut ExtCtxt<'_>,
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax_ext/deriving/cmp/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use syntax_pos::Span;
pub fn expand_deriving_ord(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
ref item: Annotatable)
-> Vec<Annotatable> {
let inline = cx.meta_word(span, sym::inline);
let attrs = vec![cx.attribute(span, inline)];
let trait_def = TraitDef {
Expand All @@ -40,7 +40,7 @@ pub fn expand_deriving_ord(cx: &mut ExtCtxt<'_>,
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, item)
}


Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax_ext/deriving/cmp/partial_eq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use syntax_pos::Span;
pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
ref item: Annotatable)
-> Vec<Annotatable> {
// structures are equal if all fields are equal, and non equal, if
// any fields are not equal or if the enum variants are different
fn cs_op(cx: &mut ExtCtxt<'_>,
Expand Down Expand Up @@ -99,5 +99,5 @@ pub fn expand_deriving_partial_eq(cx: &mut ExtCtxt<'_>,
methods,
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, item)
}
6 changes: 3 additions & 3 deletions src/libsyntax_ext/deriving/cmp/partial_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use syntax_pos::Span;
pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
ref item: Annotatable)
-> Vec<Annotatable> {
macro_rules! md {
($name:expr, $op:expr, $equal:expr) => { {
let inline = cx.meta_word(span, sym::inline);
Expand Down Expand Up @@ -83,7 +83,7 @@ pub fn expand_deriving_partial_ord(cx: &mut ExtCtxt<'_>,
methods,
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, item)
}

#[derive(Copy, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax_ext/deriving/debug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ use syntax_pos::{DUMMY_SP, Span};
pub fn expand_deriving_debug(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
ref item: Annotatable)
-> Vec<Annotatable> {
// &mut ::std::fmt::Formatter
let fmtr = Ptr(Box::new(Literal(path_std!(cx, fmt::Formatter))),
Borrowed(None, ast::Mutability::Mutable));
Expand Down Expand Up @@ -44,7 +44,7 @@ pub fn expand_deriving_debug(cx: &mut ExtCtxt<'_>,
}],
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, item)
}

/// We use the debug builders to do the heavy lifting here
Expand Down
20 changes: 10 additions & 10 deletions src/libsyntax_ext/deriving/decodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,26 @@ use syntax_pos::Span;
pub fn expand_deriving_rustc_decodable(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
expand_deriving_decodable_imp(cx, span, mitem, item, push, "rustc_serialize")
item: Annotatable)
-> Vec<Annotatable> {
expand_deriving_decodable_imp(cx, span, mitem, item, "rustc_serialize")
}

pub fn expand_deriving_decodable(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
item: Annotatable)
-> Vec<Annotatable> {
warn_if_deprecated(cx, span, "Decodable");
expand_deriving_decodable_imp(cx, span, mitem, item, push, "serialize")
expand_deriving_decodable_imp(cx, span, mitem, item, "serialize")
}

fn expand_deriving_decodable_imp(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable),
krate: &'static str) {
ref item: Annotatable,
krate: &'static str)
-> Vec<Annotatable> {
let typaram = &*deriving::hygienic_type_parameter(item, "__D");

let trait_def = TraitDef {
Expand Down Expand Up @@ -76,7 +76,7 @@ fn expand_deriving_decodable_imp(cx: &mut ExtCtxt<'_>,
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, item)
}

fn decodable_substructure(cx: &mut ExtCtxt<'_>,
Expand Down
6 changes: 3 additions & 3 deletions src/libsyntax_ext/deriving/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ use syntax_pos::Span;
pub fn expand_deriving_default(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
ref item: Annotatable)
-> Vec<Annotatable> {
let inline = cx.meta_word(span, sym::inline);
let attrs = vec![cx.attribute(span, inline)];
let trait_def = TraitDef {
Expand All @@ -40,7 +40,7 @@ pub fn expand_deriving_default(cx: &mut ExtCtxt<'_>,
}],
associated_types: Vec::new(),
};
trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, item)
}

fn default_substructure(cx: &mut ExtCtxt<'_>,
Expand Down
20 changes: 10 additions & 10 deletions src/libsyntax_ext/deriving/encodable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,26 +97,26 @@ use syntax_pos::Span;
pub fn expand_deriving_rustc_encodable(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
expand_deriving_encodable_imp(cx, span, mitem, item, push, "rustc_serialize")
item: Annotatable)
-> Vec<Annotatable> {
expand_deriving_encodable_imp(cx, span, mitem, item, "rustc_serialize")
}

pub fn expand_deriving_encodable(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable)) {
item: Annotatable)
-> Vec<Annotatable> {
warn_if_deprecated(cx, span, "Encodable");
expand_deriving_encodable_imp(cx, span, mitem, item, push, "serialize")
expand_deriving_encodable_imp(cx, span, mitem, item, "serialize")
}

fn expand_deriving_encodable_imp(cx: &mut ExtCtxt<'_>,
span: Span,
mitem: &MetaItem,
item: &Annotatable,
push: &mut dyn FnMut(Annotatable),
krate: &'static str) {
ref item: Annotatable,
krate: &'static str)
-> Vec<Annotatable> {
let typaram = &*deriving::hygienic_type_parameter(item, "__S");

let trait_def = TraitDef {
Expand Down Expand Up @@ -159,7 +159,7 @@ fn expand_deriving_encodable_imp(cx: &mut ExtCtxt<'_>,
associated_types: Vec::new(),
};

trait_def.expand(cx, mitem, item, push)
trait_def.expand(cx, mitem, item)
}

fn encodable_substructure(cx: &mut ExtCtxt<'_>,
Expand Down
18 changes: 9 additions & 9 deletions src/libsyntax_ext/deriving/generic/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,17 +388,17 @@ impl<'a> TraitDef<'a> {
pub fn expand(self,
cx: &mut ExtCtxt<'_>,
mitem: &ast::MetaItem,
item: &'a Annotatable,
push: &mut dyn FnMut(Annotatable)) {
self.expand_ext(cx, mitem, item, push, false);
item: &'a Annotatable)
-> Vec<Annotatable> {
self.expand_ext(cx, mitem, item, false)
}

pub fn expand_ext(self,
cx: &mut ExtCtxt<'_>,
mitem: &ast::MetaItem,
item: &'a Annotatable,
push: &mut dyn FnMut(Annotatable),
from_scratch: bool) {
from_scratch: bool)
-> Vec<Annotatable> {
match *item {
Annotatable::Item(ref item) => {
let is_packed = item.attrs.iter().any(|attr| {
Expand All @@ -422,7 +422,7 @@ impl<'a> TraitDef<'a> {
// Non-ADT derive is an error, but it should have been
// set earlier; see
// libsyntax/ext/expand.rs:MacroExpander::expand()
return;
return Vec::new();
}
};
let is_always_copy =
Expand Down Expand Up @@ -453,7 +453,7 @@ impl<'a> TraitDef<'a> {
} else {
cx.span_err(mitem.span,
"this trait cannot be derived for unions");
return;
return Vec::new();
}
}
_ => unreachable!(),
Expand All @@ -468,13 +468,13 @@ impl<'a> TraitDef<'a> {
.contains(&a.name_or_empty())
})
.cloned());
push(Annotatable::Item(P(ast::Item { attrs: attrs, ..(*newitem).clone() })))
vec![Annotatable::Item(P(ast::Item { attrs: attrs, ..(*newitem).clone() }))]
}
_ => {
// Non-Item derive is an error, but it should have been
// set earlier; see
// libsyntax/ext/expand.rs:MacroExpander::expand()
return;
Vec::new()
}
}
}
Expand Down
Loading

0 comments on commit cc17dbb

Please sign in to comment.