Unnamed repository; edit this file 'description' to name the repository.
Diffstat (limited to 'crates/hir_expand/src/proc_macro.rs')
| -rw-r--r-- | crates/hir_expand/src/proc_macro.rs | 24 |
1 files changed, 8 insertions, 16 deletions
diff --git a/crates/hir_expand/src/proc_macro.rs b/crates/hir_expand/src/proc_macro.rs index 025e10239b..1e5fff07d5 100644 --- a/crates/hir_expand/src/proc_macro.rs +++ b/crates/hir_expand/src/proc_macro.rs @@ -2,6 +2,7 @@ use crate::db::AstDatabase; use base_db::{CrateId, ProcMacroId}; +use mbe::ExpandResult; #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] pub struct ProcMacroExpander { @@ -9,15 +10,6 @@ pub struct ProcMacroExpander { proc_macro_id: Option<ProcMacroId>, } -macro_rules! err { - ($fmt:literal, $($tt:tt),*) => { - mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown(format!($fmt, $($tt),*))) - }; - ($fmt:literal) => { - mbe::ExpandError::ProcMacroError(tt::ExpansionError::Unknown($fmt.to_string())) - } -} - impl ProcMacroExpander { pub fn new(krate: CrateId, proc_macro_id: ProcMacroId) -> Self { Self { krate, proc_macro_id: Some(proc_macro_id) } @@ -38,21 +30,21 @@ impl ProcMacroExpander { calling_crate: CrateId, tt: &tt::Subtree, attr_arg: Option<&tt::Subtree>, - ) -> Result<tt::Subtree, mbe::ExpandError> { + ) -> ExpandResult<tt::Subtree> { match self.proc_macro_id { Some(id) => { let krate_graph = db.crate_graph(); - let proc_macro = krate_graph[self.krate] - .proc_macro - .get(id.0 as usize) - .ok_or_else(|| err!("No derive macro found."))?; + let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) { + Some(proc_macro) => proc_macro, + None => return ExpandResult::str_err("No derive macro found.".to_string()), + }; // Proc macros have access to the environment variables of the invoking crate. let env = &krate_graph[calling_crate].env; - proc_macro.expander.expand(tt, attr_arg, env).map_err(mbe::ExpandError::from) + proc_macro.expander.expand(tt, attr_arg, env).map_err(mbe::ExpandError::from).into() } - None => Err(mbe::ExpandError::UnresolvedProcMacro), + None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro), } } } |