2019-12-05 08:10:33 -06:00
|
|
|
//! Builtin derives.
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
use log::debug;
|
|
|
|
|
|
|
|
use ra_parser::FragmentKind;
|
|
|
|
use ra_syntax::{
|
|
|
|
ast::{self, AstNode, ModuleItemOwner, NameOwner, TypeParamsOwner},
|
|
|
|
match_ast,
|
|
|
|
};
|
|
|
|
|
|
|
|
use crate::db::AstDatabase;
|
2020-03-02 00:05:15 -06:00
|
|
|
use crate::{name, quote, LazyMacroId, MacroDefId, MacroDefKind};
|
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,
|
|
|
|
db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
id: LazyMacroId,
|
2019-12-05 08:10:33 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn find_builtin_derive(ident: &name::Name) -> Option<MacroDefId> {
|
|
|
|
let kind = match ident {
|
2019-12-13 15:01:06 -06:00
|
|
|
$( id if id == &name::name![$trait] => BuiltinDeriveExpander::$trait, )*
|
2019-12-05 08:10:33 -06:00
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(kind) })
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
struct BasicAdtInfo {
|
|
|
|
name: tt::Ident,
|
|
|
|
type_params: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_adt(tt: &tt::Subtree) -> Result<BasicAdtInfo, mbe::ExpandError> {
|
|
|
|
let (parsed, token_map) = mbe::token_tree_to_syntax_node(tt, FragmentKind::Items)?; // FragmentKind::Items doesn't parse attrs?
|
|
|
|
let macro_items = ast::MacroItems::cast(parsed.syntax_node()).ok_or_else(|| {
|
|
|
|
debug!("derive node didn't parse");
|
|
|
|
mbe::ExpandError::UnexpectedToken
|
|
|
|
})?;
|
|
|
|
let item = macro_items.items().next().ok_or_else(|| {
|
|
|
|
debug!("no module item parsed");
|
|
|
|
mbe::ExpandError::NoMatchingRule
|
|
|
|
})?;
|
|
|
|
let node = item.syntax();
|
|
|
|
let (name, params) = match_ast! {
|
|
|
|
match node {
|
|
|
|
ast::StructDef(it) => { (it.name(), it.type_param_list()) },
|
|
|
|
ast::EnumDef(it) => { (it.name(), it.type_param_list()) },
|
|
|
|
ast::UnionDef(it) => { (it.name(), it.type_param_list()) },
|
|
|
|
_ => {
|
|
|
|
debug!("unexpected node is {:?}", node);
|
|
|
|
return Err(mbe::ExpandError::ConversionError)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
};
|
|
|
|
let name = name.ok_or_else(|| {
|
|
|
|
debug!("parsed item has no name");
|
|
|
|
mbe::ExpandError::NoMatchingRule
|
|
|
|
})?;
|
|
|
|
let name_token_id = token_map.token_by_range(name.syntax().text_range()).ok_or_else(|| {
|
|
|
|
debug!("name token not found");
|
|
|
|
mbe::ExpandError::ConversionError
|
|
|
|
})?;
|
|
|
|
let name_token = tt::Ident { id: name_token_id, text: name.text().clone() };
|
|
|
|
let type_params = params.map_or(0, |type_param_list| type_param_list.type_params().count());
|
|
|
|
Ok(BasicAdtInfo { name: name_token, type_params })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
|
|
|
|
let mut result = Vec::<tt::TokenTree>::new();
|
2019-12-12 07:47:54 -06:00
|
|
|
result.push(
|
|
|
|
tt::Leaf::Punct(tt::Punct {
|
|
|
|
char: '<',
|
|
|
|
spacing: tt::Spacing::Alone,
|
|
|
|
id: tt::TokenId::unspecified(),
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
);
|
2019-12-05 12:29:57 -06:00
|
|
|
for i in 0..n {
|
|
|
|
if i > 0 {
|
2019-12-12 07:47:54 -06:00
|
|
|
result.push(
|
|
|
|
tt::Leaf::Punct(tt::Punct {
|
|
|
|
char: ',',
|
|
|
|
spacing: tt::Spacing::Alone,
|
|
|
|
id: tt::TokenId::unspecified(),
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
);
|
2019-12-05 12:29:57 -06:00
|
|
|
}
|
|
|
|
result.push(
|
|
|
|
tt::Leaf::Ident(tt::Ident {
|
|
|
|
id: tt::TokenId::unspecified(),
|
|
|
|
text: format!("T{}", i).into(),
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
);
|
|
|
|
result.extend(bound.iter().cloned());
|
|
|
|
}
|
2019-12-12 07:47:54 -06:00
|
|
|
result.push(
|
|
|
|
tt::Leaf::Punct(tt::Punct {
|
|
|
|
char: '>',
|
|
|
|
spacing: tt::Spacing::Alone,
|
|
|
|
id: tt::TokenId::unspecified(),
|
|
|
|
})
|
|
|
|
.into(),
|
|
|
|
);
|
2019-12-05 12:29:57 -06:00
|
|
|
result
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:52:52 -06:00
|
|
|
fn expand_simple_derive(
|
2019-12-05 12:29:57 -06:00
|
|
|
tt: &tt::Subtree,
|
2019-12-05 12:52:52 -06:00
|
|
|
trait_path: tt::Subtree,
|
2019-12-05 08:10:33 -06:00
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
2019-12-05 12:29:57 -06:00
|
|
|
let info = parse_adt(tt)?;
|
|
|
|
let name = info.name;
|
2019-12-05 12:52:52 -06:00
|
|
|
let trait_path_clone = trait_path.token_trees.clone();
|
|
|
|
let bound = (quote! { : ##trait_path_clone }).token_trees;
|
2019-12-05 12:29:57 -06:00
|
|
|
let type_params = make_type_args(info.type_params, bound);
|
|
|
|
let type_args = make_type_args(info.type_params, Vec::new());
|
2019-12-05 12:52:52 -06:00
|
|
|
let trait_path = trait_path.token_trees;
|
2019-12-05 08:10:33 -06:00
|
|
|
let expanded = quote! {
|
2019-12-05 12:52:52 -06:00
|
|
|
impl ##type_params ##trait_path for #name ##type_args {}
|
2019-12-05 08:10:33 -06:00
|
|
|
};
|
|
|
|
Ok(expanded)
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:52:52 -06:00
|
|
|
fn copy_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
expand_simple_derive(tt, quote! { std::marker::Copy })
|
|
|
|
}
|
|
|
|
|
2019-12-05 08:10:33 -06:00
|
|
|
fn clone_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-05 12:29:57 -06:00
|
|
|
tt: &tt::Subtree,
|
2019-12-05 08:10:33 -06:00
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
2019-12-05 12:52:52 -06:00
|
|
|
expand_simple_derive(tt, quote! { std::clone::Clone })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn default_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
expand_simple_derive(tt, quote! { std::default::Default })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn debug_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
expand_simple_derive(tt, quote! { std::fmt::Debug })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn hash_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
expand_simple_derive(tt, quote! { std::hash::Hash })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn eq_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
expand_simple_derive(tt, quote! { std::cmp::Eq })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn partial_eq_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
expand_simple_derive(tt, quote! { std::cmp::PartialEq })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn ord_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
expand_simple_derive(tt, quote! { std::cmp::Ord })
|
|
|
|
}
|
|
|
|
|
|
|
|
fn partial_ord_expand(
|
|
|
|
_db: &dyn AstDatabase,
|
2020-03-02 00:05:15 -06:00
|
|
|
_id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
|
|
|
expand_simple_derive(tt, quote! { std::cmp::PartialOrd })
|
2019-12-05 08:10:33 -06:00
|
|
|
}
|
2019-12-05 12:29:57 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2020-03-02 00:05:15 -06:00
|
|
|
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
|
2019-12-05 12:29:57 -06:00
|
|
|
use ra_db::{fixture::WithFixture, SourceDatabase};
|
|
|
|
|
|
|
|
fn expand_builtin_derive(s: &str, expander: BuiltinDeriveExpander) -> String {
|
|
|
|
let (db, file_id) = TestDB::with_single_file(&s);
|
|
|
|
let parsed = db.parse(file_id);
|
|
|
|
let items: Vec<_> =
|
2020-02-18 06:53:02 -06:00
|
|
|
parsed.syntax_node().descendants().filter_map(ast::ModuleItem::cast).collect();
|
2019-12-05 12:29:57 -06:00
|
|
|
|
|
|
|
let ast_id_map = db.ast_id_map(file_id.into());
|
|
|
|
|
|
|
|
// the first one should be a macro_rules
|
|
|
|
let def =
|
|
|
|
MacroDefId { krate: None, ast_id: None, kind: MacroDefKind::BuiltInDerive(expander) };
|
|
|
|
|
|
|
|
let loc = MacroCallLoc {
|
|
|
|
def,
|
|
|
|
kind: MacroCallKind::Attr(AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]))),
|
|
|
|
};
|
|
|
|
|
2020-02-17 05:32:13 -06:00
|
|
|
let id: MacroCallId = db.intern_macro(loc).into();
|
2019-12-08 02:16:52 -06:00
|
|
|
let parsed = db.parse_or_expand(id.as_file()).unwrap();
|
2019-12-05 12:29:57 -06:00
|
|
|
|
|
|
|
// FIXME text() for syntax nodes parsed from token tree looks weird
|
|
|
|
// because there's no whitespace, see below
|
|
|
|
parsed.text().to_string()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_copy_expand_simple() {
|
|
|
|
let expanded = expand_builtin_derive(
|
|
|
|
r#"
|
|
|
|
#[derive(Copy)]
|
|
|
|
struct Foo;
|
|
|
|
"#,
|
|
|
|
BuiltinDeriveExpander::Copy,
|
|
|
|
);
|
|
|
|
|
2020-03-04 10:12:39 -06:00
|
|
|
assert_eq!(expanded, "impl< >std::marker::CopyforFoo< >{}");
|
2019-12-05 12:29:57 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_copy_expand_with_type_params() {
|
|
|
|
let expanded = expand_builtin_derive(
|
|
|
|
r#"
|
|
|
|
#[derive(Copy)]
|
|
|
|
struct Foo<A, B>;
|
|
|
|
"#,
|
|
|
|
BuiltinDeriveExpander::Copy,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
expanded,
|
|
|
|
"impl<T0:std::marker::Copy,T1:std::marker::Copy>std::marker::CopyforFoo<T0,T1>{}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_copy_expand_with_lifetimes() {
|
|
|
|
let expanded = expand_builtin_derive(
|
|
|
|
r#"
|
|
|
|
#[derive(Copy)]
|
|
|
|
struct Foo<A, B, 'a, 'b>;
|
|
|
|
"#,
|
|
|
|
BuiltinDeriveExpander::Copy,
|
|
|
|
);
|
|
|
|
|
|
|
|
// We currently just ignore lifetimes
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
expanded,
|
|
|
|
"impl<T0:std::marker::Copy,T1:std::marker::Copy>std::marker::CopyforFoo<T0,T1>{}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clone_expand() {
|
|
|
|
let expanded = expand_builtin_derive(
|
|
|
|
r#"
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct Foo<A, B>;
|
|
|
|
"#,
|
|
|
|
BuiltinDeriveExpander::Clone,
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
expanded,
|
|
|
|
"impl<T0:std::clone::Clone,T1:std::clone::Clone>std::clone::CloneforFoo<T0,T1>{}"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|