2019-12-05 08:10:33 -06:00
|
|
|
//! Builtin derives.
|
|
|
|
|
2022-05-20 09:42:28 -05:00
|
|
|
use base_db::{CrateOrigin, LangCrateOrigin};
|
2023-04-07 11:03:14 -05:00
|
|
|
use std::collections::HashSet;
|
2021-08-15 07:46:13 -05:00
|
|
|
use tracing::debug;
|
2019-12-05 12:29:57 -06:00
|
|
|
|
2023-01-31 04:49:49 -06:00
|
|
|
use crate::tt::{self, TokenId};
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2023-04-07 11:03:14 -05:00
|
|
|
ast::{self, AstNode, HasGenericParams, HasModuleItem, HasName, HasTypeBounds, PathType},
|
2019-12-05 12:29:57 -06:00
|
|
|
match_ast,
|
|
|
|
};
|
|
|
|
|
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,
|
2019-12-05 08:10:33 -06:00
|
|
|
tt: &tt::Subtree,
|
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
|
|
|
};
|
|
|
|
expander(db, id, tt)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
struct BasicAdtInfo {
|
|
|
|
name: tt::Ident,
|
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
|
|
|
}
|
|
|
|
|
2022-02-21 12:14:06 -06:00
|
|
|
fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, ExpandError> {
|
|
|
|
let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, mbe::TopEntryPoint::MacroItems);
|
2019-12-05 12:29:57 -06:00
|
|
|
let macro_items = ast::MacroItems::cast(parsed.syntax_node()).ok_or_else(|| {
|
|
|
|
debug!("derive node didn't parse");
|
2022-02-21 12:14:06 -06:00
|
|
|
ExpandError::Other("invalid item definition".into())
|
2019-12-05 12:29:57 -06:00
|
|
|
})?;
|
|
|
|
let item = macro_items.items().next().ok_or_else(|| {
|
|
|
|
debug!("no module item parsed");
|
2022-02-21 12:14:06 -06:00
|
|
|
ExpandError::Other("no item found".into())
|
2019-12-05 12:29:57 -06:00
|
|
|
})?;
|
|
|
|
let node = item.syntax();
|
2023-04-07 04:01:17 -05:00
|
|
|
let (name, params) = match_ast! {
|
2019-12-05 12:29:57 -06:00
|
|
|
match node {
|
2023-04-07 04:01:17 -05:00
|
|
|
ast::Struct(it) => (it.name(), it.generic_param_list()),
|
|
|
|
ast::Enum(it) => (it.name(), it.generic_param_list()),
|
|
|
|
ast::Union(it) => (it.name(), it.generic_param_list()),
|
2019-12-05 12:29:57 -06:00
|
|
|
_ => {
|
|
|
|
debug!("unexpected node is {:?}", node);
|
2022-02-21 12:14:06 -06:00
|
|
|
return Err(ExpandError::Other("expected struct, enum or union".into()))
|
2019-12-05 12:29:57 -06:00
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
2023-04-07 11:03:14 -05:00
|
|
|
let mut param_type_set: HashSet<String> = HashSet::new();
|
2022-10-22 03:49:38 -05:00
|
|
|
let param_types = params
|
|
|
|
.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 {
|
|
|
|
Some(x) => {
|
|
|
|
param_type_set.insert(x.to_string());
|
|
|
|
mbe::syntax_node_to_token_tree(x.syntax()).0
|
|
|
|
}
|
|
|
|
None => tt::Subtree::empty(),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let bounds = match ¶m {
|
|
|
|
ast::TypeOrConstParam::Type(x) => {
|
|
|
|
x.type_bound_list().map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0)
|
|
|
|
}
|
|
|
|
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()
|
|
|
|
.map(|ty| mbe::syntax_node_to_token_tree(ty.syntax()).0)
|
2023-01-31 04:49:49 -06:00
|
|
|
.unwrap_or_else(tt::Subtree::empty);
|
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-04-07 11:03:14 -05:00
|
|
|
let is_associated_type = |p: &PathType| {
|
|
|
|
if let Some(p) = p.path() {
|
|
|
|
if let Some(parent) = p.qualifier() {
|
|
|
|
if let Some(x) = parent.segment() {
|
|
|
|
if let Some(x) = x.path_type() {
|
|
|
|
if let Some(x) = x.path() {
|
|
|
|
if let Some(pname) = x.as_single_name_ref() {
|
|
|
|
if param_type_set.contains(&pname.to_string()) {
|
|
|
|
// <T as Trait>::Assoc
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(pname) = parent.as_single_name_ref() {
|
|
|
|
if param_type_set.contains(&pname.to_string()) {
|
|
|
|
// T::Assoc
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
false
|
|
|
|
};
|
|
|
|
let associated_types = node
|
|
|
|
.descendants()
|
|
|
|
.filter_map(PathType::cast)
|
|
|
|
.filter(is_associated_type)
|
|
|
|
.map(|x| mbe::syntax_node_to_token_tree(x.syntax()).0)
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
let name = name.ok_or_else(|| {
|
|
|
|
debug!("parsed item has no name");
|
|
|
|
ExpandError::Other("missing name".into())
|
|
|
|
})?;
|
|
|
|
let name_token_id =
|
|
|
|
token_map.token_by_range(name.syntax().text_range()).unwrap_or_else(TokenId::unspecified);
|
|
|
|
let name_token = tt::Ident { span: name_token_id, text: name.text().into() };
|
|
|
|
Ok(BasicAdtInfo { name: name_token, param_types, associated_types })
|
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,
|
|
|
|
/// ...
|
|
|
|
/// {
|
|
|
|
/// ...
|
|
|
|
/// }
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// where B1, ..., BN are the bounds given by `bounds_paths`.'. Z is a phantom type, and
|
|
|
|
/// therefore does not get bound by the derived trait.
|
2021-08-20 07:28:36 -05:00
|
|
|
fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
|
|
|
|
let info = match parse_adt(tt) {
|
|
|
|
Ok(info) => info,
|
2023-01-31 04:49:49 -06:00
|
|
|
Err(e) => return ExpandResult::with_err(tt::Subtree::empty(), e),
|
2021-08-20 07:28:36 -05:00
|
|
|
};
|
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();
|
|
|
|
where_block.push(quote! { #ident : #b , });
|
|
|
|
}
|
2022-10-22 03:49:38 -05:00
|
|
|
if let Some(ty) = param_ty {
|
|
|
|
(quote! { const #ident : #ty , }, quote! { #ident_ , })
|
|
|
|
} else {
|
|
|
|
let bound = trait_path.clone();
|
|
|
|
(quote! { #ident : #bound , }, quote! { #ident_ , })
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.unzip();
|
2023-04-07 11:03:14 -05:00
|
|
|
|
|
|
|
where_block.extend(info.associated_types.iter().map(|x| {
|
|
|
|
let x = x.clone();
|
|
|
|
let bound = trait_path.clone();
|
|
|
|
quote! { #x : #bound , }
|
|
|
|
}));
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
let name = info.name;
|
2019-12-05 08:10:33 -06:00
|
|
|
let expanded = quote! {
|
2023-04-07 11:03:14 -05:00
|
|
|
impl < ##params > #trait_path for #name < ##args > where ##where_block {}
|
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-03-13 10:33:52 -05:00
|
|
|
fn find_builtin_crate(db: &dyn ExpandDatabase, id: MacroCallId) -> 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);
|
2020-04-27 15:11:24 -05:00
|
|
|
quote! { crate }
|
2022-05-20 09:42:28 -05:00
|
|
|
} else {
|
|
|
|
quote! { 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,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::marker::Copy })
|
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,
|
2019-12-05 12:29:57 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::clone::Clone })
|
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,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::default::Default })
|
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,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::fmt::Debug })
|
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,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::hash::Hash })
|
2019-12-05 12:52:52 -06:00
|
|
|
}
|
|
|
|
|
2023-03-13 10:33:52 -05:00
|
|
|
fn eq_expand(
|
|
|
|
db: &dyn ExpandDatabase,
|
|
|
|
id: MacroCallId,
|
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::cmp::Eq })
|
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,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::cmp::PartialEq })
|
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,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::cmp::Ord })
|
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,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
2021-08-20 07:28:36 -05:00
|
|
|
) -> ExpandResult<tt::Subtree> {
|
2020-04-27 12:48:55 -05:00
|
|
|
let krate = find_builtin_crate(db, id);
|
|
|
|
expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd })
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|