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
|
|
|
|
2020-04-27 17:56:11 +00:00
|
|
|
use rustc_ast as ast;
|
2020-02-29 17:37:32 +00:00
|
|
|
use rustc_ast::ptr::P;
|
2021-01-29 07:31:08 +00:00
|
|
|
use rustc_ast::{ImplKind, ItemKind, MetaItem};
|
2020-03-09 17:50:12 +00:00
|
|
|
use rustc_expand::base::{Annotatable, ExpandResult, ExtCtxt, MultiItemModifier};
|
2020-04-19 11:00:18 +00:00
|
|
|
use rustc_span::symbol::{sym, Ident, Symbol};
|
2019-12-31 17:15:40 +00:00
|
|
|
use rustc_span::Span;
|
2014-05-16 07:16:13 +00:00
|
|
|
|
2017-12-06 18:50:55 +00:00
|
|
|
macro path_local($x:ident) {
|
2020-07-14 05:05:26 +00:00
|
|
|
generic::ty::Path::new_local(sym::$x)
|
2014-09-07 20:58:41 +00:00
|
|
|
}
|
|
|
|
|
2020-07-14 04:59:39 +00:00
|
|
|
macro pathvec_std($($rest:ident)::+) {{
|
2020-07-14 05:05:26 +00:00
|
|
|
vec![ $( sym::$rest ),+ ]
|
2017-12-06 18:50:55 +00:00
|
|
|
}}
|
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;
|
2015-10-18 16:03:42 +00:00
|
|
|
pub mod debug;
|
2019-12-22 22:42:04 +00:00
|
|
|
pub mod decodable;
|
2013-09-12 04:51:13 +00:00
|
|
|
pub mod default;
|
2019-12-22 22:42:04 +00:00
|
|
|
pub mod encodable;
|
|
|
|
pub mod hash;
|
2012-11-20 02:05:50 +00:00
|
|
|
|
2019-12-22 22:42:04 +00:00
|
|
|
#[path = "cmp/eq.rs"]
|
2013-03-30 14:58:05 +00:00
|
|
|
pub mod eq;
|
2019-12-22 22:42:04 +00:00
|
|
|
#[path = "cmp/ord.rs"]
|
2013-03-30 14:58:05 +00:00
|
|
|
pub mod ord;
|
2019-12-22 22:42:04 +00:00
|
|
|
#[path = "cmp/partial_eq.rs"]
|
|
|
|
pub mod partial_eq;
|
|
|
|
#[path = "cmp/partial_ord.rs"]
|
|
|
|
pub mod partial_ord;
|
2013-03-30 14:58:05 +00:00
|
|
|
|
2013-03-28 10:50:10 +00:00
|
|
|
pub mod generic;
|
|
|
|
|
2019-06-20 08:52:31 +00:00
|
|
|
crate struct BuiltinDerive(
|
2019-12-22 22:42:04 +00:00
|
|
|
crate fn(&mut ExtCtxt<'_>, Span, &MetaItem, &Annotatable, &mut dyn FnMut(Annotatable)),
|
2019-06-06 20:21:44 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
impl MultiItemModifier for BuiltinDerive {
|
2019-12-22 22:42:04 +00:00
|
|
|
fn expand(
|
|
|
|
&self,
|
|
|
|
ecx: &mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
meta_item: &MetaItem,
|
|
|
|
item: Annotatable,
|
2020-03-09 17:50:12 +00:00
|
|
|
) -> ExpandResult<Vec<Annotatable>, 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();
|
2020-11-23 00:32:39 +00:00
|
|
|
match item {
|
|
|
|
Annotatable::Stmt(stmt) => {
|
|
|
|
if let ast::StmtKind::Item(item) = stmt.into_inner().kind {
|
|
|
|
(self.0)(ecx, span, meta_item, &Annotatable::Item(item), &mut |a| {
|
|
|
|
// Cannot use 'ecx.stmt_item' here, because we need to pass 'ecx'
|
|
|
|
// to the function
|
|
|
|
items.push(Annotatable::Stmt(P(ast::Stmt {
|
|
|
|
id: ast::DUMMY_NODE_ID,
|
|
|
|
kind: ast::StmtKind::Item(a.expect_item()),
|
|
|
|
span,
|
|
|
|
})));
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
unreachable!("should have already errored on non-item statement")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => {
|
|
|
|
(self.0)(ecx, span, meta_item, &item, &mut |a| items.push(a));
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 17:50:12 +00:00
|
|
|
ExpandResult::Ready(items)
|
2019-06-06 20:21:44 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-10 05:31:19 +00:00
|
|
|
/// Constructs an expression that calls an intrinsic
|
2019-12-22 22:42:04 +00:00
|
|
|
fn call_intrinsic(
|
|
|
|
cx: &ExtCtxt<'_>,
|
|
|
|
span: Span,
|
2020-07-08 01:04:10 +00:00
|
|
|
intrinsic: Symbol,
|
2019-12-22 22:42:04 +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);
|
2020-07-08 01:04:10 +00:00
|
|
|
let path = cx.std_path(&[sym::intrinsics, intrinsic]);
|
2020-10-11 00:00:00 +00:00
|
|
|
cx.expr_call_global(span, path, args)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Constructs an expression that calls the `unreachable` intrinsic.
|
|
|
|
fn call_unreachable(cx: &ExtCtxt<'_>, span: Span) -> P<ast::Expr> {
|
|
|
|
let span = cx.with_def_site_ctxt(span);
|
|
|
|
let path = cx.std_path(&[sym::intrinsics, sym::unreachable]);
|
|
|
|
let call = cx.expr_call_global(span, path, vec![]);
|
2016-03-10 05:31:19 +00:00
|
|
|
|
|
|
|
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,
|
2020-08-21 21:52:52 +00:00
|
|
|
tokens: None,
|
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.)
|
2019-12-22 22:42:04 +00:00
|
|
|
fn inject_impl_of_structural_trait(
|
|
|
|
cx: &mut ExtCtxt<'_>,
|
|
|
|
span: Span,
|
|
|
|
item: &Annotatable,
|
2020-07-14 05:05:26 +00:00
|
|
|
structural_path: generic::ty::Path,
|
2019-12-22 22:42:04 +00:00
|
|
|
push: &mut dyn FnMut(Annotatable),
|
|
|
|
) {
|
2019-10-17 08:54:37 +00:00
|
|
|
let item = match *item {
|
|
|
|
Annotatable::Item(ref item) => item,
|
2020-11-18 22:55:59 +00:00
|
|
|
_ => unreachable!(),
|
2019-10-17 08:54:37 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let generics = match item.kind {
|
2019-12-22 22:42:04 +00:00
|
|
|
ItemKind::Struct(_, ref generics) | ItemKind::Enum(_, ref generics) => generics,
|
2019-10-17 08:54:37 +00:00
|
|
|
// 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).
|
2019-12-22 22:42:04 +00:00
|
|
|
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))
|
|
|
|
}
|
2020-12-31 00:58:27 +00:00
|
|
|
ast::GenericParamKind::Const { ty: _, kw_span: _, default } => {
|
|
|
|
*default = None;
|
2019-12-22 22:42:04 +00:00
|
|
|
ast::GenericArg::Const(cx.const_ident(span, param.ident))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect();
|
2019-10-17 08:54:37 +00:00
|
|
|
|
|
|
|
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();
|
2019-12-22 22:42:04 +00:00
|
|
|
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,
|
2020-04-19 11:00:18 +00:00
|
|
|
Ident::invalid(),
|
2019-12-22 22:42:04 +00:00
|
|
|
attrs,
|
2021-08-05 01:53:21 +00:00
|
|
|
ItemKind::Impl(Box::new(ImplKind {
|
2020-01-30 01:42:33 +00:00
|
|
|
unsafety: ast::Unsafe::No,
|
2020-01-14 04:30:20 +00:00
|
|
|
polarity: ast::ImplPolarity::Positive,
|
|
|
|
defaultness: ast::Defaultness::Final,
|
2020-01-30 01:42:33 +00:00
|
|
|
constness: ast::Const::No,
|
2019-12-22 22:42:04 +00:00
|
|
|
generics,
|
2020-01-14 04:30:20 +00:00
|
|
|
of_trait: Some(trait_ref),
|
|
|
|
self_ty: self_type,
|
|
|
|
items: Vec::new(),
|
2021-08-05 01:53:21 +00:00
|
|
|
})),
|
2019-12-22 22:42:04 +00:00
|
|
|
);
|
2019-10-17 08:54:37 +00:00
|
|
|
|
|
|
|
push(Annotatable::Item(newitem));
|
|
|
|
}
|