2019-12-05 08:10:33 -06:00
|
|
|
//! Builtin derives.
|
|
|
|
|
2023-11-24 09:38:48 -06:00
|
|
|
use base_db::{span::SpanData, CrateOrigin, LangCrateOrigin};
|
2023-05-03 05:44:47 -05:00
|
|
|
use itertools::izip;
|
2023-06-07 04:37:09 -05:00
|
|
|
use rustc_hash::FxHashSet;
|
2023-05-03 05:44:47 -05:00
|
|
|
use stdx::never;
|
2021-08-15 07:46:13 -05:00
|
|
|
use tracing::debug;
|
2019-12-05 12:29:57 -06:00
|
|
|
|
2023-06-07 04:37:09 -05:00
|
|
|
use crate::{
|
2023-11-24 09:38:48 -06:00
|
|
|
hygiene::span_with_def_site_ctxt,
|
2023-06-07 04:37:09 -05:00
|
|
|
name::{AsName, Name},
|
2023-11-24 09:38:48 -06:00
|
|
|
span::SpanMapRef,
|
|
|
|
tt,
|
2023-06-07 04:37:09 -05:00
|
|
|
};
|
2023-11-24 09:38:48 -06:00
|
|
|
use syntax::ast::{self, AstNode, FieldList, HasAttrs, HasGenericParams, HasName, HasTypeBounds};
|
2019-12-05 12:29:57 -06:00
|
|
|
|
2023-03-13 10:33:52 -05:00
|
|
|
use crate::{db::ExpandDatabase, name, quote, ExpandError, ExpandResult, MacroCallId};
|
2019-12-05 08:10:33 -06:00
|
|
|
|
|
|
|
macro_rules! register_builtin {
|
2019-12-13 14:43:53 -06:00
|
|
|
( $($trait:ident => $expand:ident),* ) => {
|
2019-12-05 08:10:33 -06:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
|
|
|
pub enum BuiltinDeriveExpander {
|
2019-12-13 14:43:53 -06:00
|
|
|
$($trait),*
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
impl BuiltinDeriveExpander {
|
|
|
|
pub fn expand(
|
|
|
|
&self,
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
token_map: SpanMapRef<'_>,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2019-12-05 08:10:33 -06:00
|
|
|
let expander = match *self {
|
2019-12-13 14:43:53 -06:00
|
|
|
$( BuiltinDeriveExpander::$trait => $expand, )*
|
2019-12-05 08:10:33 -06:00
|
|
|
};
|
2023-11-24 09:38:48 -06:00
|
|
|
|
|
|
|
let span = db.lookup_intern_macro_call(id).span(db);
|
|
|
|
let span = span_with_def_site_ctxt(db, span, id);
|
|
|
|
expander(db, id, span, tt, token_map)
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
|
|
|
|
2020-12-15 13:33:05 -06:00
|
|
|
fn find_by_name(name: &name::Name) -> Option<Self> {
|
|
|
|
match name {
|
|
|
|
$( id if id == &name::name![$trait] => Some(BuiltinDeriveExpander::$trait), )*
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
2020-12-15 13:33:05 -06:00
|
|
|
|
2019-12-05 08:10:33 -06:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
register_builtin! {
|
2019-12-13 14:43:53 -06:00
|
|
|
Copy => copy_expand,
|
|
|
|
Clone => clone_expand,
|
|
|
|
Default => default_expand,
|
|
|
|
Debug => debug_expand,
|
|
|
|
Hash => hash_expand,
|
|
|
|
Ord => ord_expand,
|
|
|
|
PartialOrd => partial_ord_expand,
|
|
|
|
Eq => eq_expand,
|
|
|
|
PartialEq => partial_eq_expand
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
|
|
|
|
2022-03-08 14:41:19 -06:00
|
|
|
pub fn find_builtin_derive(ident: &name::Name) -> Option<BuiltinDeriveExpander> {
|
|
|
|
BuiltinDeriveExpander::find_by_name(ident)
|
2020-12-15 13:33:05 -06:00
|
|
|
}
|
|
|
|
|
2023-05-03 05:44:47 -05:00
|
|
|
enum VariantShape {
|
|
|
|
Struct(Vec<tt::Ident>),
|
|
|
|
Tuple(usize),
|
|
|
|
Unit,
|
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
fn tuple_field_iterator(span: SpanData, n: usize) -> impl Iterator<Item = tt::Ident> {
|
|
|
|
(0..n).map(move |it| tt::Ident::new(format!("f{it}"), span))
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VariantShape {
|
2023-12-01 06:56:25 -06:00
|
|
|
fn as_pattern(&self, path: tt::Subtree, span: SpanData) -> tt::Subtree {
|
|
|
|
self.as_pattern_map(path, span, |it| quote!(span => #it))
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
fn field_names(&self, span: SpanData) -> Vec<tt::Ident> {
|
2023-05-03 05:44:47 -05:00
|
|
|
match self {
|
|
|
|
VariantShape::Struct(s) => s.clone(),
|
2023-12-01 06:56:25 -06:00
|
|
|
VariantShape::Tuple(n) => tuple_field_iterator(span, *n).collect(),
|
2023-05-03 05:44:47 -05:00
|
|
|
VariantShape::Unit => vec![],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_pattern_map(
|
|
|
|
&self,
|
|
|
|
path: tt::Subtree,
|
2023-12-01 06:56:25 -06:00
|
|
|
span: SpanData,
|
2023-05-03 05:44:47 -05:00
|
|
|
field_map: impl Fn(&tt::Ident) -> tt::Subtree,
|
|
|
|
) -> tt::Subtree {
|
|
|
|
match self {
|
|
|
|
VariantShape::Struct(fields) => {
|
2023-07-06 09:03:17 -05:00
|
|
|
let fields = fields.iter().map(|it| {
|
|
|
|
let mapped = field_map(it);
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span => #it : #mapped , }
|
2023-05-03 05:44:47 -05:00
|
|
|
});
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
#path { ##fields }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
&VariantShape::Tuple(n) => {
|
2023-12-01 06:56:25 -06:00
|
|
|
let fields = tuple_field_iterator(span, n).map(|it| {
|
2023-07-06 09:03:17 -05:00
|
|
|
let mapped = field_map(&it);
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
#mapped ,
|
|
|
|
}
|
|
|
|
});
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
#path ( ##fields )
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VariantShape::Unit => path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-24 09:38:48 -06:00
|
|
|
fn from(tm: SpanMapRef<'_>, value: Option<FieldList>) -> Result<Self, ExpandError> {
|
2023-05-03 05:44:47 -05:00
|
|
|
let r = match value {
|
|
|
|
None => VariantShape::Unit,
|
2023-07-06 09:03:17 -05:00
|
|
|
Some(FieldList::RecordFieldList(it)) => VariantShape::Struct(
|
|
|
|
it.fields()
|
|
|
|
.map(|it| it.name())
|
2023-07-10 08:19:00 -05:00
|
|
|
.map(|it| name_to_token(tm, it))
|
2023-05-03 05:44:47 -05:00
|
|
|
.collect::<Result<_, _>>()?,
|
|
|
|
),
|
2023-07-06 09:03:17 -05:00
|
|
|
Some(FieldList::TupleFieldList(it)) => VariantShape::Tuple(it.fields().count()),
|
2023-05-03 05:44:47 -05:00
|
|
|
};
|
|
|
|
Ok(r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enum AdtShape {
|
|
|
|
Struct(VariantShape),
|
|
|
|
Enum { variants: Vec<(tt::Ident, VariantShape)>, default_variant: Option<usize> },
|
|
|
|
Union,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AdtShape {
|
2023-12-01 06:56:25 -06:00
|
|
|
fn as_pattern(&self, span: SpanData, name: &tt::Ident) -> Vec<tt::Subtree> {
|
|
|
|
self.as_pattern_map(name, |it| quote!(span =>#it), span)
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
fn field_names(&self, span: SpanData) -> Vec<Vec<tt::Ident>> {
|
2023-05-03 05:44:47 -05:00
|
|
|
match self {
|
|
|
|
AdtShape::Struct(s) => {
|
2023-12-01 06:56:25 -06:00
|
|
|
vec![s.field_names(span)]
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
AdtShape::Enum { variants, .. } => {
|
2023-12-01 06:56:25 -06:00
|
|
|
variants.iter().map(|(_, fields)| fields.field_names(span)).collect()
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
AdtShape::Union => {
|
|
|
|
never!("using fields of union in derive is always wrong");
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn as_pattern_map(
|
|
|
|
&self,
|
|
|
|
name: &tt::Ident,
|
|
|
|
field_map: impl Fn(&tt::Ident) -> tt::Subtree,
|
2023-12-01 06:56:25 -06:00
|
|
|
span: SpanData,
|
2023-05-03 05:44:47 -05:00
|
|
|
) -> Vec<tt::Subtree> {
|
|
|
|
match self {
|
|
|
|
AdtShape::Struct(s) => {
|
2023-12-01 06:56:25 -06:00
|
|
|
vec![s.as_pattern_map(quote! {span => #name }, span, field_map)]
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
AdtShape::Enum { variants, .. } => variants
|
|
|
|
.iter()
|
2023-12-01 06:56:25 -06:00
|
|
|
.map(|(v, fields)| {
|
|
|
|
fields.as_pattern_map(quote! {span => #name :: #v }, span, &field_map)
|
|
|
|
})
|
2023-05-03 05:44:47 -05:00
|
|
|
.collect(),
|
|
|
|
AdtShape::Union => {
|
|
|
|
never!("pattern matching on union is always wrong");
|
2023-12-01 06:56:25 -06:00
|
|
|
vec![quote! {span => un }]
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
struct BasicAdtInfo {
|
|
|
|
name: tt::Ident,
|
2023-05-03 05:44:47 -05:00
|
|
|
shape: AdtShape,
|
2023-04-07 11:03:14 -05:00
|
|
|
/// first field is the name, and
|
|
|
|
/// second field is `Some(ty)` if it's a const param of type `ty`, `None` if it's a type param.
|
|
|
|
/// third fields is where bounds, if any
|
|
|
|
param_types: Vec<(tt::Subtree, Option<tt::Subtree>, Option<tt::Subtree>)>,
|
|
|
|
associated_types: Vec<tt::Subtree>,
|
2019-12-05 12:29:57 -06:00
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
fn parse_adt(
|
|
|
|
tm: SpanMapRef<'_>,
|
|
|
|
adt: &ast::Adt,
|
|
|
|
call_site: SpanData,
|
|
|
|
) -> Result<BasicAdtInfo, ExpandError> {
|
2023-09-29 05:37:57 -05:00
|
|
|
let (name, generic_param_list, shape) = match adt {
|
2023-06-07 04:37:09 -05:00
|
|
|
ast::Adt::Struct(it) => (
|
|
|
|
it.name(),
|
|
|
|
it.generic_param_list(),
|
2023-07-10 08:19:00 -05:00
|
|
|
AdtShape::Struct(VariantShape::from(tm, it.field_list())?),
|
2023-06-07 04:37:09 -05:00
|
|
|
),
|
|
|
|
ast::Adt::Enum(it) => {
|
|
|
|
let default_variant = it
|
|
|
|
.variant_list()
|
|
|
|
.into_iter()
|
2023-07-06 09:03:17 -05:00
|
|
|
.flat_map(|it| it.variants())
|
|
|
|
.position(|it| it.attrs().any(|it| it.simple_name() == Some("default".into())));
|
2023-06-07 04:37:09 -05:00
|
|
|
(
|
|
|
|
it.name(),
|
|
|
|
it.generic_param_list(),
|
|
|
|
AdtShape::Enum {
|
|
|
|
default_variant,
|
|
|
|
variants: it
|
|
|
|
.variant_list()
|
|
|
|
.into_iter()
|
2023-07-06 09:03:17 -05:00
|
|
|
.flat_map(|it| it.variants())
|
|
|
|
.map(|it| {
|
2023-06-07 04:37:09 -05:00
|
|
|
Ok((
|
2023-07-10 08:19:00 -05:00
|
|
|
name_to_token(tm, it.name())?,
|
|
|
|
VariantShape::from(tm, it.field_list())?,
|
2023-06-07 04:37:09 -05:00
|
|
|
))
|
|
|
|
})
|
|
|
|
.collect::<Result<_, ExpandError>>()?,
|
|
|
|
},
|
|
|
|
)
|
2019-12-05 12:29:57 -06:00
|
|
|
}
|
2023-06-07 04:37:09 -05:00
|
|
|
ast::Adt::Union(it) => (it.name(), it.generic_param_list(), AdtShape::Union),
|
2019-12-05 12:29:57 -06:00
|
|
|
};
|
2023-06-07 04:37:09 -05:00
|
|
|
|
|
|
|
let mut param_type_set: FxHashSet<Name> = FxHashSet::default();
|
|
|
|
let param_types = generic_param_list
|
2022-10-22 03:49:38 -05:00
|
|
|
.into_iter()
|
|
|
|
.flat_map(|param_list| param_list.type_or_const_params())
|
|
|
|
.map(|param| {
|
2023-04-07 11:03:14 -05:00
|
|
|
let name = {
|
|
|
|
let this = param.name();
|
|
|
|
match this {
|
2023-07-06 09:03:17 -05:00
|
|
|
Some(it) => {
|
|
|
|
param_type_set.insert(it.as_name());
|
2023-11-24 09:38:48 -06:00
|
|
|
mbe::syntax_node_to_token_tree(it.syntax(), tm)
|
2023-04-07 11:03:14 -05:00
|
|
|
}
|
2023-12-01 06:56:25 -06:00
|
|
|
None => {
|
|
|
|
tt::Subtree::empty(::tt::DelimSpan { open: call_site, close: call_site })
|
|
|
|
}
|
2023-04-07 11:03:14 -05:00
|
|
|
}
|
|
|
|
};
|
|
|
|
let bounds = match ¶m {
|
2023-11-24 09:38:48 -06:00
|
|
|
ast::TypeOrConstParam::Type(it) => {
|
|
|
|
it.type_bound_list().map(|it| mbe::syntax_node_to_token_tree(it.syntax(), tm))
|
|
|
|
}
|
2023-04-07 11:03:14 -05:00
|
|
|
ast::TypeOrConstParam::Const(_) => None,
|
|
|
|
};
|
|
|
|
let ty = if let ast::TypeOrConstParam::Const(param) = param {
|
2022-10-22 03:49:38 -05:00
|
|
|
let ty = param
|
|
|
|
.ty()
|
2023-11-24 09:38:48 -06:00
|
|
|
.map(|ty| mbe::syntax_node_to_token_tree(ty.syntax(), tm))
|
2023-12-01 06:56:25 -06:00
|
|
|
.unwrap_or_else(|| {
|
|
|
|
tt::Subtree::empty(::tt::DelimSpan { open: call_site, close: call_site })
|
|
|
|
});
|
2022-10-22 03:49:38 -05:00
|
|
|
Some(ty)
|
|
|
|
} else {
|
|
|
|
None
|
2023-04-07 11:03:14 -05:00
|
|
|
};
|
|
|
|
(name, ty, bounds)
|
2019-12-12 07:47:54 -06:00
|
|
|
})
|
2022-10-22 03:49:38 -05:00
|
|
|
.collect();
|
2023-06-07 04:37:09 -05:00
|
|
|
|
|
|
|
// For a generic parameter `T`, when shorthand associated type `T::Assoc` appears in field
|
|
|
|
// types (of any variant for enums), we generate trait bound for it. It sounds reasonable to
|
|
|
|
// also generate trait bound for qualified associated type `<T as Trait>::Assoc`, but rustc
|
|
|
|
// does not do that for some unknown reason.
|
|
|
|
//
|
|
|
|
// See the analogous function in rustc [find_type_parameters()] and rust-lang/rust#50730.
|
|
|
|
// [find_type_parameters()]: https://github.com/rust-lang/rust/blob/1.70.0/compiler/rustc_builtin_macros/src/deriving/generic/mod.rs#L378
|
|
|
|
|
|
|
|
// It's cumbersome to deal with the distinct structures of ADTs, so let's just get untyped
|
|
|
|
// `SyntaxNode` that contains fields and look for descendant `ast::PathType`s. Of note is that
|
|
|
|
// we should not inspect `ast::PathType`s in parameter bounds and where clauses.
|
|
|
|
let field_list = match adt {
|
|
|
|
ast::Adt::Enum(it) => it.variant_list().map(|list| list.syntax().clone()),
|
|
|
|
ast::Adt::Struct(it) => it.field_list().map(|list| list.syntax().clone()),
|
|
|
|
ast::Adt::Union(it) => it.record_field_list().map(|list| list.syntax().clone()),
|
2023-04-07 11:03:14 -05:00
|
|
|
};
|
2023-06-07 04:37:09 -05:00
|
|
|
let associated_types = field_list
|
|
|
|
.into_iter()
|
|
|
|
.flat_map(|it| it.descendants())
|
|
|
|
.filter_map(ast::PathType::cast)
|
|
|
|
.filter_map(|p| {
|
|
|
|
let name = p.path()?.qualifier()?.as_single_name_ref()?.as_name();
|
|
|
|
param_type_set.contains(&name).then_some(p)
|
|
|
|
})
|
2023-11-24 09:38:48 -06:00
|
|
|
.map(|it| mbe::syntax_node_to_token_tree(it.syntax(), tm))
|
2023-06-07 04:37:09 -05:00
|
|
|
.collect();
|
2023-11-24 09:38:48 -06:00
|
|
|
let name_token = name_to_token(tm, name)?;
|
2023-05-03 05:44:47 -05:00
|
|
|
Ok(BasicAdtInfo { name: name_token, shape, param_types, associated_types })
|
|
|
|
}
|
|
|
|
|
2023-11-24 09:38:48 -06:00
|
|
|
fn name_to_token(
|
|
|
|
token_map: SpanMapRef<'_>,
|
|
|
|
name: Option<ast::Name>,
|
|
|
|
) -> Result<tt::Ident, ExpandError> {
|
2023-04-07 11:03:14 -05:00
|
|
|
let name = name.ok_or_else(|| {
|
|
|
|
debug!("parsed item has no name");
|
2023-06-07 04:20:10 -05:00
|
|
|
ExpandError::other("missing name")
|
2023-04-07 11:03:14 -05:00
|
|
|
})?;
|
2023-11-24 09:38:48 -06:00
|
|
|
let span = token_map.span_for_range(name.syntax().text_range());
|
2023-09-29 05:37:57 -05:00
|
|
|
let name_token = tt::Ident { span, text: name.text().into() };
|
2023-05-03 05:44:47 -05:00
|
|
|
Ok(name_token)
|
2019-12-05 12:29:57 -06:00
|
|
|
}
|
|
|
|
|
2023-04-07 11:03:14 -05:00
|
|
|
/// Given that we are deriving a trait `DerivedTrait` for a type like:
|
|
|
|
///
|
|
|
|
/// ```ignore (only-for-syntax-highlight)
|
|
|
|
/// struct Struct<'a, ..., 'z, A, B: DeclaredTrait, C, ..., Z> where C: WhereTrait {
|
|
|
|
/// a: A,
|
|
|
|
/// b: B::Item,
|
|
|
|
/// b1: <B as DeclaredTrait>::Item,
|
|
|
|
/// c1: <C as WhereTrait>::Item,
|
|
|
|
/// c2: Option<<C as WhereTrait>::Item>,
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// create an impl like:
|
|
|
|
///
|
|
|
|
/// ```ignore (only-for-syntax-highlight)
|
|
|
|
/// impl<'a, ..., 'z, A, B: DeclaredTrait, C, ... Z> where
|
|
|
|
/// C: WhereTrait,
|
|
|
|
/// A: DerivedTrait + B1 + ... + BN,
|
|
|
|
/// B: DerivedTrait + B1 + ... + BN,
|
|
|
|
/// C: DerivedTrait + B1 + ... + BN,
|
|
|
|
/// B::Item: DerivedTrait + B1 + ... + BN,
|
|
|
|
/// <C as WhereTrait>::Item: DerivedTrait + B1 + ... + BN,
|
|
|
|
/// ...
|
|
|
|
/// {
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
2023-06-07 04:37:09 -05:00
|
|
|
/// where B1, ..., BN are the bounds given by `bounds_paths`. Z is a phantom type, and
|
2023-04-07 11:03:14 -05:00
|
|
|
/// therefore does not get bound by the derived trait.
|
2023-05-03 05:44:47 -05:00
|
|
|
fn expand_simple_derive(
|
2023-11-24 09:38:48 -06:00
|
|
|
// FIXME: use
|
2023-12-01 06:56:25 -06:00
|
|
|
invoc_span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2023-05-03 05:44:47 -05:00
|
|
|
trait_path: tt::Subtree,
|
2023-06-07 04:37:09 -05:00
|
|
|
make_trait_body: impl FnOnce(&BasicAdtInfo) -> tt::Subtree,
|
2023-05-03 05:44:47 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let info = match parse_adt(tm, tt, invoc_span) {
|
2021-08-20 07:28:36 -05:00
|
|
|
Ok(info) => info,
|
2023-12-01 06:56:25 -06:00
|
|
|
Err(e) => {
|
|
|
|
return ExpandResult::new(
|
|
|
|
tt::Subtree::empty(tt::DelimSpan { open: invoc_span, close: invoc_span }),
|
|
|
|
e,
|
|
|
|
)
|
|
|
|
}
|
2021-08-20 07:28:36 -05:00
|
|
|
};
|
2023-06-07 04:37:09 -05:00
|
|
|
let trait_body = make_trait_body(&info);
|
2023-04-07 11:03:14 -05:00
|
|
|
let mut where_block = vec![];
|
2022-10-22 03:49:38 -05:00
|
|
|
let (params, args): (Vec<_>, Vec<_>) = info
|
|
|
|
.param_types
|
|
|
|
.into_iter()
|
2023-04-07 11:03:14 -05:00
|
|
|
.map(|(ident, param_ty, bound)| {
|
2022-10-22 03:49:38 -05:00
|
|
|
let ident_ = ident.clone();
|
2023-04-07 11:03:14 -05:00
|
|
|
if let Some(b) = bound {
|
|
|
|
let ident = ident.clone();
|
2023-12-01 06:56:25 -06:00
|
|
|
where_block.push(quote! {invoc_span => #ident : #b , });
|
2023-04-07 11:03:14 -05:00
|
|
|
}
|
2022-10-22 03:49:38 -05:00
|
|
|
if let Some(ty) = param_ty {
|
2023-12-01 06:56:25 -06:00
|
|
|
(quote! {invoc_span => const #ident : #ty , }, quote! {invoc_span => #ident_ , })
|
2022-10-22 03:49:38 -05:00
|
|
|
} else {
|
|
|
|
let bound = trait_path.clone();
|
2023-12-01 06:56:25 -06:00
|
|
|
(quote! {invoc_span => #ident : #bound , }, quote! {invoc_span => #ident_ , })
|
2022-10-22 03:49:38 -05:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.unzip();
|
2023-04-07 11:03:14 -05:00
|
|
|
|
2023-07-06 09:03:17 -05:00
|
|
|
where_block.extend(info.associated_types.iter().map(|it| {
|
|
|
|
let it = it.clone();
|
2023-04-07 11:03:14 -05:00
|
|
|
let bound = trait_path.clone();
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {invoc_span => #it : #bound , }
|
2023-04-07 11:03:14 -05:00
|
|
|
}));
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
let name = info.name;
|
2023-12-01 06:56:25 -06:00
|
|
|
let expanded = quote! {invoc_span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
impl < ##params > #trait_path for #name < ##args > where ##where_block { #trait_body }
|
2019-12-05 08:10:33 -06:00
|
|
|
};
|
2021-08-20 07:28:36 -05:00
|
|
|
ExpandResult::ok(expanded)
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
fn find_builtin_crate(db: &dyn ExpandDatabase, id: MacroCallId, span: SpanData) -> tt::TokenTree {
|
2020-04-27 12:48:55 -05:00
|
|
|
// FIXME: make hygiene works for builtin derive macro
|
|
|
|
// such that $crate can be used here.
|
|
|
|
let cg = db.crate_graph();
|
2021-11-14 09:25:40 -06:00
|
|
|
let krate = db.lookup_intern_macro_call(id).krate;
|
2020-04-27 12:48:55 -05:00
|
|
|
|
2022-05-20 09:42:28 -05:00
|
|
|
let tt = if matches!(cg[krate].origin, CrateOrigin::Lang(LangCrateOrigin::Core)) {
|
2021-10-10 08:13:45 -05:00
|
|
|
cov_mark::hit!(test_copy_expand_in_core);
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span => crate }
|
2022-05-20 09:42:28 -05:00
|
|
|
} else {
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span => core }
|
2020-04-27 12:48:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
tt.token_trees[0].clone()
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:52:52 -06:00
|
|
|
fn copy_expand(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2023-11-24 09:38:48 -06:00
|
|
|
span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let krate = find_builtin_crate(db, id, span);
|
|
|
|
expand_simple_derive(span, tt, tm, quote! {span => #krate::marker::Copy }, |_| quote! {span =>})
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
2019-12-05 08:10:33 -06:00
|
|
|
fn clone_expand(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2023-11-24 09:38:48 -06:00
|
|
|
span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let krate = find_builtin_crate(db, id, span);
|
|
|
|
expand_simple_derive(span, tt, tm, quote! {span => #krate::clone::Clone }, |adt| {
|
2023-05-03 05:44:47 -05:00
|
|
|
if matches!(adt.shape, AdtShape::Union) {
|
2023-12-01 06:56:25 -06:00
|
|
|
let star = tt::Punct { char: '*', spacing: ::tt::Spacing::Alone, span };
|
|
|
|
return quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
#star self
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
if matches!(&adt.shape, AdtShape::Enum { variants, .. } if variants.is_empty()) {
|
2023-12-01 06:56:25 -06:00
|
|
|
let star = tt::Punct { char: '*', spacing: ::tt::Spacing::Alone, span };
|
|
|
|
return quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
match #star self {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let name = &adt.name;
|
2023-12-01 06:56:25 -06:00
|
|
|
let patterns = adt.shape.as_pattern(span, name);
|
|
|
|
let exprs = adt.shape.as_pattern_map(name, |it| quote! {span => #it .clone() }, span);
|
2023-05-03 05:44:47 -05:00
|
|
|
let arms = patterns.into_iter().zip(exprs.into_iter()).map(|(pat, expr)| {
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow = fat_arrow(span);
|
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
#pat #fat_arrow #expr,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
fn clone(&self) -> Self {
|
|
|
|
match self {
|
|
|
|
##arms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
/// This function exists since `quote! {span => => }` doesn't work.
|
|
|
|
fn fat_arrow(span: SpanData) -> tt::Subtree {
|
|
|
|
let eq = tt::Punct { char: '=', spacing: ::tt::Spacing::Joint, span };
|
|
|
|
quote! {span => #eq> }
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
/// This function exists since `quote! {span => && }` doesn't work.
|
|
|
|
fn and_and(span: SpanData) -> tt::Subtree {
|
|
|
|
let and = tt::Punct { char: '&', spacing: ::tt::Spacing::Joint, span };
|
|
|
|
quote! {span => #and& }
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn default_expand(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2023-11-24 09:38:48 -06:00
|
|
|
span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let krate = &find_builtin_crate(db, id, span);
|
|
|
|
expand_simple_derive(span, tt, tm, quote! {span => #krate::default::Default }, |adt| {
|
2023-05-03 05:44:47 -05:00
|
|
|
let body = match &adt.shape {
|
|
|
|
AdtShape::Struct(fields) => {
|
|
|
|
let name = &adt.name;
|
2023-12-01 06:56:25 -06:00
|
|
|
fields.as_pattern_map(
|
|
|
|
quote!(span =>#name),
|
|
|
|
span,
|
|
|
|
|_| quote!(span =>#krate::default::Default::default()),
|
|
|
|
)
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
AdtShape::Enum { default_variant, variants } => {
|
|
|
|
if let Some(d) = default_variant {
|
|
|
|
let (name, fields) = &variants[*d];
|
|
|
|
let adt_name = &adt.name;
|
|
|
|
fields.as_pattern_map(
|
2023-12-01 06:56:25 -06:00
|
|
|
quote!(span =>#adt_name :: #name),
|
|
|
|
span,
|
|
|
|
|_| quote!(span =>#krate::default::Default::default()),
|
2023-05-03 05:44:47 -05:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
// FIXME: Return expand error here
|
2023-12-01 06:56:25 -06:00
|
|
|
quote!(span =>)
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
AdtShape::Union => {
|
|
|
|
// FIXME: Return expand error here
|
2023-12-01 06:56:25 -06:00
|
|
|
quote!(span =>)
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
};
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
fn default() -> Self {
|
|
|
|
#body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn debug_expand(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2023-11-24 09:38:48 -06:00
|
|
|
span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let krate = &find_builtin_crate(db, id, span);
|
|
|
|
expand_simple_derive(span, tt, tm, quote! {span => #krate::fmt::Debug }, |adt| {
|
2023-05-03 05:44:47 -05:00
|
|
|
let for_variant = |name: String, v: &VariantShape| match v {
|
|
|
|
VariantShape::Struct(fields) => {
|
2023-07-06 09:03:17 -05:00
|
|
|
let for_fields = fields.iter().map(|it| {
|
|
|
|
let x_string = it.to_string();
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-07-06 09:03:17 -05:00
|
|
|
.field(#x_string, & #it)
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
});
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
f.debug_struct(#name) ##for_fields .finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
VariantShape::Tuple(n) => {
|
2023-12-01 06:56:25 -06:00
|
|
|
let for_fields = tuple_field_iterator(span, *n).map(|it| {
|
|
|
|
quote! {span =>
|
2023-07-06 09:03:17 -05:00
|
|
|
.field( & #it)
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
});
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
f.debug_tuple(#name) ##for_fields .finish()
|
|
|
|
}
|
|
|
|
}
|
2023-12-01 06:56:25 -06:00
|
|
|
VariantShape::Unit => quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
f.write_str(#name)
|
|
|
|
},
|
|
|
|
};
|
|
|
|
if matches!(&adt.shape, AdtShape::Enum { variants, .. } if variants.is_empty()) {
|
2023-12-01 06:56:25 -06:00
|
|
|
let star = tt::Punct { char: '*', spacing: ::tt::Spacing::Alone, span };
|
|
|
|
return quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
fn fmt(&self, f: &mut #krate::fmt::Formatter) -> #krate::fmt::Result {
|
|
|
|
match #star self {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
let arms = match &adt.shape {
|
|
|
|
AdtShape::Struct(fields) => {
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow = fat_arrow(span);
|
2023-05-03 05:44:47 -05:00
|
|
|
let name = &adt.name;
|
2023-12-01 06:56:25 -06:00
|
|
|
let pat = fields.as_pattern(quote!(span =>#name), span);
|
2023-05-03 05:44:47 -05:00
|
|
|
let expr = for_variant(name.to_string(), fields);
|
2023-12-01 06:56:25 -06:00
|
|
|
vec![quote! {span => #pat #fat_arrow #expr }]
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
AdtShape::Enum { variants, .. } => variants
|
|
|
|
.iter()
|
|
|
|
.map(|(name, v)| {
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow = fat_arrow(span);
|
2023-05-03 05:44:47 -05:00
|
|
|
let adt_name = &adt.name;
|
2023-12-01 06:56:25 -06:00
|
|
|
let pat = v.as_pattern(quote!(span =>#adt_name :: #name), span);
|
2023-05-03 05:44:47 -05:00
|
|
|
let expr = for_variant(name.to_string(), v);
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
#pat #fat_arrow #expr ,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect(),
|
|
|
|
AdtShape::Union => {
|
|
|
|
// FIXME: Return expand error here
|
|
|
|
vec![]
|
|
|
|
}
|
|
|
|
};
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
fn fmt(&self, f: &mut #krate::fmt::Formatter) -> #krate::fmt::Result {
|
|
|
|
match self {
|
|
|
|
##arms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_expand(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2023-11-24 09:38:48 -06:00
|
|
|
span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let krate = &find_builtin_crate(db, id, span);
|
|
|
|
expand_simple_derive(span, tt, tm, quote! {span => #krate::hash::Hash }, |adt| {
|
2023-05-03 05:44:47 -05:00
|
|
|
if matches!(adt.shape, AdtShape::Union) {
|
|
|
|
// FIXME: Return expand error here
|
2023-12-01 06:56:25 -06:00
|
|
|
return quote! {span =>};
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
if matches!(&adt.shape, AdtShape::Enum { variants, .. } if variants.is_empty()) {
|
2023-12-01 06:56:25 -06:00
|
|
|
let star = tt::Punct { char: '*', spacing: ::tt::Spacing::Alone, span };
|
|
|
|
return quote! {span =>
|
2023-06-13 00:50:27 -05:00
|
|
|
fn hash<H: #krate::hash::Hasher>(&self, ra_expand_state: &mut H) {
|
2023-05-03 05:44:47 -05:00
|
|
|
match #star self {}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2023-12-01 06:56:25 -06:00
|
|
|
let arms =
|
|
|
|
adt.shape.as_pattern(span, &adt.name).into_iter().zip(adt.shape.field_names(span)).map(
|
|
|
|
|(pat, names)| {
|
|
|
|
let expr = {
|
|
|
|
let it =
|
|
|
|
names.iter().map(|it| quote! {span => #it . hash(ra_expand_state); });
|
|
|
|
quote! {span => {
|
|
|
|
##it
|
|
|
|
} }
|
|
|
|
};
|
|
|
|
let fat_arrow = fat_arrow(span);
|
|
|
|
quote! {span =>
|
|
|
|
#pat #fat_arrow #expr ,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
);
|
2023-07-14 11:45:18 -05:00
|
|
|
let check_discriminant = if matches!(&adt.shape, AdtShape::Enum { .. }) {
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span => #krate::mem::discriminant(self).hash(ra_expand_state); }
|
2023-07-14 11:45:18 -05:00
|
|
|
} else {
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>}
|
2023-07-14 11:45:18 -05:00
|
|
|
};
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-06-13 00:50:27 -05:00
|
|
|
fn hash<H: #krate::hash::Hasher>(&self, ra_expand_state: &mut H) {
|
2023-07-14 11:45:18 -05:00
|
|
|
#check_discriminant
|
2023-05-03 05:44:47 -05:00
|
|
|
match self {
|
|
|
|
##arms
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
2023-03-13 10:33:52 -05:00
|
|
|
fn eq_expand(
|
|
|
|
db: &dyn ExpandDatabase,
|
|
|
|
id: MacroCallId,
|
2023-11-24 09:38:48 -06:00
|
|
|
span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2023-03-13 10:33:52 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let krate = find_builtin_crate(db, id, span);
|
|
|
|
expand_simple_derive(span, tt, tm, quote! {span => #krate::cmp::Eq }, |_| quote! {span =>})
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn partial_eq_expand(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2023-11-24 09:38:48 -06:00
|
|
|
span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let krate = find_builtin_crate(db, id, span);
|
|
|
|
expand_simple_derive(span, tt, tm, quote! {span => #krate::cmp::PartialEq }, |adt| {
|
2023-05-03 05:44:47 -05:00
|
|
|
if matches!(adt.shape, AdtShape::Union) {
|
|
|
|
// FIXME: Return expand error here
|
2023-12-01 06:56:25 -06:00
|
|
|
return quote! {span =>};
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
let name = &adt.name;
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
let (self_patterns, other_patterns) = self_and_other_patterns(adt, name, span);
|
|
|
|
let arms = izip!(self_patterns, other_patterns, adt.shape.field_names(span)).map(
|
2023-05-03 05:44:47 -05:00
|
|
|
|(pat1, pat2, names)| {
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow = fat_arrow(span);
|
2023-05-03 05:44:47 -05:00
|
|
|
let body = match &*names {
|
|
|
|
[] => {
|
2023-12-01 06:56:25 -06:00
|
|
|
quote!(span =>true)
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
[first, rest @ ..] => {
|
2023-07-06 09:03:17 -05:00
|
|
|
let rest = rest.iter().map(|it| {
|
2023-09-29 05:37:57 -05:00
|
|
|
let t1 = tt::Ident::new(format!("{}_self", it.text), it.span);
|
|
|
|
let t2 = tt::Ident::new(format!("{}_other", it.text), it.span);
|
2023-12-01 06:56:25 -06:00
|
|
|
let and_and = and_and(span);
|
|
|
|
quote!(span =>#and_and #t1 .eq( #t2 ))
|
2023-05-03 05:44:47 -05:00
|
|
|
});
|
|
|
|
let first = {
|
2023-09-29 05:37:57 -05:00
|
|
|
let t1 = tt::Ident::new(format!("{}_self", first.text), first.span);
|
|
|
|
let t2 = tt::Ident::new(format!("{}_other", first.text), first.span);
|
2023-12-01 06:56:25 -06:00
|
|
|
quote!(span =>#t1 .eq( #t2 ))
|
2023-05-03 05:44:47 -05:00
|
|
|
};
|
2023-12-01 06:56:25 -06:00
|
|
|
quote!(span =>#first ##rest)
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
|
|
|
};
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span => ( #pat1 , #pat2 ) #fat_arrow #body , }
|
2023-05-03 05:44:47 -05:00
|
|
|
},
|
|
|
|
);
|
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow = fat_arrow(span);
|
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
fn eq(&self, other: &Self) -> bool {
|
|
|
|
match (self, other) {
|
|
|
|
##arms
|
|
|
|
_unused #fat_arrow false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn self_and_other_patterns(
|
|
|
|
adt: &BasicAdtInfo,
|
|
|
|
name: &tt::Ident,
|
2023-12-01 06:56:25 -06:00
|
|
|
span: SpanData,
|
2023-05-03 05:44:47 -05:00
|
|
|
) -> (Vec<tt::Subtree>, Vec<tt::Subtree>) {
|
2023-12-01 06:56:25 -06:00
|
|
|
let self_patterns = adt.shape.as_pattern_map(
|
|
|
|
name,
|
|
|
|
|it| {
|
|
|
|
let t = tt::Ident::new(format!("{}_self", it.text), it.span);
|
|
|
|
quote!(span =>#t)
|
|
|
|
},
|
|
|
|
span,
|
|
|
|
);
|
|
|
|
let other_patterns = adt.shape.as_pattern_map(
|
|
|
|
name,
|
|
|
|
|it| {
|
|
|
|
let t = tt::Ident::new(format!("{}_other", it.text), it.span);
|
|
|
|
quote!(span =>#t)
|
|
|
|
},
|
|
|
|
span,
|
|
|
|
);
|
2023-05-03 05:44:47 -05:00
|
|
|
(self_patterns, other_patterns)
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn ord_expand(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2023-11-24 09:38:48 -06:00
|
|
|
span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let krate = &find_builtin_crate(db, id, span);
|
|
|
|
expand_simple_derive(span, tt, tm, quote! {span => #krate::cmp::Ord }, |adt| {
|
2023-05-03 05:44:47 -05:00
|
|
|
fn compare(
|
|
|
|
krate: &tt::TokenTree,
|
|
|
|
left: tt::Subtree,
|
|
|
|
right: tt::Subtree,
|
|
|
|
rest: tt::Subtree,
|
2023-12-01 06:56:25 -06:00
|
|
|
span: SpanData,
|
2023-05-03 05:44:47 -05:00
|
|
|
) -> tt::Subtree {
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow1 = fat_arrow(span);
|
|
|
|
let fat_arrow2 = fat_arrow(span);
|
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
match #left.cmp(&#right) {
|
|
|
|
#krate::cmp::Ordering::Equal #fat_arrow1 {
|
|
|
|
#rest
|
|
|
|
}
|
|
|
|
c #fat_arrow2 return c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if matches!(adt.shape, AdtShape::Union) {
|
|
|
|
// FIXME: Return expand error here
|
2023-12-01 06:56:25 -06:00
|
|
|
return quote!(span =>);
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
2023-12-01 06:56:25 -06:00
|
|
|
let (self_patterns, other_patterns) = self_and_other_patterns(adt, &adt.name, span);
|
|
|
|
let arms = izip!(self_patterns, other_patterns, adt.shape.field_names(span)).map(
|
2023-05-03 05:44:47 -05:00
|
|
|
|(pat1, pat2, fields)| {
|
2023-12-01 06:56:25 -06:00
|
|
|
let mut body = quote!(span =>#krate::cmp::Ordering::Equal);
|
2023-05-03 05:44:47 -05:00
|
|
|
for f in fields.into_iter().rev() {
|
2023-09-29 05:37:57 -05:00
|
|
|
let t1 = tt::Ident::new(format!("{}_self", f.text), f.span);
|
|
|
|
let t2 = tt::Ident::new(format!("{}_other", f.text), f.span);
|
2023-12-01 06:56:25 -06:00
|
|
|
body = compare(krate, quote!(span =>#t1), quote!(span =>#t2), body, span);
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow = fat_arrow(span);
|
|
|
|
quote! {span => ( #pat1 , #pat2 ) #fat_arrow #body , }
|
2023-05-03 05:44:47 -05:00
|
|
|
},
|
|
|
|
);
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow = fat_arrow(span);
|
|
|
|
let mut body = quote! {span =>
|
2023-07-14 11:45:18 -05:00
|
|
|
match (self, other) {
|
|
|
|
##arms
|
|
|
|
_unused #fat_arrow #krate::cmp::Ordering::Equal
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if matches!(&adt.shape, AdtShape::Enum { .. }) {
|
2023-12-01 06:56:25 -06:00
|
|
|
let left = quote!(span =>#krate::intrinsics::discriminant_value(self));
|
|
|
|
let right = quote!(span =>#krate::intrinsics::discriminant_value(other));
|
|
|
|
body = compare(krate, left, right, body, span);
|
2023-07-14 11:45:18 -05:00
|
|
|
}
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
fn cmp(&self, other: &Self) -> #krate::cmp::Ordering {
|
|
|
|
#body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
fn partial_ord_expand(
|
2023-03-13 10:33:52 -05:00
|
|
|
db: &dyn ExpandDatabase,
|
2021-05-19 13:19:08 -05:00
|
|
|
id: MacroCallId,
|
2023-11-24 09:38:48 -06:00
|
|
|
span: SpanData,
|
2023-07-10 08:19:00 -05:00
|
|
|
tt: &ast::Adt,
|
2023-11-24 09:38:48 -06:00
|
|
|
tm: SpanMapRef<'_>,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2023-12-01 06:56:25 -06:00
|
|
|
let krate = &find_builtin_crate(db, id, span);
|
|
|
|
expand_simple_derive(span, tt, tm, quote! {span => #krate::cmp::PartialOrd }, |adt| {
|
2023-05-03 05:44:47 -05:00
|
|
|
fn compare(
|
|
|
|
krate: &tt::TokenTree,
|
|
|
|
left: tt::Subtree,
|
|
|
|
right: tt::Subtree,
|
|
|
|
rest: tt::Subtree,
|
2023-12-01 06:56:25 -06:00
|
|
|
span: SpanData,
|
2023-05-03 05:44:47 -05:00
|
|
|
) -> tt::Subtree {
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow1 = fat_arrow(span);
|
|
|
|
let fat_arrow2 = fat_arrow(span);
|
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
match #left.partial_cmp(&#right) {
|
|
|
|
#krate::option::Option::Some(#krate::cmp::Ordering::Equal) #fat_arrow1 {
|
|
|
|
#rest
|
|
|
|
}
|
|
|
|
c #fat_arrow2 return c,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if matches!(adt.shape, AdtShape::Union) {
|
|
|
|
// FIXME: Return expand error here
|
2023-12-01 06:56:25 -06:00
|
|
|
return quote!(span =>);
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
2023-12-01 06:56:25 -06:00
|
|
|
let left = quote!(span =>#krate::intrinsics::discriminant_value(self));
|
|
|
|
let right = quote!(span =>#krate::intrinsics::discriminant_value(other));
|
2023-05-03 05:44:47 -05:00
|
|
|
|
2023-12-01 06:56:25 -06:00
|
|
|
let (self_patterns, other_patterns) = self_and_other_patterns(adt, &adt.name, span);
|
|
|
|
let arms = izip!(self_patterns, other_patterns, adt.shape.field_names(span)).map(
|
2023-05-03 05:44:47 -05:00
|
|
|
|(pat1, pat2, fields)| {
|
2023-12-01 06:56:25 -06:00
|
|
|
let mut body =
|
|
|
|
quote!(span =>#krate::option::Option::Some(#krate::cmp::Ordering::Equal));
|
2023-05-03 05:44:47 -05:00
|
|
|
for f in fields.into_iter().rev() {
|
2023-09-29 05:37:57 -05:00
|
|
|
let t1 = tt::Ident::new(format!("{}_self", f.text), f.span);
|
|
|
|
let t2 = tt::Ident::new(format!("{}_other", f.text), f.span);
|
2023-12-01 06:56:25 -06:00
|
|
|
body = compare(krate, quote!(span =>#t1), quote!(span =>#t2), body, span);
|
2023-05-03 05:44:47 -05:00
|
|
|
}
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow = fat_arrow(span);
|
|
|
|
quote! {span => ( #pat1 , #pat2 ) #fat_arrow #body , }
|
2023-05-03 05:44:47 -05:00
|
|
|
},
|
|
|
|
);
|
2023-12-01 06:56:25 -06:00
|
|
|
let fat_arrow = fat_arrow(span);
|
2023-05-03 05:44:47 -05:00
|
|
|
let body = compare(
|
|
|
|
krate,
|
|
|
|
left,
|
|
|
|
right,
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
match (self, other) {
|
|
|
|
##arms
|
|
|
|
_unused #fat_arrow #krate::option::Option::Some(#krate::cmp::Ordering::Equal)
|
|
|
|
}
|
|
|
|
},
|
2023-12-01 06:56:25 -06:00
|
|
|
span,
|
2023-05-03 05:44:47 -05:00
|
|
|
);
|
2023-12-01 06:56:25 -06:00
|
|
|
quote! {span =>
|
2023-05-03 05:44:47 -05:00
|
|
|
fn partial_cmp(&self, other: &Self) -> #krate::option::Option::Option<#krate::cmp::Ordering> {
|
|
|
|
#body
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|