2014-12-31 04:25:18 +00:00
|
|
|
//! The compiler code necessary to implement the `#[derive]` extensions.
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2019-10-17 08:54:37 +00:00
|
|
|
use syntax::ast::{self, ItemKind, MetaItem};
|
2019-10-16 08:59:30 +00:00
|
|
|
use syntax_expand::base::{Annotatable, ExtCtxt, MultiItemModifier};
|
2016-03-10 05:31:19 +00:00
|
|
|
use syntax::ptr::P;
|
2019-05-07 06:03:44 +00:00
|
|
|
use syntax::symbol::{Symbol, sym};
|
2016-06-21 22:08:13 +00:00
|
|
|
use syntax_pos::Span;
|
2014-05-16 07:16:13 +00:00
|
|
|
|
2017-12-06 18:50:55 +00:00
|
|
|
macro path_local($x:ident) {
|
|
|
|
generic::ty::Path::new_local(stringify!($x))
|
2014-09-07 20:58:41 +00:00
|
|
|
}
|
|
|
|
|
2017-12-06 18:50:55 +00:00
|
|
|
macro pathvec_std($cx:expr, $($rest:ident)::+) {{
|
|
|
|
vec![ $( stringify!($rest) ),+ ]
|
|
|
|
}}
|
2014-09-07 21:57:26 +00:00
|
|
|
|
2017-12-06 18:50:55 +00:00
|
|
|
macro path_std($($x:tt)*) {
|
|
|
|
generic::ty::Path::new( pathvec_std!( $($x)* ) )
|
2014-09-07 21:57:26 +00:00
|
|
|
}
|
|
|
|
|
2014-04-30 09:26:50 +00:00
|
|
|
pub mod bounds;
|
2013-03-28 10:50:10 +00:00
|
|
|
pub mod clone;
|
2013-04-10 23:31:51 +00:00
|
|
|
pub mod encodable;
|
2013-04-09 01:53:39 +00:00
|
|
|
pub mod decodable;
|
2014-02-22 05:33:23 +00:00
|
|
|
pub mod hash;
|
2015-10-18 16:03:42 +00:00
|
|
|
pub mod debug;
|
2013-09-12 04:51:13 +00:00
|
|
|
pub mod default;
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2015-04-02 16:14:53 +00:00
|
|
|
#[path="cmp/partial_eq.rs"]
|
|
|
|
pub mod partial_eq;
|
2013-03-30 14:58:05 +00:00
|
|
|
#[path="cmp/eq.rs"]
|
|
|
|
pub mod eq;
|
2015-04-02 16:14:53 +00:00
|
|
|
#[path="cmp/partial_ord.rs"]
|
|
|
|
pub mod partial_ord;
|
2013-03-30 14:58:05 +00:00
|
|
|
#[path="cmp/ord.rs"]
|
|
|
|
pub mod ord;
|
|
|
|
|
2013-03-28 10:50:10 +00:00
|
|
|
pub mod generic;
|
|
|
|
|
2019-06-20 08:52:31 +00:00
|
|
|
crate struct BuiltinDerive(
|
|
|
|
crate fn(&mut ExtCtxt<'_>, Span, &MetaItem, &Annotatable, &mut dyn FnMut(Annotatable))
|
2019-06-06 20:21:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
impl MultiItemModifier for BuiltinDerive {
|
|
|
|
fn expand(&self,
|
|
|
|
ecx: &mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
meta_item: &MetaItem,
|
|
|
|
item: Annotatable)
|
|
|
|
-> Vec<Annotatable> {
|
2019-08-21 18:28:22 +00:00
|
|
|
// FIXME: Built-in derives often forget to give spans contexts,
|
|
|
|
// so we are doing it here in a centralized way.
|
|
|
|
let span = ecx.with_def_site_ctxt(span);
|
2019-06-06 20:21:44 +00:00
|
|
|
let mut items = Vec::new();
|
|
|
|
(self.0)(ecx, span, meta_item, &item, &mut |a| items.push(a));
|
|
|
|
items
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 05:31:19 +00:00
|
|
|
/// Constructs an expression that calls an intrinsic
|
2019-02-04 12:49:54 +00:00
|
|
|
fn call_intrinsic(cx: &ExtCtxt<'_>,
|
2019-06-16 21:31:46 +00:00
|
|
|
span: Span,
|
2016-03-10 05:31:19 +00:00
|
|
|
intrinsic: &str,
|
2016-07-19 17:32:06 +00:00
|
|
|
args: Vec<P<ast::Expr>>)
|
|
|
|
-> P<ast::Expr> {
|
2019-08-21 18:28:22 +00:00
|
|
|
let span = cx.with_def_site_ctxt(span);
|
2019-05-22 04:41:15 +00:00
|
|
|
let path = cx.std_path(&[sym::intrinsics, Symbol::intern(intrinsic)]);
|
2016-03-10 05:31:19 +00:00
|
|
|
let call = cx.expr_call_global(span, path, args);
|
|
|
|
|
|
|
|
cx.expr_block(P(ast::Block {
|
2016-06-23 09:51:18 +00:00
|
|
|
stmts: vec![cx.stmt_expr(call)],
|
2016-03-10 05:31:19 +00:00
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
rules: ast::BlockCheckMode::Unsafe(ast::CompilerGenerated),
|
2017-08-07 05:54:09 +00:00
|
|
|
span,
|
2016-07-19 17:32:06 +00:00
|
|
|
}))
|
2016-03-10 05:31:19 +00:00
|
|
|
}
|
2019-10-17 08:54:37 +00:00
|
|
|
|
|
|
|
|
|
|
|
// Injects `impl<...> Structural for ItemType<...> { }`. In particular,
|
|
|
|
// does *not* add `where T: Structural` for parameters `T` in `...`.
|
|
|
|
// (That's the main reason we cannot use TraitDef here.)
|
|
|
|
fn inject_impl_of_structural_trait(cx: &mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
item: &Annotatable,
|
|
|
|
structural_path: generic::ty::Path<'_>,
|
|
|
|
push: &mut dyn FnMut(Annotatable)) {
|
|
|
|
let item = match *item {
|
|
|
|
Annotatable::Item(ref item) => item,
|
|
|
|
_ => {
|
|
|
|
// Non-Item derive is an error, but it should have been
|
|
|
|
// set earlier; see
|
|
|
|
// libsyntax/ext/expand.rs:MacroExpander::expand()
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let generics = match item.kind {
|
|
|
|
ItemKind::Struct(_, ref generics) |
|
|
|
|
ItemKind::Enum(_, ref generics) => generics,
|
|
|
|
// Do not inject `impl Structural for Union`. (`PartialEq` does not
|
|
|
|
// support unions, so we will see error downstream.)
|
|
|
|
ItemKind::Union(..) => return,
|
|
|
|
_ => unreachable!(),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Create generics param list for where clauses and impl headers
|
|
|
|
let mut generics = generics.clone();
|
|
|
|
|
|
|
|
// Create the type of `self`.
|
|
|
|
//
|
|
|
|
// in addition, remove defaults from type params (impls cannot have them).
|
|
|
|
let self_params: Vec<_> = generics.params.iter_mut().map(|param| match &mut param.kind {
|
|
|
|
ast::GenericParamKind::Lifetime => {
|
|
|
|
ast::GenericArg::Lifetime(cx.lifetime(span, param.ident))
|
|
|
|
}
|
|
|
|
ast::GenericParamKind::Type { default } => {
|
|
|
|
*default = None;
|
|
|
|
ast::GenericArg::Type(cx.ty_ident(span, param.ident))
|
|
|
|
}
|
|
|
|
ast::GenericParamKind::Const { ty: _ } => {
|
|
|
|
ast::GenericArg::Const(cx.const_ident(span, param.ident))
|
|
|
|
}
|
|
|
|
}).collect();
|
|
|
|
|
|
|
|
let type_ident = item.ident;
|
|
|
|
|
|
|
|
let trait_ref = cx.trait_ref(structural_path.to_path(cx, span, type_ident, &generics));
|
|
|
|
let self_type = cx.ty_path(cx.path_all(span, false, vec![type_ident], self_params));
|
|
|
|
|
|
|
|
// It would be nice to also encode constraint `where Self: Eq` (by adding it
|
|
|
|
// onto `generics` cloned above). Unfortunately, that strategy runs afoul of
|
|
|
|
// rust-lang/rust#48214. So we perform that additional check in the compiler
|
|
|
|
// itself, instead of encoding it here.
|
|
|
|
|
|
|
|
// Keep the lint and stability attributes of the original item, to control
|
|
|
|
// how the generated implementation is linted.
|
|
|
|
let mut attrs = Vec::new();
|
|
|
|
attrs.extend(item.attrs
|
|
|
|
.iter()
|
|
|
|
.filter(|a| {
|
|
|
|
[sym::allow, sym::warn, sym::deny, sym::forbid, sym::stable, sym::unstable]
|
|
|
|
.contains(&a.name_or_empty())
|
|
|
|
})
|
|
|
|
.cloned());
|
|
|
|
|
|
|
|
let newitem = cx.item(span,
|
|
|
|
ast::Ident::invalid(),
|
|
|
|
attrs,
|
|
|
|
ItemKind::Impl(ast::Unsafety::Normal,
|
|
|
|
ast::ImplPolarity::Positive,
|
|
|
|
ast::Defaultness::Final,
|
|
|
|
generics,
|
|
|
|
Some(trait_ref),
|
|
|
|
self_type,
|
|
|
|
Vec::new()));
|
|
|
|
|
|
|
|
push(Annotatable::Item(newitem));
|
|
|
|
}
|