Switch BuiltinDeriveExpander::expand to ExpandResult

This commit is contained in:
Lukas Wirth 2021-08-20 14:28:36 +02:00
parent 557df6ff3f
commit 269de9abe3
3 changed files with 19 additions and 23 deletions

View file

@ -2,6 +2,7 @@
use log::debug; use log::debug;
use mbe::ExpandResult;
use parser::FragmentKind; use parser::FragmentKind;
use syntax::{ use syntax::{
ast::{self, AstNode, GenericParamsOwner, ModuleItemOwner, NameOwner}, ast::{self, AstNode, GenericParamsOwner, ModuleItemOwner, NameOwner},
@ -23,7 +24,7 @@ macro_rules! register_builtin {
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> ExpandResult<tt::Subtree> {
let expander = match *self { let expander = match *self {
$( BuiltinDeriveExpander::$trait => $expand, )* $( BuiltinDeriveExpander::$trait => $expand, )*
}; };
@ -147,11 +148,11 @@ fn make_type_args(n: usize, bound: Vec<tt::TokenTree>) -> Vec<tt::TokenTree> {
result result
} }
fn expand_simple_derive( fn expand_simple_derive(tt: &tt::Subtree, trait_path: tt::Subtree) -> ExpandResult<tt::Subtree> {
tt: &tt::Subtree, let info = match parse_adt(tt) {
trait_path: tt::Subtree, Ok(info) => info,
) -> Result<tt::Subtree, mbe::ExpandError> { Err(e) => return ExpandResult::only_err(e),
let info = parse_adt(tt)?; };
let name = info.name; let name = info.name;
let trait_path_clone = trait_path.token_trees.clone(); let trait_path_clone = trait_path.token_trees.clone();
let bound = (quote! { : ##trait_path_clone }).token_trees; let bound = (quote! { : ##trait_path_clone }).token_trees;
@ -161,7 +162,7 @@ fn expand_simple_derive(
let expanded = quote! { let expanded = quote! {
impl ##type_params ##trait_path for #name ##type_args {} impl ##type_params ##trait_path for #name ##type_args {}
}; };
Ok(expanded) ExpandResult::ok(expanded)
} }
fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree { fn find_builtin_crate(db: &dyn AstDatabase, id: MacroCallId) -> tt::TokenTree {
@ -186,7 +187,7 @@ fn copy_expand(
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> ExpandResult<tt::Subtree> {
let krate = find_builtin_crate(db, id); let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::marker::Copy }) expand_simple_derive(tt, quote! { #krate::marker::Copy })
} }
@ -195,7 +196,7 @@ fn clone_expand(
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> ExpandResult<tt::Subtree> {
let krate = find_builtin_crate(db, id); let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::clone::Clone }) expand_simple_derive(tt, quote! { #krate::clone::Clone })
} }
@ -204,7 +205,7 @@ fn default_expand(
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> ExpandResult<tt::Subtree> {
let krate = find_builtin_crate(db, id); let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::default::Default }) expand_simple_derive(tt, quote! { #krate::default::Default })
} }
@ -213,7 +214,7 @@ fn debug_expand(
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> ExpandResult<tt::Subtree> {
let krate = find_builtin_crate(db, id); let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::fmt::Debug }) expand_simple_derive(tt, quote! { #krate::fmt::Debug })
} }
@ -222,16 +223,12 @@ fn hash_expand(
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> ExpandResult<tt::Subtree> {
let krate = find_builtin_crate(db, id); let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::hash::Hash }) expand_simple_derive(tt, quote! { #krate::hash::Hash })
} }
fn eq_expand( fn eq_expand(db: &dyn AstDatabase, id: MacroCallId, tt: &tt::Subtree) -> ExpandResult<tt::Subtree> {
db: &dyn AstDatabase,
id: MacroCallId,
tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> {
let krate = find_builtin_crate(db, id); let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::cmp::Eq }) expand_simple_derive(tt, quote! { #krate::cmp::Eq })
} }
@ -240,7 +237,7 @@ fn partial_eq_expand(
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> ExpandResult<tt::Subtree> {
let krate = find_builtin_crate(db, id); let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::cmp::PartialEq }) expand_simple_derive(tt, quote! { #krate::cmp::PartialEq })
} }
@ -249,7 +246,7 @@ fn ord_expand(
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> ExpandResult<tt::Subtree> {
let krate = find_builtin_crate(db, id); let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::cmp::Ord }) expand_simple_derive(tt, quote! { #krate::cmp::Ord })
} }
@ -258,7 +255,7 @@ fn partial_ord_expand(
db: &dyn AstDatabase, db: &dyn AstDatabase,
id: MacroCallId, id: MacroCallId,
tt: &tt::Subtree, tt: &tt::Subtree,
) -> Result<tt::Subtree, mbe::ExpandError> { ) -> ExpandResult<tt::Subtree> {
let krate = find_builtin_crate(db, id); let krate = find_builtin_crate(db, id);
expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd }) expand_simple_derive(tt, quote! { #krate::cmp::PartialOrd })
} }

View file

@ -60,7 +60,7 @@ impl TokenExpander {
mbe::ExpandError::Other("No item argument for attribute".to_string()).into(), mbe::ExpandError::Other("No item argument for attribute".to_string()).into(),
), ),
}, },
TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt).into(), TokenExpander::BuiltinDerive(it) => it.expand(db, id, tt),
TokenExpander::ProcMacro(_) => { TokenExpander::ProcMacro(_) => {
// We store the result in salsa db to prevent non-deterministic behavior in // We store the result in salsa db to prevent non-deterministic behavior in
// some proc-macro implementation // some proc-macro implementation

View file

@ -62,8 +62,7 @@ fn remove_attr_invoc(item: ast::Item, attr_index: usize) -> ast::Item {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use base_db::fixture::WithFixture; use base_db::{fixture::WithFixture, SourceDatabase};
use base_db::SourceDatabase;
use expect_test::{expect, Expect}; use expect_test::{expect, Expect};
use crate::test_db::TestDB; use crate::test_db::TestDB;