2019-12-05 08:10:33 -06:00
|
|
|
//! Builtin derives.
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
use log::debug;
|
|
|
|
|
2020-08-12 10:06:49 -05:00
|
|
|
use parser::FragmentKind;
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::{
|
2020-07-30 08:36:21 -05:00
|
|
|
ast::{self, AstNode, GenericParamsOwner, ModuleItemOwner, NameOwner},
|
2019-12-05 12:29:57 -06:00
|
|
|
match_ast,
|
|
|
|
};
|
|
|
|
|
2020-12-15 13:33:05 -06:00
|
|
|
use crate::{db::AstDatabase, name, quote, AstId, CrateId, 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)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-12-15 13:33:05 -06:00
|
|
|
pub fn find_builtin_derive(
|
|
|
|
ident: &name::Name,
|
|
|
|
krate: CrateId,
|
|
|
|
ast_id: AstId<ast::Macro>,
|
|
|
|
) -> Option<MacroDefId> {
|
|
|
|
let expander = BuiltinDeriveExpander::find_by_name(ident)?;
|
|
|
|
Some(MacroDefId {
|
|
|
|
krate,
|
|
|
|
ast_id: Some(ast_id),
|
|
|
|
kind: MacroDefKind::BuiltInDerive(expander),
|
|
|
|
local_inner: false,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
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 {
|
2020-07-30 10:50:40 -05:00
|
|
|
ast::Struct(it) => (it.name(), it.generic_param_list()),
|
2020-07-30 10:52:53 -05:00
|
|
|
ast::Enum(it) => (it.name(), it.generic_param_list()),
|
2020-07-30 10:36:46 -05:00
|
|
|
ast::Union(it) => (it.name(), it.generic_param_list()),
|
2019-12-05 12:29:57 -06:00
|
|
|
_ => {
|
|
|
|
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
|
|
|
|
})?;
|
2021-01-19 16:56:11 -06:00
|
|
|
let name_token = tt::Ident { id: name_token_id, text: name.text().into() };
|
2019-12-05 12:29:57 -06:00
|
|
|
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> {
|
2021-03-15 04:15:08 -05:00
|
|
|
let mut result = Vec::<tt::TokenTree>::with_capacity(n * 2);
|
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)
|
|
|
|
}
|
|
|
|
|
2020-04-27 12:48:55 -05:00
|
|
|
fn find_builtin_crate(db: &dyn AstDatabase, id: LazyMacroId) -> tt::TokenTree {
|
|
|
|
// FIXME: make hygiene works for builtin derive macro
|
|
|
|
// such that $crate can be used here.
|
|
|
|
let cg = db.crate_graph();
|
2020-06-11 05:08:24 -05:00
|
|
|
let krate = db.lookup_intern_macro(id).krate;
|
2020-04-27 12:48:55 -05:00
|
|
|
|
2020-04-27 15:11:24 -05:00
|
|
|
// XXX
|
|
|
|
// All crates except core itself should have a dependency on core,
|
|
|
|
// We detect `core` by seeing whether it doesn't have such a dependency.
|
2020-07-01 02:53:53 -05:00
|
|
|
let tt = if cg[krate].dependencies.iter().any(|dep| &*dep.name == "core") {
|
2020-04-27 12:48:55 -05:00
|
|
|
quote! { core }
|
2020-04-27 15:11:24 -05:00
|
|
|
} else {
|
|
|
|
quote! { crate }
|
2020-04-27 12:48:55 -05:00
|
|
|
};
|
|
|
|
|
|
|
|
tt.token_trees[0].clone()
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:52:52 -06:00
|
|
|
fn copy_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
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(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
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> {
|
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(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
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(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
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(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
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
|
|
|
}
|
|
|
|
|
|
|
|
fn eq_expand(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
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(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
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(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
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(
|
2020-04-27 12:48:55 -05:00
|
|
|
db: &dyn AstDatabase,
|
|
|
|
id: LazyMacroId,
|
2019-12-05 12:52:52 -06:00
|
|
|
tt: &tt::Subtree,
|
|
|
|
) -> Result<tt::Subtree, mbe::ExpandError> {
|
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
|
|
|
}
|
2019-12-05 12:29:57 -06:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-08-13 09:25:38 -05:00
|
|
|
use base_db::{fixture::WithFixture, CrateId, SourceDatabase};
|
2021-03-10 14:05:02 -06:00
|
|
|
use expect_test::{expect, Expect};
|
2020-03-23 14:32:06 -05:00
|
|
|
use name::{known, Name};
|
2020-06-11 05:08:24 -05:00
|
|
|
|
|
|
|
use crate::{test_db::TestDB, AstId, MacroCallId, MacroCallKind, MacroCallLoc};
|
|
|
|
|
|
|
|
use super::*;
|
2019-12-05 12:29:57 -06:00
|
|
|
|
2021-03-10 14:05:02 -06:00
|
|
|
fn expand_builtin_derive(ra_fixture: &str, name: Name) -> String {
|
2020-12-15 13:33:05 -06:00
|
|
|
let expander = BuiltinDeriveExpander::find_by_name(&name).unwrap();
|
2020-04-27 14:32:47 -05:00
|
|
|
let fixture = format!(
|
|
|
|
r#"//- /main.rs crate:main deps:core
|
2021-01-06 14:15:48 -06:00
|
|
|
$0
|
2020-04-27 14:32:47 -05:00
|
|
|
{}
|
|
|
|
//- /lib.rs crate:core
|
|
|
|
// empty
|
|
|
|
"#,
|
2021-03-10 14:05:02 -06:00
|
|
|
ra_fixture
|
2020-04-27 14:32:47 -05:00
|
|
|
);
|
2020-03-18 04:47:59 -05:00
|
|
|
|
2020-04-27 14:32:47 -05:00
|
|
|
let (db, file_pos) = TestDB::with_position(&fixture);
|
|
|
|
let file_id = file_pos.file_id;
|
2019-12-05 12:29:57 -06:00
|
|
|
let parsed = db.parse(file_id);
|
|
|
|
let items: Vec<_> =
|
2020-07-29 17:23:03 -05:00
|
|
|
parsed.syntax_node().descendants().filter_map(ast::Item::cast).collect();
|
2019-12-05 12:29:57 -06:00
|
|
|
|
|
|
|
let ast_id_map = db.ast_id_map(file_id.into());
|
|
|
|
|
2020-03-18 04:47:59 -05:00
|
|
|
let attr_id = AstId::new(file_id.into(), ast_id_map.ast_id(&items[0]));
|
2019-12-05 12:29:57 -06:00
|
|
|
|
2020-06-11 05:08:24 -05:00
|
|
|
let loc = MacroCallLoc {
|
2020-12-15 13:33:05 -06:00
|
|
|
def: MacroDefId {
|
|
|
|
krate: CrateId(0),
|
|
|
|
ast_id: None,
|
|
|
|
kind: MacroDefKind::BuiltInDerive(expander),
|
|
|
|
local_inner: false,
|
|
|
|
},
|
2020-06-11 05:08:24 -05:00
|
|
|
krate: CrateId(0),
|
|
|
|
kind: MacroCallKind::Attr(attr_id, name.to_string()),
|
|
|
|
};
|
2019-12-05 12:29:57 -06:00
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2021-03-10 14:05:02 -06:00
|
|
|
fn check_derive(ra_fixture: &str, name: Name, expected: Expect) {
|
|
|
|
let expanded = expand_builtin_derive(ra_fixture, name);
|
|
|
|
expected.assert_eq(&expanded);
|
|
|
|
}
|
|
|
|
|
2019-12-05 12:29:57 -06:00
|
|
|
#[test]
|
|
|
|
fn test_copy_expand_simple() {
|
2021-03-10 14:05:02 -06:00
|
|
|
check_derive(
|
2019-12-05 12:29:57 -06:00
|
|
|
r#"
|
2021-03-10 14:05:02 -06:00
|
|
|
#[derive(Copy)]
|
|
|
|
struct Foo;
|
|
|
|
"#,
|
2020-03-23 14:32:06 -05:00
|
|
|
known::Copy,
|
2021-03-10 14:05:02 -06:00
|
|
|
expect![["impl< >core::marker::CopyforFoo< >{}"]],
|
2019-12-05 12:29:57 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_copy_expand_with_type_params() {
|
2021-03-10 14:05:02 -06:00
|
|
|
check_derive(
|
2019-12-05 12:29:57 -06:00
|
|
|
r#"
|
2021-03-10 14:05:02 -06:00
|
|
|
#[derive(Copy)]
|
|
|
|
struct Foo<A, B>;
|
|
|
|
"#,
|
2020-03-23 14:32:06 -05:00
|
|
|
known::Copy,
|
2021-03-10 14:05:02 -06:00
|
|
|
expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
|
2019-12-05 12:29:57 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_copy_expand_with_lifetimes() {
|
2021-03-10 14:05:02 -06:00
|
|
|
check_derive(
|
2019-12-05 12:29:57 -06:00
|
|
|
r#"
|
2021-03-10 14:05:02 -06:00
|
|
|
#[derive(Copy)]
|
|
|
|
struct Foo<A, B, 'a, 'b>;
|
|
|
|
"#,
|
2020-03-23 14:32:06 -05:00
|
|
|
known::Copy,
|
2021-03-10 14:05:02 -06:00
|
|
|
// We currently just ignore lifetimes
|
|
|
|
expect![["impl<T0:core::marker::Copy,T1:core::marker::Copy>core::marker::CopyforFoo<T0,T1>{}"]],
|
2019-12-05 12:29:57 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_clone_expand() {
|
2021-03-10 14:05:02 -06:00
|
|
|
check_derive(
|
2019-12-05 12:29:57 -06:00
|
|
|
r#"
|
2021-03-10 14:05:02 -06:00
|
|
|
#[derive(Clone)]
|
|
|
|
struct Foo<A, B>;
|
|
|
|
"#,
|
2020-03-23 14:32:06 -05:00
|
|
|
known::Clone,
|
2021-03-10 14:05:02 -06:00
|
|
|
expect![["impl<T0:core::clone::Clone,T1:core::clone::Clone>core::clone::CloneforFoo<T0,T1>{}"]],
|
2019-12-05 12:29:57 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|