|
1 | 1 | //! Proc Macro Expander stub |
2 | 2 |
|
3 | | -use crate::db::AstDatabase; |
4 | | -use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId}; |
| 3 | +use base_db::{CrateId, ProcMacroExpansionError, ProcMacroId, ProcMacroKind}; |
5 | 4 | use mbe::ExpandResult; |
6 | 5 |
|
| 6 | +use crate::db::AstDatabase; |
| 7 | + |
7 | 8 | #[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)] |
8 | 9 | pub struct ProcMacroExpander { |
9 | 10 | krate: CrateId, |
@@ -36,20 +37,29 @@ impl ProcMacroExpander { |
36 | 37 | let krate_graph = db.crate_graph(); |
37 | 38 | let proc_macro = match krate_graph[self.krate].proc_macro.get(id.0 as usize) { |
38 | 39 | Some(proc_macro) => proc_macro, |
39 | | - None => return ExpandResult::str_err("No derive macro found.".to_string()), |
| 40 | + None => return ExpandResult::str_err("No proc-macro found.".to_string()), |
40 | 41 | }; |
41 | 42 |
|
42 | 43 | // Proc macros have access to the environment variables of the invoking crate. |
43 | 44 | let env = &krate_graph[calling_crate].env; |
44 | | - |
45 | | - proc_macro |
46 | | - .expander |
47 | | - .expand(tt, attr_arg, env) |
48 | | - .map_err(|err| match err { |
49 | | - ProcMacroExpansionError::Panic(text) => mbe::ExpandError::Other(text), |
50 | | - ProcMacroExpansionError::System(text) => mbe::ExpandError::Other(text), |
51 | | - }) |
52 | | - .into() |
| 45 | + match proc_macro.expander.expand(tt, attr_arg, env) { |
| 46 | + Ok(t) => ExpandResult::ok(t), |
| 47 | + Err(err) => match err { |
| 48 | + // Don't discard the item in case something unexpected happened while expanding attributes |
| 49 | + ProcMacroExpansionError::System(text) |
| 50 | + if proc_macro.kind == ProcMacroKind::Attr => |
| 51 | + { |
| 52 | + ExpandResult { |
| 53 | + value: tt.clone(), |
| 54 | + err: Some(mbe::ExpandError::Other(text)), |
| 55 | + } |
| 56 | + } |
| 57 | + ProcMacroExpansionError::System(text) |
| 58 | + | ProcMacroExpansionError::Panic(text) => { |
| 59 | + ExpandResult::only_err(mbe::ExpandError::Other(text)) |
| 60 | + } |
| 61 | + }, |
| 62 | + } |
53 | 63 | } |
54 | 64 | None => ExpandResult::only_err(mbe::ExpandError::UnresolvedProcMacro), |
55 | 65 | } |
|
0 commit comments